UIPanel.cs 8.8 KB

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