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.Root.transform.SetAsLastSibling();
  108. uiPanel.DelEvent();
  109. uiPanel.Show();
  110. return uiPanel;
  111. }
  112. /// <summary>
  113. /// 关闭界面
  114. /// </summary>
  115. /// <param name="isClose"></param>
  116. /// <typeparam name="T"></typeparam>
  117. public void HideUIPanel<T>(bool isClose = false) where T : UIPanel, new()
  118. {
  119. Type type;
  120. type = typeof(T);
  121. string name = type.ToString();
  122. if (AllPanel.ContainsKey(name))
  123. {
  124. if (isClose)
  125. {
  126. AllPanel[name].Dispose();
  127. AllPanel.Remove(name);
  128. }
  129. else
  130. {
  131. AllPanel[name].Hide();
  132. }
  133. }
  134. else
  135. {
  136. Debug.Log("没有这个界面");
  137. }
  138. }
  139. /// <summary>
  140. /// 创建组件
  141. /// </summary>
  142. /// <param name="parent"></param>
  143. /// <param name="root"></param>
  144. /// <typeparam name="T"></typeparam>
  145. /// <returns></returns>
  146. public T CreatUICom<T>(UIBasic parent, Transform root) where T : UICom, new()
  147. {
  148. Type type;
  149. type = typeof(T);
  150. T uiCom;
  151. // 获取UI绑定属性
  152. UIBindingAttribute uiBindingAttribute = type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  153. if (uiBindingAttribute == null)
  154. {
  155. return null;
  156. }
  157. string name = type.ToString();
  158. if (!AllUIComPool.ContainsKey(name))
  159. {
  160. uiCom = new T();
  161. GameObject prefab = Resources.Load<GameObject>("UICom/" + uiBindingAttribute.prefab);
  162. GameObject gameObject = Object.Instantiate(prefab, root);
  163. uiCom.SetUIRoot(gameObject);
  164. }
  165. else
  166. {
  167. uiCom = (T)AllUIComPool[name][0];
  168. AllUIComPool[name].Remove(uiCom);
  169. if (AllUIComPool[name].Count == 0)
  170. {
  171. AllUIComPool.Remove(name);
  172. }
  173. }
  174. uiCom.Root.transform.SetParent(root);
  175. if (!parent.AllUICom.ContainsKey(name))
  176. {
  177. List<UICom> uiComs = new List<UICom>();
  178. uiComs.Add(uiCom);
  179. parent.AllUICom.Add(name, uiComs);
  180. }
  181. else
  182. {
  183. parent.AllUICom[name].Add(uiCom);
  184. }
  185. uiCom.DelEvent();
  186. uiCom.Show();
  187. return uiCom;
  188. }
  189. /// <summary>
  190. /// 回收组件
  191. /// </summary>
  192. /// <param name="uiCom"></param>
  193. public void RecUICom(UIBasic parent, UICom uiCom)
  194. {
  195. string name = uiCom.ToString();
  196. if (!AllUIComPool.ContainsKey(name))
  197. {
  198. List<UICom> coms = new List<UICom>();
  199. coms.Add(uiCom);
  200. uiCom.Hide();
  201. AllUIComPool.Add(name, coms);
  202. }
  203. else
  204. {
  205. uiCom.Hide();
  206. AllUIComPool[name].Add(uiCom);
  207. }
  208. if (parent.AllUICom.ContainsKey(name))
  209. {
  210. parent.AllUICom[name].Remove(uiCom);
  211. if (parent.AllUICom[name].Count == 0)
  212. {
  213. parent.AllUICom.Remove(name);
  214. }
  215. }
  216. }
  217. }
  218. }