UI管理模块
泰课指路牌:https://www.taikr.com/course/1062/task/31006/show.
统一管理UI以及相关(UGUI),提高代码复用率,降低文件耦合性。
面板基类代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class BasePanel : MonoBehaviour
{
private Dictionary<string, List<UIBehaviour>> uiDic = new Dictionary<string, List<UIBehaviour>>();
private void Awake()
{
FindChildrenUIComponent<Button>();
FindChildrenUIComponent<Image>();
FindChildrenUIComponent<Text>();
FindChildrenUIComponent<Toggle>();
FindChildrenUIComponent<Slider>();
FindChildrenUIComponent<ScrollRect>();
}
public virtual void ShowMe()
{
}
public virtual void HideMe()
{
}
protected T GetControl<T>(string name) where T : UIBehaviour
{
if (uiDic.ContainsKey(name))
{
for (int i = 0; i < uiDic[name].Count; i++)
{
if (uiDic[name][i] is T)
return uiDic[name][i] as T;
}
}
return null;
}
private void FindChildrenUIComponent<T>() where T : UIBehaviour
{
T[] array = this.GetComponentsInChildren<T>();
string objName;
for (int i = 0; i < array.Length; i++)
{
objName = array[i].gameObject.name;
if (uiDic.ContainsKey(objName))
uiDic[objName].Add(array[i]);
else
uiDic.Add(objName, new List<UIBehaviour>() { array[i] });
}
}
}
在UI的Canvas上挂载一个脚本继承自这个BasePanel,这样这个Canvas下面所有的UI都可以被GetControl直接找到
面板基类的使用:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UITest : BasePanel
{
private void Start()
{
GetControl<Button>("Button").onClick.AddListener(() =>
{
print("开始游戏");
});
GetControl<Button>("Button2").onClick.AddListener(() =>
{
print("设置");
});
}
}
UI管理器 在编写UI管理器,要确保分辨率问题,通过修改场景Canvas的属性来保证
主要是修改Canvas Scaler (画布定标器) 关于Canvas Scaler 的详解,下面两篇文章讲解 自适应神器------Canvas Scaler (画布定标器) 深入理解Canvas Scaler
UIManager:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum E_UI_Layer
{
Bot,
Mid,
Top,
System
}
public class UIManager : SignleBaseManager<UIManager>
{
public Dictionary<string, BasePanel> panelDic = new Dictionary<string, BasePanel>();
private Transform bot;
private Transform mid;
private Transform top;
private Transform system;
public UIManager()
{
GameObject obj = ResManager.getInstance().Load<GameObject>("canvas");
Transform canvas = obj.transform;
GameObject.DontDestroyOnLoad(obj);
bot = canvas.Find("Bot");
mid = canvas.Find("Mid");
top = canvas.Find("Top");
system = canvas.Find("System");
obj = ResManager.getInstance().Load<GameObject>("EventSystem");
GameObject.DontDestroyOnLoad(obj);
}
public void ShowPanel<T>(string panelName, E_UI_Layer layer, Action<T> callback) where T : BasePanel
{
if (panelDic.ContainsKey(panelName))
{
panelDic[panelName].ShowMe();
callback?.Invoke(panelDic[panelName] as T);
}
ResManager.getInstance().LoadAysnc<GameObject>("path" + panelName, (obj) =>
{
Transform father = bot;
switch (layer)
{
case E_UI_Layer.Bot:
father = bot;
break;
case E_UI_Layer.Mid:
father = mid;
break;
case E_UI_Layer.Top:
father = top;
break;
case E_UI_Layer.System:
father = system;
break;
default:
break;
}
obj.transform.SetParent(father);
obj.transform.localPosition = Vector3.one;
obj.transform.localScale = Vector3.one;
(obj.transform as RectTransform).offsetMax = Vector2.zero;
(obj.transform as RectTransform).offsetMin = Vector2.zero;
T panel = obj.GetComponent<T>();
callback?.Invoke(panel);
panel.ShowMe();
panelDic.Add(panelName, panel);
});
}
public void HidePanel(string panelName)
{
if (panelDic.ContainsKey(panelName))
{
panelDic[panelName].HideMe();
GameObject.Destroy(panelDic[panelName].gameObject);
panelDic.Remove(panelName);
}
}
}
public class UItest :MonoBehaviour
{
private void Start()
{
UIManager.GetInstance().ShowPanel<LoginPanel>("LoginPanel",
E_UI_Layer.Top,
ShowPanelOver);
}
private void ShowPanelOver(LoginPanel panel) {
panel.InitInfo();
Invoke("DelayHide", 6);
}
private void DelayHide() {
UIManager.GetInstance().HidePanel("LoginPanel");
}
}
相关链接 Unity程序基础框架__单例基类模块 Unity程序基础框架__缓存池模块 Unity程序基础框架__事件中心模块 Unity程序基础框架__公共Mono模块 Unity程序基础框架__场景切换模块 Unity程序基础框架__资源加载模块 Unity程序基础框架__输入控制模块 Unity程序基础框架__事件中心模块基类优化 Unity程序基础框架__音效管理模块 Unity程序基础框架__UI管理模块
|