UIBase.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System;
  2. using System.Collections.Generic;
  3. using Fort23.Core;
  4. using Fort23.UTool;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace Fort23.Mono
  8. {
  9. /// <summary>
  10. /// 打开一个UI的流程是:
  11. /// 1.生成Entity脚本
  12. /// 2.通过Init方法获取到数据
  13. /// 3.如果数据获取失败了,则界面打开失败,直接调用Entity的Dispose方法销毁;如果数据获取成功则调用Show展示界面。
  14. /// </summary>
  15. public class UIBase : Entity
  16. {
  17. private RectTransform _transform;
  18. public RectTransform transform => _transform;
  19. public GameObject GObjectPoolInterface;
  20. /// <summary>
  21. /// 序列化数据,用于拖动使用
  22. /// </summary>
  23. protected ReferenceCollector _referenceCollector;
  24. protected bool _isShow;
  25. /// <summary>
  26. /// 标记是否打开状态
  27. /// </summary>
  28. public bool isShow
  29. {
  30. get { return _isShow; }
  31. set { _isShow = value; }
  32. }
  33. /// <summary>
  34. /// 是否添加了button事件
  35. /// </summary>
  36. private bool _isAddButtonEvent;
  37. /// <summary>
  38. /// 是否启用动画
  39. /// </summary>
  40. public bool isActiveAnima;
  41. public Animator Animator;
  42. /// <summary>
  43. /// 结束动画的长度
  44. /// </summary>
  45. protected long _closeAnimationTimeCount;
  46. /// <summary>
  47. /// 开始动画的长度
  48. /// </summary>
  49. protected long _openAnimationTimeCount;
  50. /// <summary>
  51. /// 是否展示完成
  52. /// </summary>
  53. public bool IsShowComplete = true;
  54. public CTask hideCtask;
  55. protected CTask showcCTask;
  56. /// <summary>
  57. /// 这个方法可以用来替代Awake,直接重写就可以不注册Awake系统了。
  58. /// </summary>
  59. /// <param name="gObjectPoolInterface"></param>
  60. public virtual async CTask SetUIGameObject(GameObject gObjectPoolInterface)
  61. {
  62. GObjectPoolInterface = gObjectPoolInterface;
  63. _transform = gObjectPoolInterface.GetComponent<RectTransform>();
  64. _referenceCollector = gObjectPoolInterface.GetComponent<ReferenceCollector>();
  65. Animator = gObjectPoolInterface.GetComponent<Animator>();
  66. // this.isActiveAnima = isActiveAnima;
  67. if (!_isAddButtonEvent)
  68. {
  69. AddButtonEvent();
  70. _isAddButtonEvent = true;
  71. }
  72. if (Animator != null && Animator.runtimeAnimatorController != null )
  73. {
  74. Animator.updateMode = AnimatorUpdateMode.UnscaledTime;
  75. foreach (var animatorClip in Animator.runtimeAnimatorController.animationClips)
  76. {
  77. if (animatorClip.name.Contains("close") || animatorClip.name.Contains("Close"))
  78. {
  79. _closeAnimationTimeCount = (long)(animatorClip.length * 1000);
  80. }
  81. if (animatorClip.name.Contains("open") || animatorClip.name.Contains("Open"))
  82. {
  83. _openAnimationTimeCount = (long)(animatorClip.length * 1000);
  84. }
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// UI一次打开的时候调用一次,后面就不会再调用
  90. /// </summary>
  91. public virtual void AddButtonEvent()
  92. {
  93. }
  94. public override bool IsUpdate()
  95. {
  96. if (GObjectPoolInterface == null)
  97. {
  98. return false;
  99. }
  100. return GObjectPoolInterface.activeInHierarchy;
  101. }
  102. /// <summary>
  103. /// 获取UI拖动的部件信息
  104. /// </summary>
  105. /// <param name="key"></param>
  106. /// <typeparam name="T"></typeparam>
  107. /// <returns></returns>
  108. public T GetUIUnit<T>(string key) where T : class
  109. {
  110. return _referenceCollector.Get<T>(key);
  111. }
  112. /// <summary>
  113. /// 初始化相关数据(针对内部请求初始化) 可使用异步等待 与自动化Init 区分开
  114. /// 先初始化数据,然后再打开界面,可以避免某些界面先打开后才被初始化
  115. /// </summary>
  116. /// <returns></returns>
  117. public virtual async CTask<bool> AsyncInit(object[] uiData)
  118. {
  119. try
  120. {
  121. CTask<bool> cTask = CTask<bool>.Create();
  122. cTask.SetResult(true);
  123. return await cTask;
  124. }
  125. catch (Exception e)
  126. {
  127. LogTool.Exception(e);
  128. }
  129. return default;
  130. }
  131. /// <summary>
  132. /// 打开界面
  133. /// </summary>
  134. public virtual async CTask Open()
  135. {
  136. await Show();
  137. }
  138. public virtual CTask Show()
  139. {
  140. if (GObjectPoolInterface == null)
  141. {
  142. return null;
  143. }
  144. CTask cTask = CTask.Create();
  145. _isShow = true;
  146. GObjectPoolInterface.SetActive(true);
  147. if (timerEntity != null)
  148. {
  149. TimerComponent.Instance.Remove(timerEntity);
  150. }
  151. if (openTimerEntity != null)
  152. {
  153. TimerComponent.Instance.Remove(openTimerEntity);
  154. }
  155. ShowAnimator();
  156. cTask.SetResult();
  157. return cTask;
  158. }
  159. public virtual void ShowAnimator()
  160. {
  161. showcCTask = CTask.Create(false);
  162. TimerComponent.Instance.Remove(openTimerEntity);
  163. if (Animator != null && isActiveAnima)
  164. {
  165. if (_openAnimationTimeCount > 0)
  166. {
  167. Animator.Play("open", 0, 0.01f);
  168. Animator.Update(0.01f);
  169. openTimerEntity = TimerComponent.Instance.AddTimer(_openAnimationTimeCount, delegate
  170. {
  171. showcCTask.SetResult();
  172. });
  173. }
  174. else
  175. {
  176. showcCTask.SetResult();
  177. }
  178. }
  179. else
  180. {
  181. showcCTask.SetResult();
  182. }
  183. }
  184. protected TimerEntity timerEntity;
  185. protected TimerEntity openTimerEntity;
  186. //等大show动画播放完毕
  187. public async CTask WaitShowAnimOver()
  188. {
  189. await showcCTask;
  190. }
  191. /// <summary>
  192. /// 隐藏界面
  193. /// </summary>
  194. public virtual async void Hide()
  195. {
  196. hideCtask = CTask.Create(false);
  197. _isShow = false;
  198. if (Animator != null && isActiveAnima)
  199. {
  200. Animator.Play("close");
  201. if (timerEntity != null)
  202. {
  203. TimerComponent.Instance.Remove(timerEntity);
  204. }
  205. timerEntity = TimerComponent.Instance.AddTimer(_closeAnimationTimeCount,
  206. () =>
  207. {
  208. if (GObjectPoolInterface != null)
  209. {
  210. GObjectPoolInterface.SetActive(false);
  211. }
  212. hideCtask.SetResult();
  213. });
  214. }
  215. else
  216. {
  217. if (GObjectPoolInterface != null)
  218. {
  219. GObjectPoolInterface.SetActive(false);
  220. }
  221. hideCtask.SetResult();
  222. }
  223. }
  224. public async CTask PlayAnim(string anmiName)
  225. {
  226. CTask cTask = CTask.Create();
  227. long animaTimeCount = 0;
  228. if (Animator != null && Animator.runtimeAnimatorController != null && isActiveAnima == true)
  229. {
  230. Animator.updateMode = AnimatorUpdateMode.UnscaledTime;
  231. foreach (var animatorClip in Animator.runtimeAnimatorController.animationClips)
  232. {
  233. if (animatorClip.name.Contains(anmiName))
  234. {
  235. animaTimeCount = (long)(animatorClip.length * 1000);
  236. }
  237. }
  238. }
  239. Animator.Play(anmiName);
  240. TimerComponent.Instance.AddTimer(animaTimeCount,
  241. () => { cTask?.SetResult(); });
  242. await cTask;
  243. }
  244. public override async void Dispose()
  245. {
  246. if (GObjectPoolInterface != null)
  247. {
  248. OnDestroy();
  249. GameObject.Destroy(GObjectPoolInterface);
  250. GObjectPoolInterface = null;
  251. }
  252. base.Dispose();
  253. }
  254. protected virtual void OnDestroy()
  255. {
  256. }
  257. }
  258. }