UIManager.cs 6.3 KB

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