UIManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /****************************************************************************
  2. * Copyright (c) 2017 xiaojun
  3. * Copyright (c) 2017 imagicbell
  4. * Copyright (c) 2017 ~ 2021.3 liangxie
  5. *
  6. * http://qframework.io
  7. * https://github.com/liangxiegame/QFramework
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. ****************************************************************************/
  27. using System.Linq;
  28. using UnityEngine;
  29. namespace QFramework
  30. {
  31. [MonoSingletonPath("UIRoot/Manager")]
  32. public partial class UIManager : QMgrBehaviour, ISingleton
  33. {
  34. void ISingleton.OnSingletonInit()
  35. {
  36. }
  37. private static UIManager mInstance;
  38. public static UIManager Instance
  39. {
  40. get
  41. {
  42. if (!mInstance)
  43. {
  44. var uiRoot = UIRoot.Instance;
  45. Debug.Log("currentUIRoot:" + uiRoot);
  46. mInstance = MonoSingletonProperty<UIManager>.Instance;
  47. }
  48. return mInstance;
  49. }
  50. }
  51. public IPanel OpenUI(PanelSearchKeys panelSearchKeys)
  52. {
  53. if (panelSearchKeys.OpenType == PanelOpenType.Single)
  54. {
  55. var retPanel = UIKit.Table.GetPanelsByPanelSearchKeys(panelSearchKeys).FirstOrDefault();
  56. if (retPanel == null)
  57. {
  58. retPanel = CreateUI(panelSearchKeys);
  59. }
  60. retPanel.Open(panelSearchKeys.UIData);
  61. retPanel.Show();
  62. return retPanel;
  63. }
  64. else
  65. {
  66. var retPanel = CreateUI(panelSearchKeys);
  67. retPanel.Open(panelSearchKeys.UIData);
  68. retPanel.Show();
  69. return retPanel;
  70. }
  71. }
  72. /// <summary>
  73. /// 显示UIBehaiviour
  74. /// </summary>
  75. /// <param name="uiBehaviourName"></param>
  76. public void ShowUI(PanelSearchKeys panelSearchKeys)
  77. {
  78. var retPanel = UIKit.Table.GetPanelsByPanelSearchKeys(panelSearchKeys).FirstOrDefault();
  79. if (retPanel != null)
  80. {
  81. retPanel.Show();
  82. }
  83. }
  84. /// <summary>
  85. /// 隐藏UI
  86. /// </summary>
  87. /// <param name="uiBehaviourName"></param>
  88. public void HideUI(PanelSearchKeys panelSearchKeys)
  89. {
  90. var retPanel = UIKit.Table.GetPanelsByPanelSearchKeys(panelSearchKeys).FirstOrDefault();
  91. if (retPanel != null)
  92. {
  93. retPanel.Hide();
  94. }
  95. }
  96. /// <summary>
  97. /// 删除所有UI层
  98. /// </summary>
  99. public void CloseAllUI()
  100. {
  101. foreach (var layer in UIKit.Table)
  102. {
  103. layer.Close();
  104. layer.Info.Recycle2Cache();
  105. layer.Info = null;
  106. }
  107. UIKit.Table.Clear();
  108. }
  109. /// <summary>
  110. /// 隐藏所有 UI
  111. /// </summary>
  112. public void HideAllUI()
  113. {
  114. foreach (var panel in UIKit.Table)
  115. {
  116. panel.Hide();
  117. }
  118. }
  119. /// <summary>
  120. /// 关闭并卸载UI
  121. /// </summary>
  122. /// <param name="behaviourName"></param>
  123. public void CloseUI(PanelSearchKeys panelSearchKeys)
  124. {
  125. var panel = UIKit.Table.GetPanelsByPanelSearchKeys(panelSearchKeys).LastOrDefault();
  126. if (panel as UIPanel)
  127. {
  128. panel.Close();
  129. UIKit.Table.Remove(panel);
  130. panel.Info.Recycle2Cache();
  131. panel.Info = null;
  132. }
  133. }
  134. public void RemoveUI(PanelSearchKeys panelSearchKeys)
  135. {
  136. var panel = UIKit.Table.GetPanelsByPanelSearchKeys(panelSearchKeys).FirstOrDefault();
  137. if (panel != null)
  138. {
  139. UIKit.Table.Remove(panel);
  140. }
  141. }
  142. /// <summary>
  143. /// 获取UIBehaviour
  144. /// </summary>
  145. /// <param name="uiBehaviourName"></param>
  146. /// <returns></returns>
  147. public UIPanel GetUI(PanelSearchKeys panelSearchKeys)
  148. {
  149. return UIKit.Table.GetPanelsByPanelSearchKeys(panelSearchKeys).FirstOrDefault() as UIPanel;
  150. }
  151. public override int ManagerId
  152. {
  153. get { return QMgrID.UI; }
  154. }
  155. public IPanel CreateUI(PanelSearchKeys panelSearchKeys)
  156. {
  157. var panel = UIKit.Config.LoadPanel(panelSearchKeys);
  158. UIKit.Root.SetLevelOfPanel(panelSearchKeys.Level, panel);
  159. UIKit.Config.SetDefaultSizeOfPanel(panel);
  160. panel.Transform.gameObject.name = panelSearchKeys.GameObjName ?? panelSearchKeys.PanelType.Name;
  161. panel.Info = PanelInfo.Allocate(panelSearchKeys.GameObjName, panelSearchKeys.Level, panelSearchKeys.UIData,
  162. panelSearchKeys.PanelType, panelSearchKeys.AssetBundleName);
  163. UIKit.Table.Add(panel);
  164. panel.Init(panelSearchKeys.UIData);
  165. return panel;
  166. }
  167. }
  168. }