UIPanel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /// 是否是弹窗ui
  86. /// </summary>
  87. public bool isPopUi = false;
  88. /// <summary>
  89. /// 面板名字
  90. /// </summary>
  91. public string PanelName;
  92. /// <summary>
  93. /// 是否显示导航栏
  94. /// </summary>
  95. public bool IsShowAppBar = true;
  96. public override async CTask SetUIGameObject(GameObject gObjectPoolInterface)
  97. {
  98. await base.SetUIGameObject(gObjectPoolInterface);
  99. if (isFocus)
  100. {
  101. GObjectPoolInterface.GetComponent<RectTransform>().offsetMax = Vector2.zero;
  102. GObjectPoolInterface.GetComponent<RectTransform>().offsetMin = Vector2.zero;
  103. }
  104. // transform.sizeDelta = UIManager.Instance.UIRootTransform.sizeDelta;
  105. transform.sizeDelta = UIManager.Instance.sizeDelta;
  106. }
  107. /// <summary>
  108. /// 1.打开界面 只有通过UIManager API 打开 并触发
  109. /// </summary>
  110. public override async CTask Open()
  111. {
  112. await ProOpen();
  113. await base.Open();
  114. DelEvent();
  115. IsClose = false;
  116. AddEvent();
  117. }
  118. protected virtual async CTask ProOpen()
  119. {
  120. }
  121. protected override void OnDestroy()
  122. {
  123. DelEvent();
  124. }
  125. public override void ShowAnimator()
  126. {
  127. showcCTask = CTask.Create();
  128. TimerComponent.Instance.Remove(openTimerEntity);
  129. if (Animator != null && isActiveAnima)
  130. {
  131. if (_openAnimationTimeCount > 0)
  132. {
  133. UIManager.Instance.SetEventSystemEnable(false);
  134. Animator.Play("open");
  135. Animator.Update(0.01f);
  136. openTimerEntity = TimerComponent.Instance.AddTimer(_openAnimationTimeCount, delegate
  137. {
  138. UIManager.Instance.SetEventSystemEnable(true);
  139. showcCTask.SetResult();
  140. });
  141. }
  142. else
  143. {
  144. showcCTask.SetResult();
  145. }
  146. }
  147. else
  148. {
  149. showcCTask.SetResult();
  150. }
  151. }
  152. /// <summary>
  153. /// 展示这个界面 只做了显示和焦点获取 每次显示和隐藏会调用
  154. /// </summary>
  155. public override async CTask Show()
  156. {
  157. base.Show();
  158. DelEvent();
  159. IsClose = false;
  160. AddEvent();
  161. GObjectPoolInterface.transform.SetAsLastSibling();
  162. await GetFocus();
  163. }
  164. /// <summary>
  165. /// 添加EventManager事件(不在处理gameObject相关的操作,不然有会出现对象丢失的情况)
  166. /// </summary>
  167. protected abstract void AddEvent();
  168. /// <summary>
  169. /// 添加EventManager事件(不在处理gameObject相关的操作,不然有会出现对象丢失的情况)
  170. /// </summary>
  171. protected abstract void DelEvent();
  172. /// <summary>
  173. /// 点击背景面板时的回调
  174. /// </summary>
  175. public virtual void BackgroundCallBack()
  176. {
  177. }
  178. public virtual bool IsHindBackground()
  179. {
  180. return true;
  181. }
  182. public CTask CloseAwaitTask;
  183. /// <summary>
  184. /// 关闭界面
  185. /// </summary>
  186. public virtual async void Close()
  187. {
  188. if (IsClose)
  189. {
  190. return;
  191. }
  192. _closedCallback?.Invoke();
  193. _closedCallback = null;
  194. Hide();
  195. IsClose = true;
  196. GObjectPoolInterface?.transform.SetSiblingIndex(0); //关闭界面的时候将他放在底部
  197. // if (IsShowCustomBGPanel)
  198. // UIManager.Instance.SetBackgroundPanel(this);
  199. if (this == UIManager.Instance.currOpenPanel)
  200. {
  201. UIManager.Instance.currOpenPanel = null; //如果关闭的界面是最TOP 就把最Top设置为null
  202. }
  203. _closedTask?.SetResult();
  204. _closedTask = null;
  205. if (isPopUi)
  206. {
  207. if (UIManager.Instance.popUIPanels.Contains(this))
  208. UIManager.Instance.popUIPanels.Remove(this);
  209. }
  210. }
  211. /// <summary>
  212. /// 隐藏界面
  213. /// </summary>
  214. public override async void Hide()
  215. {
  216. base.Hide();
  217. DelEvent();
  218. await LoseFocus(); //关闭界面默认失去焦点
  219. }
  220. private CTask _closedTask;
  221. private Action _closedCallback;
  222. /// <summary>
  223. /// 增加的一个可等待的UI关闭任务,可以等待一个界面完全关闭后处理后面的事情
  224. /// </summary>
  225. /// <returns></returns>
  226. public async CTask UIClosed(Action action = null)
  227. {
  228. _closedTask = CTask.Create(false);
  229. _closedCallback += action;
  230. await _closedTask;
  231. }
  232. /// <summary>
  233. /// 获得焦点(当前显示再最 上层)
  234. /// </summary>
  235. public virtual async CTask GetFocus()
  236. {
  237. if (isAddStack)
  238. {
  239. if (!IsShowAppBar)
  240. {
  241. Assembly assembly = Assembly.Load("Fort23.Mono");
  242. Type AppBarPanel = assembly.GetType(" Fort23.Mono.AppBarPanel");
  243. UIPanel uiPanel = UIManager.Instance.GetComponent(AppBarPanel) as UIPanel;
  244. if (uiPanel != null)
  245. {
  246. uiPanel.Hide();
  247. }
  248. }
  249. UIManager.Instance.currOpenPanel = this;
  250. }
  251. if (isPopUi)
  252. {
  253. if (!UIManager.Instance.popUIPanels.Contains(this))
  254. UIManager.Instance.popUIPanels.Add(this);
  255. }
  256. }
  257. /// <summary>
  258. /// 失去焦点(被其他UI所遮挡)
  259. /// </summary>
  260. public virtual async CTask LoseFocus()
  261. {
  262. }
  263. public void AddDelayDestroy()
  264. {
  265. RemoveDestroyTimerEntity();
  266. DestroyTimerEntity = TimerComponent.Instance.AddTimer(DelayDestroyTime, delegate
  267. {
  268. DestroyTimerEntity = null;
  269. UIManager.Instance.DestroyUIPanel(this);
  270. });
  271. }
  272. public void RemoveDestroyTimerEntity()
  273. {
  274. if (DestroyTimerEntity != null)
  275. {
  276. TimerComponent.Instance.Remove(DestroyTimerEntity.ID);
  277. }
  278. }
  279. // public virtual void GetBack()
  280. // {
  281. // UIPanel uiPanel = UIManager.Instance.CloseTopUI();
  282. //
  283. // UIManager.Instance.OpenTopUI();
  284. // if (UIManager.Instance.GetComponent<AppBarPanel>() != null)
  285. // {
  286. // UIManager.Instance.GetComponent<AppBarPanel>().UpdateAssociatedUIPanelChangeData(this);
  287. // }
  288. // }
  289. }
  290. }