Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / UiManager.cs
@小李xl 小李xl on 20 Aug 2023 7 KB 创建房间标记, 开发中
  1. using System;
  2. using System.Collections.Generic;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// ui管理类
  7. /// </summary>
  8. public static partial class UiManager
  9. {
  10. public enum RecordType
  11. {
  12. Open,
  13. Close,
  14. }
  15. private static bool _init = false;
  16.  
  17. private static CanvasLayer _bottomLayer;
  18. private static CanvasLayer _middleLayer;
  19. private static CanvasLayer _heightLayer;
  20. private static CanvasLayer _popLayer;
  21.  
  22. private static Dictionary<string, List<UiBase>> _recordUiMap = new Dictionary<string, List<UiBase>>();
  23.  
  24. /// <summary>
  25. /// 初始化Ui管理器
  26. /// </summary>
  27. public static void Init()
  28. {
  29. if (_init)
  30. {
  31. return;
  32. }
  33.  
  34. _init = true;
  35. //创建ui层
  36. //Bottom
  37. _bottomLayer = new CanvasLayer();
  38. _bottomLayer.Name = "BottomLayer";
  39. _bottomLayer.Layer = 5;
  40. GameApplication.Instance.AddChild(_bottomLayer);
  41. //Middle
  42. _middleLayer = new CanvasLayer();
  43. _middleLayer.Name = "MiddleLayer";
  44. _middleLayer.Layer = 15;
  45. GameApplication.Instance.AddChild(_middleLayer);
  46. //Height
  47. _heightLayer = new CanvasLayer();
  48. _heightLayer.Name = "HeightLayer";
  49. _heightLayer.Layer = 25;
  50. GameApplication.Instance.AddChild(_heightLayer);
  51. //Pop
  52. _popLayer = new CanvasLayer();
  53. _popLayer.Name = "PopLayer";
  54. _popLayer.Layer = 35;
  55. GameApplication.Instance.AddChild(_popLayer);
  56. }
  57.  
  58. /// <summary>
  59. /// 获取指定的Ui层根节点
  60. /// </summary>
  61. public static CanvasLayer GetUiLayer(UiLayer uiLayer)
  62. {
  63. switch (uiLayer)
  64. {
  65. case UiLayer.Bottom:
  66. return _bottomLayer;
  67. case UiLayer.Middle:
  68. return _middleLayer;
  69. case UiLayer.Height:
  70. return _heightLayer;
  71. case UiLayer.Pop:
  72. return _popLayer;
  73. }
  74.  
  75. return null;
  76. }
  77.  
  78. /// <summary>
  79. /// 记录ui的创建或者销毁, 子 ui 通过 UiBase.RecordNestedUi() 来记录
  80. /// </summary>
  81. public static void RecordUi(UiBase uiBase, RecordType type)
  82. {
  83. if (type == RecordType.Open)
  84. {
  85. if (_recordUiMap.TryGetValue(uiBase.UiName, out var list))
  86. {
  87. list.Add(uiBase);
  88. }
  89. else
  90. {
  91. list = new List<UiBase>();
  92. list.Add(uiBase);
  93. _recordUiMap.Add(uiBase.UiName, list);
  94. }
  95. }
  96. else
  97. {
  98. if (_recordUiMap.TryGetValue(uiBase.UiName, out var list))
  99. {
  100. list.Remove(uiBase);
  101. if (list.Count == 0)
  102. {
  103. _recordUiMap.Remove(uiBase.UiName);
  104. }
  105. }
  106. }
  107. }
  108.  
  109. /// <summary>
  110. /// 根据Ui资源路径创建Ui, 并返回Ui实例, 该Ui资源的场景根节点必须继承<see cref="UiBase"/><br/>
  111. /// 该函数不会自动打开Ui, 需要手动调用 ShowUi() 函数来显示Ui
  112. /// </summary>
  113. /// <param name="uiName">Ui名称</param>
  114. /// <param name="prevUi">上一级Ui, 用于UIBase.OpenPrevUi()函数返回上一级Ui</param>
  115. public static UiBase CreateUi(string uiName, UiBase prevUi = null)
  116. {
  117. if (!_init)
  118. {
  119. throw new Exception("未初始化 UiManager!, 请先调用 UiManager.Init() 函数!");
  120. }
  121. var packedScene = ResourceLoader.Load<PackedScene>("res://" + GameConfig.UiPrefabDir + uiName + ".tscn");
  122. var uiBase = packedScene.Instantiate<UiBase>();
  123. uiBase.Visible = false;
  124. uiBase.PrevUi = prevUi;
  125. var canvasLayer = GetUiLayer(uiBase.Layer);
  126. canvasLayer.AddChild(uiBase);
  127. uiBase.OnCreateUi();
  128. uiBase.OnInitNestedUi();
  129. return uiBase;
  130. }
  131.  
  132. /// <summary>
  133. /// 根据Ui资源路径创建Ui, 并返回Ui实例, 该Ui资源的场景根节点必须继承<see cref="UiBase"/><br/>
  134. /// 该函数不会自动打开Ui, 需要手动调用 ShowUi() 函数来显示Ui
  135. /// </summary>
  136. /// <param name="uiName">Ui名称</param>
  137. /// <param name="prevUi">上一级Ui, 用于UIBase.OpenPrevUi()函数返回上一级Ui</param>
  138. public static T CreateUi<T>(string uiName, UiBase prevUi = null) where T : UiBase
  139. {
  140. return (T)CreateUi(uiName, prevUi);
  141. }
  142.  
  143. /// <summary>
  144. /// 根据Ui资源路径打开Ui, 并返回Ui实例, 该Ui资源的场景根节点必须继承<see cref="UiBase"/>
  145. /// </summary>
  146. /// <param name="uiName">Ui名称</param>
  147. /// <param name="prevUi">上一级Ui, 用于UIBase.OpenPrevUi()函数返回上一级Ui</param>
  148. public static UiBase OpenUi(string uiName, UiBase prevUi = null)
  149. {
  150. var uiBase = CreateUi(uiName, prevUi);
  151. uiBase.ShowUi();
  152. return uiBase;
  153. }
  154.  
  155. /// <summary>
  156. /// 根据Ui资源路径打开Ui, 并返回Ui实例, 该Ui资源的场景根节点必须继承<see cref="UiBase"/>
  157. /// </summary>
  158. /// <param name="uiName">Ui名称</param>
  159. /// <param name="prevUi">上一级Ui, 用于UIBase.OpenPrevUi()函数返回上一级Ui</param>
  160. public static T OpenUi<T>(string uiName, UiBase prevUi = null) where T : UiBase
  161. {
  162. return (T)OpenUi(uiName, prevUi);
  163. }
  164.  
  165.  
  166. /// <summary>
  167. /// 销毁指定Ui
  168. /// </summary>
  169. public static void DestroyUi(UiBase uiBase)
  170. {
  171. uiBase.Destroy();
  172. }
  173.  
  174. /// <summary>
  175. ///
  176. /// </summary>
  177. public static void HideUi(UiBase uiBase)
  178. {
  179. uiBase.HideUi();
  180. }
  181. /// <summary>
  182. /// 销毁所有Ui
  183. /// </summary>
  184. public static void DestroyAllUi()
  185. {
  186. var map = new Dictionary<string, List<UiBase>>();
  187. foreach (var item in _recordUiMap)
  188. {
  189. map.Add(item.Key, new List<UiBase>(item.Value));
  190. }
  191. foreach (var item in map)
  192. {
  193. foreach (var uiBase in item.Value)
  194. {
  195. uiBase.Destroy();
  196. }
  197. }
  198. }
  199.  
  200. /// <summary>
  201. /// 隐藏所有Ui
  202. /// </summary>
  203. public static void HideAllUi()
  204. {
  205. var map = new Dictionary<string, List<UiBase>>();
  206. foreach (var item in _recordUiMap)
  207. {
  208. map.Add(item.Key, new List<UiBase>(item.Value));
  209. }
  210. foreach (var item in map)
  211. {
  212. foreach (var uiBase in item.Value)
  213. {
  214. uiBase.HideUi();
  215. }
  216. }
  217. }
  218.  
  219. /// <summary>
  220. /// 获取Ui实例
  221. /// </summary>
  222. public static T[] GetUiInstance<T>(string uiName) where T : UiBase
  223. {
  224. if (_recordUiMap.TryGetValue(uiName, out var list))
  225. {
  226. var result = new T[list.Count];
  227. for (var i = 0; i < list.Count; i++)
  228. {
  229. result[i] = (T)list[i];
  230. }
  231. return result;
  232. }
  233.  
  234. return new T[0];
  235. }
  236.  
  237. /// <summary>
  238. /// 获取Ui实例数量
  239. /// </summary>
  240. public static int GetUiInstanceCount(string uiName)
  241. {
  242. if (_recordUiMap.TryGetValue(uiName, out var list))
  243. {
  244. return list.Count;
  245. }
  246.  
  247. return 0;
  248. }
  249. }