using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
/// <summary>
/// C层,通过管理器拿到obj,调用V层的通用函数对obj进行操作
/// </summary>
public class UIBase : MonoBehaviour {
void Awake () {
}
/// <summary>
/// 找到并收集要用的子控件(这里标记为“_N,_S”),为子控件添加行为脚本
/// </summary>
public void AddBehavioursInChildren()
{
Transform[] childrenTrans = transform.GetComponentsInChildren<Transform>();
print("进入Base");
foreach (var v in childrenTrans)
{
if (v.name.EndsWith("_N"))
{
v.gameObject.AddComponent<UIBehaviours>();
}
else if (v.name.EndsWith("_S"))
{
v.gameObject.AddComponent<UISubManager>();
}
}
}
public GameObject GetOBJ(string widgeName)
{
return UIManager.instance.GetChild(transform.name,widgeName);
}
public void AddButLister(string widgeName,UnityAction action)
{
GameObject tmpObj = GetOBJ(widgeName);
if (tmpObj != null)
{
UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
tmpBehav.AddButListen(action);
}
}
public void ChangeImage(string widgeName, Sprite tmpSprit)
{
//GameObject tmpObj = GetOBJ(widgeName);
//if (tmpObj != null)
//{
// UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
// tmpBehav.ImageChange(tmpSprit);
//}
UIBehaviours tmpBehaviours = GetBehaviours(widgeName);
if (tmpBehaviours != null)
{
tmpBehaviours.ImageChange(tmpSprit);
}
}
/// <summary>
/// sub.API
/// </summary>
/// <param name="cellName"></param>
/// <param name="widgeName"></param>
/// <param name="tmpSprit"></param>
public void ChangeImage(string cellName, string widgeName, Sprite tmpSprit)
{
GameObject obj = GetOBJ(cellName);
if (obj != null)
{
UISubManager subManager = obj.GetComponent<UISubManager>();
if (subManager != null)
{
subManager.ImageChange(widgeName, tmpSprit);
}
}
}
public UIBehaviours GetBehaviours(string widgeName)
{
GameObject tmpObj = GetOBJ(widgeName);
//UIBehaviours tmpBehav;
if (tmpObj != null)
{
UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
return tmpBehav;
}
return null;
}
/// <summary>
/// 注册自己
/// </summary>
/// <param name="pageName">自己的名字</param>
/// <param name="Name">自己的名字</param>
/// <param name="obj">自己的obj</param>
public void RegistPage(string pageName,string Name,GameObject obj)
{
UIManager.instance.RegistGameObject(name, name,gameObject);
}
public void AddComponent(Type myComponent,string widgeName)
{
UIBehaviours tmpBehaviours = GetBehaviours(widgeName);
if (tmpBehaviours != null)
{
tmpBehaviours.AddComponent(myComponent);
}
}
private void OnDestroy()
{
UIManager.instance.UnRegistPanel(transform.name);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// UI管理器,存储需要使用的obj,此类需要先加载
/// </summary>
public class UIManager : MonoBehaviour {
public static UIManager instance;
public Dictionary<string, Dictionary<string, GameObject>> allChildren;
void Awake () {
instance = this;
allChildren = new Dictionary<string, Dictionary<string, GameObject>>();
}
/// <summary>
/// 注册obj
/// </summary>
/// <param name="panleName"></param>
/// <param name="widgeName"></param>
/// <param name="obj"></param>
public void RegistGameObject(string panleName,string widgeName,GameObject obj)
{
if (!allChildren.ContainsKey(panleName))
{
allChildren[panleName] = new Dictionary<string, GameObject>();
}
if (!allChildren[panleName].ContainsKey(widgeName))
{
allChildren[panleName].Add(widgeName, obj);
}
else
{
Debug.Log("has regist obj==" + widgeName + "panleName" + panleName);
}
}
/// <summary>
/// 对外提供的获取gameobject的函数
/// </summary>
/// <param name="panleName"></param>
/// <param name="widgeName"></param>
/// <returns></returns>
public GameObject GetChild(string panleName,string widgeName)
{
if (allChildren.ContainsKey(panleName))
{
return allChildren[panleName][widgeName];
}
return null;
}
/// <summary>
/// 清理obj
/// 清理时先把obj赋值给临时变量,在清理内存,而后清理字典
/// </summary>
/// <param name="panelNmae"></param>
public void UnRegistPanel(string panelNmae)
{
if (allChildren.ContainsKey(panelNmae))
{
Dictionary<string, GameObject> panelChild = allChildren[panelNmae];
panelChild.Clear();
allChildren.Remove(panelNmae);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
/// <summary>
/// V层
/// </summary>
public class UIBehaviours : MonoBehaviour {
void Awake () {
UIBase tmpRoot= transform.GetComponentInParent<UIBase>();
UIManager.instance.RegistGameObject(tmpRoot.name, transform.name, gameObject);
}
public void AddButListen(UnityAction action)
{
Button tmpBut = transform.GetComponent<Button>();
if (tmpBut != null)
{
tmpBut.onClick.AddListener(action);
}
}
public void AddSliderListen(UnityAction<float> action)
{
Slider tmpBut = transform.GetComponent<Slider>();
if (tmpBut != null)
{
tmpBut.onValueChanged.AddListener(action);
}
}
public void AddInputValueChangeListen(UnityAction<string> action)
{
InputField tmpBut = transform.GetComponent<InputField>();
if (tmpBut != null)
{
tmpBut.onValueChanged.AddListener(action);
}
}
public void AddComponent(Type tmpType)
{
gameObject.AddComponent(tmpType);
}
public void ImageChange(Sprite tmpSprit)
{
Image tmpImage = transform.GetComponent<Image>();
if (tmpImage != null)
{
tmpImage.sprite = tmpSprit;
}
}
public void ChangeLable(string content)
{
Text tmpText = transform.GetComponent<Text>();
if (tmpText != null)
{
tmpText.text = content;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// 二级管理单位
/// 在出现同名时需使用二级管理
/// </summary>
public class UISubManager : MonoBehaviour {
Dictionary<string, GameObject> allChild;
private void Awake()
{
UIBase tmpBase = transform.GetComponentInParent<UIBase>();
UIManager.instance.RegistGameObject(tmpBase.name, name, gameObject);
allChild = new Dictionary<string, GameObject>();
Transform[] tmpAll = GetComponentsInChildren<Transform>();
foreach(var v in tmpAll)
{
if (v.name.EndsWith("_M"))
{
allChild.Add(v.name, v.gameObject);
}
}
}
public GameObject GetChild(string widgeName)
{
return allChild[widgeName];
}
public void AddButListen(string widgeName, UnityAction action)
{
GameObject child = GetChild(widgeName);
if (child != null)
{
Button but = child.GetComponent<Button>();
if (but != null)
{
but.onClick.AddListener(action);
}
}
}
public void AddSliderListen(string widgeName, UnityAction<float> action)
{
GameObject child = GetChild(widgeName);
if (child != null)
{
Slider slider = child.GetComponent<Slider>();
if (slider != null)
{
slider.onValueChanged.AddListener(action);
}
}
}
public void ImageChange(string widgeName, Sprite tmpSprit)
{
GameObject child = GetChild(widgeName);
if (child != null)
{
Image image = child.GetComponent<Image>();
if (image != null)
{
image.sprite = tmpSprit;
}
}
}
private void OnDestroy()
{
if (allChild != null)
{
allChild.Clear();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
///绑定层, 继承C层的管理类,以panle为单位管理游戏时,此脚本挂载到panle上
/// 由于继承了C层,我们直接调用即可。
/// </summary>
public class PanleCtrl : UIBase {
PageLogic pageLogic;
PageModle pageModle;
void Awake () {
AddBehavioursInChildren();
pageLogic = new PageLogic();
pageModle = new PageModle();
RegistPage(name, name, gameObject);
print(UIManager.instance.GetChild(name, name).name);
}
private void Start()
{
print("进入STRAT");
print(pageLogic.ResourceImage());
ChangeImage("Content_S", "Image_M", pageLogic.ResourceImage());
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 界面切换,继承UIBase,
/// </summary>
public class ChangePageCtrl : UIBase {
PageLogic pageLogic;
PageModle pageModle;
private void Awake()
{
AddBehavioursInChildren();
}
void Start () {
pageLogic = new PageLogic();
pageModle = new PageModle();
InitChangePage();
}
public void InitChangePage()
{
print("进入初始化");
if (pageLogic.RegistPage())
{
pageLogic.InitPage("Panel");
}
AddButLister(ConstantManager.changePageBut, ()=>SwitchCurrentPage(ConstantManager.page));
AddButLister(ConstantManager.changePageBut_01, () => SwitchCurrentPage(ConstantManager.page_01));
AddButLister(ConstantManager.changePageBut_02, () => SwitchCurrentPage(ConstantManager.page_02));
}
public void SwitchCurrentPage(string pageName)
{
pageLogic.PageSwitch(pageName);
print("切换到"+pageName);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// M层,数据管理
/// </summary>
public class PageModle : MonoBehaviour {
public Sprite s;
public Sprite s1;
public Sprite s2;
/// <summary>
/// 页面
/// </summary>
public GameObject Page;
public GameObject Page_01;
public GameObject Page_02;
public List<GameObject> pageArray;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 逻辑层,为绑定层服务
/// </summary>
public class PageLogic : MonoBehaviour {
public PageModle pageModle;
/// <summary>
/// 界面注册是否完成
/// </summary>
/// <returns></returns>
public bool RegistPage()
{
if(UIManager.instance.GetChild(ConstantManager.page, ConstantManager.page) !=null&&
UIManager.instance.GetChild(ConstantManager.page_01, ConstantManager.page_01) != null&&
UIManager.instance.GetChild(ConstantManager.page_02, ConstantManager.page_02) != null
)
{
return true;
}
return false;
}
public void OnClick()
{
Debug.Log("but coming");
}
public void PageSwitch(string pageName)
{
for(int i = 0; i < pageModle.pageArray.Count;i++)
{
if (pageModle.pageArray[i].name.Equals(pageName))
{
pageModle.pageArray[i].SetActive(true);
}
else
{
pageModle.pageArray[i].SetActive(false);
}
}
}
public void InitPage(string mainPage)
{
pageModle = new PageModle();
pageModle.pageArray = new List<GameObject>();
print("进入初始化" );
print("初始化panle"+UIManager.instance.GetChild("Panel", "Panel"));
pageModle.pageArray.Add(UIManager.instance.GetChild("Panel", "Panel"));
pageModle.pageArray.Add(UIManager.instance.GetChild("Panel_01", "Panel_01"));
pageModle.pageArray.Add(UIManager.instance.GetChild("Panel_02", "Panel_02"));
foreach (var v in pageModle.pageArray)
{
print("主界面"+mainPage);
if (v.name.Equals(mainPage))
{
v.SetActive(true);
print("启用主界面"+"+"+v.name);
}
else
{
v.SetActive(false);
print("禁用其他界面"+ "+" + v.name);
}
}
}
public Sprite ResourceImage()
{
print("进入加载图片");
return Resources.Load<Sprite>("1534238263(1)");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 常量类
/// </summary>
public static class ConstantManager {
public const string page = "Panel";
public const string page_01 = "Panel_01";
public const string page_02 = "Panel_02";
public const string changePageBut = "Button_N";
public const string changePageBut_01 = "Button (1)_N";
public const string changePageBut_02 = "Button (2)_N";
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 功能测试脚本
/// </summary>
public class FunctionTest : MonoBehaviour {
void Start () {
}
void Update () {
}
}