UIKit.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /****************************************************************************
  2. * Copyright (c) 2021.8 liangxie
  3. *
  4. * http://qframework.io
  5. * https://github.com/liangxiegame/QFramework
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. ****************************************************************************/
  25. using UnityEngine;
  26. namespace QFramework
  27. {
  28. public class UIKit
  29. {
  30. public static UIKitConfig Config = new UIKitConfig();
  31. public static UIRoot Root
  32. {
  33. get { return Config.Root; }
  34. }
  35. /// <summary>
  36. /// UI 堆栈
  37. /// </summary>
  38. public static readonly UIPanelStack Stack = new UIPanelStack();
  39. /// <summary>
  40. /// UIPanel 管理(数据结构)
  41. /// </summary>
  42. public static readonly UIPanelTable Table = new UIPanelTable();
  43. public static T OpenPanel<T>(PanelOpenType panelOpenType, UILevel canvasLevel = UILevel.Common,
  44. IUIData uiData = null,
  45. string assetBundleName = null,
  46. string prefabName = null) where T : UIPanel
  47. {
  48. var panelSearchKeys = PanelSearchKeys.Allocate();
  49. panelSearchKeys.OpenType = panelOpenType;
  50. panelSearchKeys.Level = canvasLevel;
  51. panelSearchKeys.PanelType = typeof(T);
  52. panelSearchKeys.AssetBundleName = assetBundleName;
  53. panelSearchKeys.GameObjName = prefabName;
  54. panelSearchKeys.UIData = uiData;
  55. T retPanel = UIManager.Instance.OpenUI(panelSearchKeys) as T;
  56. panelSearchKeys.Recycle2Cache();
  57. return retPanel;
  58. }
  59. public static T OpenPanel<T>(UILevel canvasLevel = UILevel.Common, IUIData uiData = null,
  60. string assetBundleName = null,
  61. string prefabName = null) where T : UIPanel
  62. {
  63. var panelSearchKeys = PanelSearchKeys.Allocate();
  64. panelSearchKeys.OpenType = PanelOpenType.Single;
  65. panelSearchKeys.Level = canvasLevel;
  66. panelSearchKeys.PanelType = typeof(T);
  67. panelSearchKeys.AssetBundleName = assetBundleName;
  68. panelSearchKeys.GameObjName = prefabName;
  69. panelSearchKeys.UIData = uiData;
  70. T retPanel = UIManager.Instance.OpenUI(panelSearchKeys) as T;
  71. panelSearchKeys.Recycle2Cache();
  72. return retPanel;
  73. }
  74. public static T OpenPanel<T>(IUIData uiData, PanelOpenType panelOpenType = PanelOpenType.Single,
  75. string assetBundleName = null,
  76. string prefabName = null) where T : UIPanel
  77. {
  78. var panelSearchKeys = PanelSearchKeys.Allocate();
  79. panelSearchKeys.OpenType = panelOpenType;
  80. panelSearchKeys.Level = UILevel.Common;
  81. panelSearchKeys.PanelType = typeof(T);
  82. panelSearchKeys.AssetBundleName = assetBundleName;
  83. panelSearchKeys.GameObjName = prefabName;
  84. panelSearchKeys.UIData = uiData;
  85. T retPanel = UIManager.Instance.OpenUI(panelSearchKeys) as T;
  86. panelSearchKeys.Recycle2Cache();
  87. return retPanel;
  88. }
  89. public static void ClosePanel<T>() where T : UIPanel
  90. {
  91. var panelSearchKeys = PanelSearchKeys.Allocate();
  92. panelSearchKeys.PanelType = typeof(T);
  93. UIManager.Instance.CloseUI(panelSearchKeys);
  94. panelSearchKeys.Recycle2Cache();
  95. }
  96. public static void ShowPanel<T>() where T : UIPanel
  97. {
  98. var panelSearchKeys = PanelSearchKeys.Allocate();
  99. panelSearchKeys.PanelType = typeof(T);
  100. UIManager.Instance.ShowUI(panelSearchKeys);
  101. panelSearchKeys.Recycle2Cache();
  102. }
  103. public static void HidePanel<T>() where T : UIPanel
  104. {
  105. var panelSearchKeys = PanelSearchKeys.Allocate();
  106. panelSearchKeys.PanelType = typeof(T);
  107. UIManager.Instance.HideUI(panelSearchKeys);
  108. panelSearchKeys.Recycle2Cache();
  109. }
  110. public static void CloseAllPanel()
  111. {
  112. UIManager.Instance.CloseAllUI();
  113. }
  114. public static void HideAllPanel()
  115. {
  116. UIManager.Instance.HideAllUI();
  117. }
  118. public static T GetPanel<T>() where T : UIPanel
  119. {
  120. var panelSearchKeys = PanelSearchKeys.Allocate();
  121. panelSearchKeys.PanelType = typeof(T);
  122. var retPanel = UIManager.Instance.GetUI(panelSearchKeys);
  123. panelSearchKeys.Recycle2Cache();
  124. return retPanel as T;
  125. }
  126. #region 给脚本层用的 api
  127. public static UIPanel GetPanel(string panelName)
  128. {
  129. var panelSearchKeys = PanelSearchKeys.Allocate();
  130. panelSearchKeys.GameObjName = panelName;
  131. var retPanel = UIManager.Instance.GetUI(panelSearchKeys);
  132. panelSearchKeys.Recycle2Cache();
  133. return retPanel;
  134. }
  135. public static UIPanel OpenPanel(string panelName, UILevel level = UILevel.Common, string assetBundleName = null)
  136. {
  137. var panelSearchKeys = PanelSearchKeys.Allocate();
  138. panelSearchKeys.Level = level;
  139. panelSearchKeys.AssetBundleName = assetBundleName;
  140. panelSearchKeys.GameObjName = panelName;
  141. var retPanel = UIManager.Instance.OpenUI(panelSearchKeys);
  142. panelSearchKeys.Recycle2Cache();
  143. return retPanel as UIPanel;
  144. }
  145. public static void ClosePanel(string panelName)
  146. {
  147. var panelSearchKeys = PanelSearchKeys.Allocate();
  148. panelSearchKeys.GameObjName = panelName;
  149. UIManager.Instance.CloseUI(panelSearchKeys);
  150. panelSearchKeys.Recycle2Cache();
  151. }
  152. public static void ClosePanel(UIPanel panel)
  153. {
  154. var panelSearchKeys = PanelSearchKeys.Allocate();
  155. panelSearchKeys.Panel = panel;
  156. UIManager.Instance.CloseUI(panelSearchKeys);
  157. panelSearchKeys.Recycle2Cache();
  158. }
  159. public static void ShowPanel(string panelName)
  160. {
  161. var panelSearchKeys = PanelSearchKeys.Allocate();
  162. panelSearchKeys.GameObjName = panelName;
  163. UIManager.Instance.ShowUI(panelSearchKeys);
  164. panelSearchKeys.Recycle2Cache();
  165. }
  166. public static void HidePanel(string panelName)
  167. {
  168. var panelSearchKeys = PanelSearchKeys.Allocate();
  169. panelSearchKeys.GameObjName = panelName;
  170. UIManager.Instance.HideUI(panelSearchKeys);
  171. panelSearchKeys.Recycle2Cache();
  172. }
  173. #endregion
  174. public static void Back(string currentPanelName)
  175. {
  176. if (!string.IsNullOrEmpty(currentPanelName))
  177. {
  178. var panelSearchKeys = PanelSearchKeys.Allocate();
  179. panelSearchKeys.GameObjName = currentPanelName;
  180. UIManager.Instance.CloseUI(panelSearchKeys);
  181. panelSearchKeys.Recycle2Cache();
  182. }
  183. Stack.Pop();
  184. }
  185. public static void Back(UIPanel currentPanel)
  186. {
  187. if (currentPanel != null)
  188. {
  189. var panelSearchKeys = PanelSearchKeys.Allocate();
  190. panelSearchKeys.GameObjName = currentPanel.name;
  191. UIManager.Instance.CloseUI(panelSearchKeys);
  192. panelSearchKeys.Recycle2Cache();
  193. }
  194. Stack.Pop();
  195. }
  196. public static void Back<T>()
  197. {
  198. var panelSearchKeys = PanelSearchKeys.Allocate();
  199. panelSearchKeys.PanelType = typeof(T);
  200. UIManager.Instance.CloseUI(panelSearchKeys);
  201. panelSearchKeys.Recycle2Cache();
  202. Stack.Pop();
  203. }
  204. }
  205. }