using CG.UTility; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// /// UI管理类,初始挂载,全局存在 /// namespace CG.Framework { public enum UIGroup { BG = 0, Main = 1, Top = 2, Tip = 3 } public class UI_Manage : MonoSingleton { /// 挂载UI的Canvas private Transform _mainCanvas; private Transform _bgPanelGroup; private Transform _mainPanelGroup; private Transform _topPanelGroup; private Transform _tipPanelGroup; private Dictionary _allPanelDic;//对应的UIpanel private Dictionary> _allWedgeDic;//所有注册的UI protected override void AwakeSelf() { base.AwakeSelf(); _allPanelDic = new Dictionary(); _allWedgeDic = new Dictionary>(); if (_mainCanvas == null) { GameObject go = GameObject.FindGameObjectWithTag("MainCanvas"); if (go != null) _mainCanvas = go.transform; } if (_bgPanelGroup == null) { _bgPanelGroup = _mainCanvas.transform.Find("BG"); } if (_mainPanelGroup == null) { _mainPanelGroup = _mainCanvas.transform.Find("Main"); } if (_topPanelGroup == null) { _topPanelGroup = _mainCanvas.transform.Find("Top"); } if (_tipPanelGroup == null) { _tipPanelGroup = _mainCanvas.transform.Find("Tip"); } } /// /// 注册UI物体(挂有UIBase) /// /// /// public void RegisterUIPanel(string panelName, GameObject ui) { if (!_allPanelDic.ContainsKey(panelName)) { _allPanelDic.Add(panelName, ui); } } /// /// 注销UI物体(挂有UIBase) /// /// public void UnRegisterUIPanel(string panelName) { if (_allPanelDic.ContainsKey(panelName)) { _allPanelDic.Remove(panelName); } } /// /// 注册game object(挂有UIBehaviours) /// /// /// /// public void RegisterUIWedge(string panelName, string wedgeName, GameObject obj) { if (!_allWedgeDic.ContainsKey(panelName)) { _allWedgeDic[panelName] = new Dictionary(); } if (!_allWedgeDic[panelName].ContainsKey(wedgeName)) { _allWedgeDic[panelName].Add(wedgeName, obj); } } /// /// 注销game object(挂有UIBehaviours) /// /// /// /// public void UnRegisterUIWedge(string panelName, string wedgeName, GameObject obj) { if (_allWedgeDic.ContainsKey(panelName)) { if (_allWedgeDic[panelName].ContainsKey(wedgeName)) { _allWedgeDic[panelName].Remove(wedgeName); } } } /// /// 注销某个Panel里面的所有Wedge /// /// public void UnRegisterAllWedgeInPanel(string panelName) { if (_allWedgeDic.ContainsKey(panelName)) { Dictionary tmpPanel = _allWedgeDic[panelName]; _allWedgeDic.Remove(panelName); tmpPanel.Clear(); } } /// /// 获取到注册的Wedget /// /// /// /// public GameObject GetWedget(string panelName, string wedgeName) { if (_allWedgeDic.ContainsKey(panelName)) { return _allWedgeDic[panelName][wedgeName]; } return null; } /// /// 获取到注册的Panel /// /// /// public GameObject GetPanel(string panelName) { if (_allPanelDic.ContainsKey(panelName)) { return _allPanelDic[panelName]; } return null; } /// /// 加载Panel并挂到对应Canvas下 /// /// 要加载的Panel名 /// Panel父物体 /// public GameObject ShowPanel(string name,Type typeCtrl,UIGroup uIGroup,int orderInex=-1, Transform parent = null) { GameObject uiGeoTemp = null; Transform cv = _mainCanvas; switch (uIGroup) { case UIGroup.BG: cv = _bgPanelGroup; break; case UIGroup.Main: cv = _mainPanelGroup; break; case UIGroup.Top: cv = _topPanelGroup; break; case UIGroup.Tip: cv = _tipPanelGroup; break; } if (parent != null) cv = parent; if (!_allPanelDic.ContainsKey(name)) { GameObject pf = Resources.Load(ConstCtrl.RSC_Folder_UIPATH + name); if (pf != null) { GameObject go = Instantiate(pf); //Type type = Type.GetType(typeCtrl); //if (type == null) //{ // type = Type.GetType("ZXK.GYJQR." + typeCtrl);//查找时需要带有命名空间,否则访问不到 //} if (typeCtrl!=null) { go.AddComponent(typeCtrl); } go.transform.SetParent(cv, false); if (orderInex == -1) { go.transform.SetAsLastSibling(); } else { go.transform.SetSiblingIndex(orderInex); } uiGeoTemp = go; } } else { uiGeoTemp = _allPanelDic[name] != null ? _allPanelDic[name] : null; } uiGeoTemp?.gameObject.SetActive(true); return uiGeoTemp; } /// /// 关闭Panel-直接销毁 /// /// 要关闭的Panel名 /// 角色head上的Canvas public void ClosePanel(string name) { if (_allPanelDic.ContainsKey(name)) { Destroy(_allPanelDic[name]); } } public void HidePanel(string name) { if (_allPanelDic.ContainsKey(name)) { _allPanelDic[name].SetActive(false); } } /// /// 清除某一组UI /// /// public void ClearPanelGroup(UIGroup uIGroup) { switch (uIGroup) { case UIGroup.BG: UIBase[] bgBase = _bgPanelGroup.GetComponentsInChildren(true); for (int i = 0; i < bgBase.Length; i++) { Destroy(bgBase[i].gameObject); } break; case UIGroup.Main: UIBase[] mainBase = _mainPanelGroup.GetComponentsInChildren(true); for (int i = 0; i < mainBase.Length; i++) { Destroy(mainBase[i].gameObject); } break; case UIGroup.Top: UIBase[] topBase = _topPanelGroup.GetComponentsInChildren(true); for (int i = 0; i < topBase.Length; i++) { Destroy(topBase[i].gameObject); } break; case UIGroup.Tip: UIBase[] tipBase = _tipPanelGroup.GetComponentsInChildren(true); for (int i = 0; i < tipBase.Length; i++) { Destroy(tipBase[i].gameObject); } break; } } /// /// 隐藏某一组UI /// /// public void HidePanelGroup(UIGroup uIGroup) { switch (uIGroup) { case UIGroup.BG: foreach (Transform item in _bgPanelGroup) { item.gameObject.SetActive(false); } break; case UIGroup.Main: foreach (Transform item in _mainPanelGroup) { item.gameObject.SetActive(false); } break; case UIGroup.Top: foreach (Transform item in _topPanelGroup) { item.gameObject.SetActive(false); } break; case UIGroup.Tip: foreach (Transform item in _tipPanelGroup) { item.gameObject.SetActive(false); } break; } } } }