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