赞
踩
泰课指路牌:https://www.taikr.com/course/1062/task/31006/show.
面板基类代码:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; /// <summary> /// 面板基类 ///找到所有自己面板下的控件对象 ///提供显式/隐藏的行为 /// </summary> public class BasePanel : MonoBehaviour { //里氏转换原则,所有UI都会继承UIBehaviour 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>(); } //让子类重写(覆盖)此方法,来实现UI的隐藏与出现 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; } //找到相对应的UI控件并记录到字典中 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) => { // 因为是UI所以需要拖到Canvas下的父对象 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; } // 设置父对象,相对位置和大小,因为涉及到屏幕坐标,as两行是为了消除偏差 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); //LoginPanel p=this.gameobject.GetComponent<LoginPanel>(); //p.IninInfo(); } 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管理模块
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。