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. _transform.anchoredPosition = Vector2.zero;
  65. _referenceCollector = gObjectPoolInterface.GetComponent<ReferenceCollector>();
  66. Animator = gObjectPoolInterface.GetComponent<Animator>();
  67. // this.isActiveAnima = isActiveAnima;
  68. if (!_isAddButtonEvent)
  69. {
  70. AddButtonEvent();
  71. _isAddButtonEvent = true;
  72. }
  73. if (Animator != null && Animator.runtimeAnimatorController != null )
  74. {
  75. Animator.updateMode = AnimatorUpdateMode.UnscaledTime;
  76. foreach (var animatorClip in Animator.runtimeAnimatorController.animationClips)
  77. {
  78. if (animatorClip.name.Contains("close") || animatorClip.name.Contains("Close"))
  79. {
  80. _closeAnimationTimeCount = (long)(animatorClip.length * 1000);
  81. }
  82. if (animatorClip.name.Contains("open") || animatorClip.name.Contains("Open"))
  83. {
  84. _openAnimationTimeCount = (long)(animatorClip.length * 1000);
  85. }
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// UI一次打开的时候调用一次,后面就不会再调用
  91. /// </summary>
  92. public virtual void AddButtonEvent()
  93. {
  94. }
  95. public override bool IsUpdate()
  96. {
  97. if (GObjectPoolInterface == null)
  98. {
  99. return false;
  100. }
  101. return GObjectPoolInterface.activeInHierarchy;
  102. }
  103. /// <summary>
  104. /// 获取UI拖动的部件信息
  105. /// </summary>
  106. /// <param name="key"></param>
  107. /// <typeparam name="T"></typeparam>
  108. /// <returns></returns>
  109. public T GetUIUnit<T>(string key) where T : class
  110. {
  111. return _referenceCollector.Get<T>(key);
  112. }
  113. /// <summary>
  114. /// 初始化相关数据(针对内部请求初始化) 可使用异步等待 与自动化Init 区分开
  115. /// 先初始化数据,然后再打开界面,可以避免某些界面先打开后才被初始化
  116. /// </summary>
  117. /// <returns></returns>
  118. public virtual async CTask<bool> AsyncInit(object[] uiData)
  119. {
  120. try
  121. {
  122. CTask<bool> cTask = CTask<bool>.Create();
  123. cTask.SetResult(true);
  124. return await cTask;
  125. }
  126. catch (Exception e)
  127. {
  128. LogTool.Exception(e);
  129. }
  130. return default;
  131. }
  132. /// <summary>
  133. /// 打开界面
  134. /// </summary>
  135. public virtual async CTask Open()
  136. {
  137. await Show();
  138. }
  139. public virtual CTask Show()
  140. {
  141. if (GObjectPoolInterface == null)
  142. {
  143. return null;
  144. }
  145. CTask cTask = CTask.Create();
  146. _isShow = true;
  147. GObjectPoolInterface.SetActive(true);
  148. if (timerEntity != null)
  149. {
  150. TimerComponent.Instance.Remove(timerEntity);
  151. }
  152. if (openTimerEntity != null)
  153. {
  154. TimerComponent.Instance.Remove(openTimerEntity);
  155. }
  156. ShowAnimator();
  157. cTask.SetResult();
  158. return cTask;
  159. }
  160. public virtual void ShowAnimator()
  161. {
  162. showcCTask = CTask.Create(false);
  163. TimerComponent.Instance.Remove(openTimerEntity);
  164. if (Animator != null && isActiveAnima)
  165. {
  166. if (_openAnimationTimeCount > 0)
  167. {
  168. Animator.Play("open", 0, 0.01f);
  169. Animator.Update(0.01f);
  170. openTimerEntity = TimerComponent.Instance.AddTimer(_openAnimationTimeCount, delegate
  171. {
  172. showcCTask.SetResult();
  173. });
  174. }
  175. else
  176. {
  177. showcCTask.SetResult();
  178. }
  179. }
  180. else
  181. {
  182. showcCTask.SetResult();
  183. }
  184. }
  185. protected TimerEntity timerEntity;
  186. protected TimerEntity openTimerEntity;
  187. //等大show动画播放完毕
  188. public async CTask WaitShowAnimOver()
  189. {
  190. await showcCTask;
  191. }
  192. /// <summary>
  193. /// 隐藏界面
  194. /// </summary>
  195. public virtual async void Hide()
  196. {
  197. hideCtask = CTask.Create(false);
  198. _isShow = false;
  199. if (Animator != null && isActiveAnima)
  200. {
  201. Animator.Play("close");
  202. if (timerEntity != null)
  203. {
  204. TimerComponent.Instance.Remove(timerEntity);
  205. }
  206. timerEntity = TimerComponent.Instance.AddTimer(_closeAnimationTimeCount,
  207. () =>
  208. {
  209. if (GObjectPoolInterface != null)
  210. {
  211. GObjectPoolInterface.SetActive(false);
  212. }
  213. hideCtask.SetResult();
  214. });
  215. }
  216. else
  217. {
  218. if (GObjectPoolInterface != null)
  219. {
  220. GObjectPoolInterface.SetActive(false);
  221. }
  222. hideCtask.SetResult();
  223. }
  224. }
  225. public async CTask PlayAnim(string anmiName)
  226. {
  227. CTask cTask = CTask.Create();
  228. long animaTimeCount = 0;
  229. if (Animator != null && Animator.runtimeAnimatorController != null && isActiveAnima == true)
  230. {
  231. Animator.updateMode = AnimatorUpdateMode.UnscaledTime;
  232. foreach (var animatorClip in Animator.runtimeAnimatorController.animationClips)
  233. {
  234. if (animatorClip.name.Contains(anmiName))
  235. {
  236. animaTimeCount = (long)(animatorClip.length * 1000);
  237. }
  238. }
  239. }
  240. Animator.Play(anmiName);
  241. TimerComponent.Instance.AddTimer(animaTimeCount,
  242. () => { cTask?.SetResult(); });
  243. await cTask;
  244. }
  245. public override async void Dispose()
  246. {
  247. if (GObjectPoolInterface != null)
  248. {
  249. OnDestroy();
  250. GameObject.Destroy(GObjectPoolInterface);
  251. GObjectPoolInterface = null;
  252. }
  253. base.Dispose();
  254. }
  255. protected virtual void OnDestroy()
  256. {
  257. }
  258. }
  259. }