UIManager.cs 7.4 KB

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