UIPanel.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using System.Reflection;
  3. using Fort23.Core;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Fort23.Mono
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. public abstract class UIPanel : UIBase
  12. {
  13. public bool IsClose;
  14. public string uiName;
  15. public TimerEntity DestroyTimerEntity;
  16. public int DelayDestroyTime = 5000;
  17. public Graphic minDepth
  18. {
  19. get
  20. {
  21. if (GObjectPoolInterface == null)
  22. {
  23. return null;
  24. }
  25. if (_graphic == null)
  26. {
  27. _graphic = GetGraphic(GObjectPoolInterface.transform);
  28. // int count = GObjectPoolInterface.transform.childCount;
  29. // for (int i = 0; i < count; i++)
  30. // {
  31. // Transform t = GObjectPoolInterface.transform.GetChild(i);
  32. // if (!t.gameObject.activeSelf)
  33. // {
  34. // continue;
  35. // }
  36. //
  37. // _graphic = t.GetComponent<Graphic>();
  38. // break;
  39. // }
  40. }
  41. if (_graphic != null)
  42. {
  43. return _graphic;
  44. }
  45. return null;
  46. }
  47. }
  48. private Graphic GetGraphic(Transform transform)
  49. {
  50. int count = transform.childCount;
  51. for (int i = 0; i < count; i++)
  52. {
  53. Transform t = transform.GetChild(i);
  54. if (!t.gameObject.activeSelf)
  55. {
  56. continue;
  57. }
  58. _graphic = t.GetComponent<Graphic>();
  59. if (_graphic != null)
  60. {
  61. return _graphic;
  62. }
  63. else if (t.childCount > 0)
  64. {
  65. return GetGraphic(t);
  66. }
  67. break;
  68. }
  69. return null;
  70. }
  71. protected Graphic _graphic;
  72. /// <summary>
  73. /// 是否需要显示背景
  74. /// </summary>
  75. public bool IsShowCustomBGPanel = false;
  76. public bool isFocus;
  77. public bool isFullUI;
  78. /// <summary>
  79. /// 是否显示导航栏 当他为false 就可以被close 否则进入只能被hide
  80. /// </summary>
  81. public bool IsBreadcrumbBarPanel = false;
  82. //是否加入堆栈
  83. public bool isAddStack = false;
  84. /// <summary>
  85. /// 面板名字
  86. /// </summary>
  87. public string PanelName;
  88. /// <summary>
  89. /// 是否显示导航栏
  90. /// </summary>
  91. public bool IsShowAppBar = true;
  92. public override async CTask SetUIGameObject(GameObject gObjectPoolInterface)
  93. {
  94. await base.SetUIGameObject(gObjectPoolInterface);
  95. if (isFocus)
  96. {
  97. GObjectPoolInterface.GetComponent<RectTransform>().offsetMax = Vector2.zero;
  98. GObjectPoolInterface.GetComponent<RectTransform>().offsetMin = Vector2.zero;
  99. }
  100. // transform.sizeDelta = UIManager.Instance.UIRootTransform.sizeDelta;
  101. transform.sizeDelta = UIManager.Instance.sizeDelta;
  102. }
  103. /// <summary>
  104. /// 1.打开界面 只有通过UIManager API 打开 并触发
  105. /// </summary>
  106. public override async CTask Open()
  107. {
  108. await ProOpen();
  109. await base.Open();
  110. DelEvent();
  111. IsClose = false;
  112. AddEvent();
  113. }
  114. protected virtual async CTask ProOpen()
  115. {
  116. }
  117. protected override void OnDestroy()
  118. {
  119. DelEvent();
  120. }
  121. public override void ShowAnimator()
  122. {
  123. showcCTask = CTask.Create();
  124. TimerComponent.Instance.Remove(openTimerEntity);
  125. if (Animator != null && isActiveAnima)
  126. {
  127. if (_openAnimationTimeCount > 0)
  128. {
  129. UIManager.Instance.SetEventSystemEnable(false);
  130. Animator.Play("open");
  131. Animator.Update(0.01f);
  132. openTimerEntity = TimerComponent.Instance.AddTimer(_openAnimationTimeCount, delegate
  133. {
  134. UIManager.Instance.SetEventSystemEnable(true);
  135. showcCTask.SetResult();
  136. });
  137. }
  138. else
  139. {
  140. showcCTask.SetResult();
  141. }
  142. }
  143. else
  144. {
  145. showcCTask.SetResult();
  146. }
  147. }
  148. /// <summary>
  149. /// 展示这个界面 只做了显示和焦点获取 每次显示和隐藏会调用
  150. /// </summary>
  151. public override async CTask Show()
  152. {
  153. base.Show();
  154. DelEvent();
  155. IsClose = false;
  156. AddEvent();
  157. GObjectPoolInterface.transform.SetAsLastSibling();
  158. await GetFocus();
  159. }
  160. /// <summary>
  161. /// 添加EventManager事件(不在处理gameObject相关的操作,不然有会出现对象丢失的情况)
  162. /// </summary>
  163. protected abstract void AddEvent();
  164. /// <summary>
  165. /// 添加EventManager事件(不在处理gameObject相关的操作,不然有会出现对象丢失的情况)
  166. /// </summary>
  167. protected abstract void DelEvent();
  168. /// <summary>
  169. /// 点击背景面板时的回调
  170. /// </summary>
  171. public virtual void BackgroundCallBack()
  172. {
  173. }
  174. public virtual bool IsHindBackground()
  175. {
  176. return true;
  177. }
  178. //public CTask CloseAwaitTask;
  179. /// <summary>
  180. /// 关闭界面
  181. /// </summary>
  182. public virtual async void Close()
  183. {
  184. if (IsClose)
  185. {
  186. return;
  187. }
  188. _closedCallback?.Invoke();
  189. _closedCallback = null;
  190. Hide();
  191. IsClose = true;
  192. GObjectPoolInterface?.transform.SetSiblingIndex(0); //关闭界面的时候将他放在底部
  193. // if (IsShowCustomBGPanel)
  194. // UIManager.Instance.SetBackgroundPanel(this);
  195. if (this == UIManager.Instance.currOpenPanel)
  196. {
  197. UIManager.Instance.currOpenPanel = null; //如果关闭的界面是最TOP 就把最Top设置为null
  198. }
  199. _closedTask?.SetResult();
  200. _closedTask = null;
  201. }
  202. /// <summary>
  203. /// 隐藏界面
  204. /// </summary>
  205. public override async void Hide()
  206. {
  207. base.Hide();
  208. DelEvent();
  209. await LoseFocus(); //关闭界面默认失去焦点
  210. }
  211. private CTask _closedTask;
  212. private Action _closedCallback;
  213. /// <summary>
  214. /// 增加的一个可等待的UI关闭任务,可以等待一个界面完全关闭后处理后面的事情
  215. /// </summary>
  216. /// <returns></returns>
  217. public async CTask UIClosed(Action action = null)
  218. {
  219. _closedTask = CTask.Create(false);
  220. _closedCallback += action;
  221. await _closedTask;
  222. }
  223. /// <summary>
  224. /// 获得焦点(当前显示再最 上层)
  225. /// </summary>
  226. public virtual async CTask GetFocus()
  227. {
  228. if (isFocus)
  229. {
  230. if (!IsShowAppBar)
  231. {
  232. Assembly assembly = Assembly.Load("Fort23.Mono");
  233. Type AppBarPanel = assembly.GetType(" Fort23.Mono.AppBarPanel");
  234. UIPanel uiPanel = UIManager.Instance.GetComponent(AppBarPanel) as UIPanel;
  235. if (uiPanel != null)
  236. {
  237. uiPanel.Hide();
  238. }
  239. }
  240. UIManager.Instance.currOpenPanel = this;
  241. }
  242. }
  243. /// <summary>
  244. /// 失去焦点(被其他UI所遮挡)
  245. /// </summary>
  246. public virtual async CTask LoseFocus()
  247. {
  248. }
  249. public void AddDelayDestroy()
  250. {
  251. RemoveDestroyTimerEntity();
  252. DestroyTimerEntity = TimerComponent.Instance.AddTimer(DelayDestroyTime, delegate
  253. {
  254. DestroyTimerEntity = null;
  255. UIManager.Instance.DestroyUIPanel(this);
  256. });
  257. }
  258. public void RemoveDestroyTimerEntity()
  259. {
  260. if (DestroyTimerEntity != null)
  261. {
  262. TimerComponent.Instance.Remove(DestroyTimerEntity.ID);
  263. }
  264. }
  265. // public virtual void GetBack()
  266. // {
  267. // UIPanel uiPanel = UIManager.Instance.CloseTopUI();
  268. //
  269. // UIManager.Instance.OpenTopUI();
  270. // if (UIManager.Instance.GetComponent<AppBarPanel>() != null)
  271. // {
  272. // UIManager.Instance.GetComponent<AppBarPanel>().UpdateAssociatedUIPanelChangeData(this);
  273. // }
  274. // }
  275. }
  276. }