UIManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. using ReferenceCollector = Fort23.Mono.ReferenceCollector;
  8. namespace Mono
  9. {
  10. public class UIManager : Utility.Singleton<UIManager>
  11. {
  12. public Dictionary<string, UIPanel> AllPanel = new Dictionary<string, UIPanel>();
  13. public Dictionary<string, List<UICom>> AllUIComPool = new Dictionary<string, List<UICom>>();
  14. public enum UILayer
  15. {
  16. Bottom, //最底层UI(主界面)
  17. Middle, //中间UI(各种Panel)
  18. Top, //最顶层UI节点
  19. }
  20. public GameObject UIRoot;
  21. public RectTransform BottomRoot;
  22. public RectTransform MiddleRoot;
  23. public RectTransform TopRoot;
  24. public ReferenceCollector ReferenceCollector;
  25. public Camera UICamera;
  26. public CustomCameraStack CurrCustomCameraStack;
  27. public void SetUIRoot(GameObject gameObject)
  28. {
  29. UIRoot = gameObject;
  30. ReferenceCollector = gameObject.GetComponent<ReferenceCollector>();
  31. BottomRoot = ReferenceCollector.Get<RectTransform>("BottomRoot");
  32. MiddleRoot = ReferenceCollector.Get<RectTransform>("MiddleRoot");
  33. TopRoot = ReferenceCollector.Get<RectTransform>("TopRoot");
  34. UICamera = ReferenceCollector.Get<Camera>("MainCamera");
  35. }
  36. /// <summary>
  37. /// 加载打开一个界面
  38. /// </summary>
  39. /// <param name="uiLayer"></param>
  40. /// <typeparam name="T"></typeparam>
  41. /// <returns></returns>
  42. public T LoadAndOpenPanel<T>(UILayer uiLayer) where T : UIPanel, new()
  43. {
  44. Type type;
  45. type = typeof(T);
  46. T uiPanel;
  47. // 获取UI绑定属性
  48. UIBindingAttribute uiBindingAttribute = type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  49. if (uiBindingAttribute == null)
  50. {
  51. return null;
  52. }
  53. Transform root = null;
  54. switch (uiLayer)
  55. {
  56. case UILayer.Bottom:
  57. root = BottomRoot;
  58. break;
  59. case UILayer.Middle:
  60. root = MiddleRoot;
  61. break;
  62. case UILayer.Top:
  63. root = TopRoot;
  64. break;
  65. }
  66. string name = type.ToString();
  67. if (!AllPanel.ContainsKey(name))
  68. {
  69. uiPanel = UIRoot.AddComponent<T>();
  70. GameObject prefab = Resources.Load<GameObject>("UIPanel/" + uiBindingAttribute.prefab);
  71. GameObject gameObject = Object.Instantiate(prefab, root);
  72. uiPanel.SetUIRoot(gameObject);
  73. AllPanel.Add(name, uiPanel);
  74. }
  75. else
  76. {
  77. uiPanel = (T)AllPanel[name];
  78. uiPanel.Root.transform.SetParent(root);
  79. }
  80. uiPanel.DelEvent();
  81. uiPanel.Show();
  82. return uiPanel;
  83. }
  84. /// <summary>
  85. /// 关闭界面
  86. /// </summary>
  87. /// <param name="isClose"></param>
  88. /// <typeparam name="T"></typeparam>
  89. public void HideUIPanel<T>(bool isClose = false) where T : UIPanel, new()
  90. {
  91. Type type;
  92. type = typeof(T);
  93. string name = type.ToString();
  94. if (AllPanel.ContainsKey(name))
  95. {
  96. if (isClose)
  97. {
  98. AllPanel[name].Dispose();
  99. AllPanel.Remove(name);
  100. }
  101. else
  102. {
  103. AllPanel[name].Hide();
  104. }
  105. }
  106. else
  107. {
  108. Debug.Log("没有这个界面");
  109. }
  110. }
  111. /// <summary>
  112. /// 创建组件
  113. /// </summary>
  114. /// <param name="parent"></param>
  115. /// <param name="root"></param>
  116. /// <typeparam name="T"></typeparam>
  117. /// <returns></returns>
  118. public T CreatUICom<T>(UIBasic parent, Transform root) where T : UICom, new()
  119. {
  120. Type type;
  121. type = typeof(T);
  122. T uiCom;
  123. // 获取UI绑定属性
  124. UIBindingAttribute uiBindingAttribute = type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  125. if (uiBindingAttribute == null)
  126. {
  127. return null;
  128. }
  129. string name = type.ToString();
  130. if (!AllUIComPool.ContainsKey(name))
  131. {
  132. uiCom = new T();
  133. GameObject prefab = Resources.Load<GameObject>("UICom/" + uiBindingAttribute.prefab);
  134. GameObject gameObject = Object.Instantiate(prefab, root);
  135. uiCom.SetUIRoot(gameObject);
  136. }
  137. else
  138. {
  139. uiCom = (T)AllUIComPool[name][0];
  140. AllUIComPool[name].Remove(uiCom);
  141. if (AllUIComPool[name].Count == 0)
  142. {
  143. AllUIComPool.Remove(name);
  144. }
  145. }
  146. uiCom.Root.transform.SetParent(root);
  147. if (!parent.AllUICom.ContainsKey(name))
  148. {
  149. List<UICom> uiComs = new List<UICom>();
  150. uiComs.Add(uiCom);
  151. parent.AllUICom.Add(name, uiComs);
  152. }
  153. else
  154. {
  155. parent.AllUICom[name].Add(uiCom);
  156. }
  157. uiCom.DelEvent();
  158. uiCom.Show();
  159. return uiCom;
  160. }
  161. /// <summary>
  162. /// 回收组件
  163. /// </summary>
  164. /// <param name="uiCom"></param>
  165. public void RecUICom(UIBasic parent, UICom uiCom)
  166. {
  167. string name = uiCom.ToString();
  168. if (!AllUIComPool.ContainsKey(name))
  169. {
  170. List<UICom> coms = new List<UICom>();
  171. coms.Add(uiCom);
  172. uiCom.Hide();
  173. AllUIComPool.Add(name, coms);
  174. }
  175. else
  176. {
  177. uiCom.Hide();
  178. AllUIComPool[name].Add(uiCom);
  179. }
  180. if (parent.AllUICom.ContainsKey(name))
  181. {
  182. parent.AllUICom[name].Remove(uiCom);
  183. if (parent.AllUICom[name].Count == 0)
  184. {
  185. parent.AllUICom.Remove(name);
  186. }
  187. }
  188. }
  189. }
  190. }