UITweenController.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Collections.Generic;
  3. using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
  4. using UnityEngine;
  5. using Utility;
  6. using Object = UnityEngine.Object;
  7. namespace Core.UI.UTool.UITween
  8. {
  9. [ExecuteAlways]
  10. public class UITweenController : MonoBehaviour
  11. {
  12. public float maxDuration;
  13. public bool isUpdate;
  14. [HideInInspector] public float currTime;
  15. public List<TweenAssetGrpupInfo> GrpupInfos = new List<TweenAssetGrpupInfo>();
  16. private BetterList<TweenAssetGrpupInfo> _currPlay = new BetterList<TweenAssetGrpupInfo>();
  17. public ParticleSystem.MinMaxGradient MinMaxGradient = new ParticleSystem.MinMaxGradient();
  18. public bool isLoop;
  19. private List<UITweenController> _uiTweenControllers = new List<UITweenController>();
  20. private Map<TweenBasic, TweenCacheDataBasic> _currPayCacheData = new Map<TweenBasic, TweenCacheDataBasic>();
  21. private void OnEnable()
  22. {
  23. if (Application.isPlaying)
  24. {
  25. StartPlay(false);
  26. }
  27. }
  28. void OnDrawGizmos()
  29. {
  30. #if UNITY_EDITOR
  31. if (!Application.isPlaying)
  32. {
  33. UnityEditor.EditorApplication.QueuePlayerLoopUpdate();
  34. UnityEditor.SceneView.RepaintAll();
  35. }
  36. #endif
  37. }
  38. public T GetCacheData<T>(TweenBasic tweenBasic) where T : TweenCacheDataBasic, new()
  39. {
  40. if (_currPayCacheData.TryGetValue(tweenBasic, out TweenCacheDataBasic tweenPlayFxCacheData))
  41. {
  42. return tweenPlayFxCacheData as T;
  43. }
  44. T t = new T();
  45. _currPayCacheData.Add(tweenBasic, t as TweenCacheDataBasic);
  46. return t;
  47. }
  48. public void Play(string animName, bool isChildren)
  49. {
  50. _uiTweenControllers.Clear();
  51. maxDuration = 0;
  52. _currPlay.Clear();
  53. isLoop = false;
  54. for (int i = 0; i < GrpupInfos.Count; i++)
  55. {
  56. TweenAssetGrpupInfo tweenAssetGrpupInfo = GrpupInfos[i];
  57. tweenAssetGrpupInfo.SetMaxTime();
  58. if (tweenAssetGrpupInfo.animName.Equals(animName))
  59. {
  60. if (tweenAssetGrpupInfo.maxDuration > maxDuration)
  61. {
  62. maxDuration = tweenAssetGrpupInfo.maxDuration;
  63. }
  64. if (tweenAssetGrpupInfo.isLoop)
  65. {
  66. isLoop = true;
  67. }
  68. tweenAssetGrpupInfo.StartPlay(this);
  69. _currPlay.Add(tweenAssetGrpupInfo);
  70. }
  71. }
  72. if (isChildren)
  73. {
  74. UITweenController[] uiTweenControllers =
  75. gameObject.transform.GetComponentsInChildren<UITweenController>();
  76. _uiTweenControllers.AddRange(uiTweenControllers);
  77. _uiTweenControllers.Remove(this);
  78. for (int i = 0; i < _uiTweenControllers.Count; i++)
  79. {
  80. UITweenController uiTweenController = _uiTweenControllers[i];
  81. // if(uiTweenController.)
  82. uiTweenController.StartPlay(false);
  83. if (uiTweenController.maxDuration > maxDuration)
  84. {
  85. maxDuration = uiTweenController.maxDuration;
  86. }
  87. if (uiTweenController.isLoop)
  88. {
  89. isLoop = true;
  90. }
  91. }
  92. }
  93. isUpdate = true;
  94. currTime = 0;
  95. }
  96. public void Stop()
  97. {
  98. isUpdate = false;
  99. }
  100. public void StartPlay(bool isChildren)
  101. {
  102. _uiTweenControllers.Clear();
  103. maxDuration = 0;
  104. _currPlay.Clear();
  105. isLoop = false;
  106. for (int i = 0; i < GrpupInfos.Count; i++)
  107. {
  108. TweenAssetGrpupInfo tweenAssetGrpupInfo = GrpupInfos[i];
  109. tweenAssetGrpupInfo.SetMaxTime();
  110. if (tweenAssetGrpupInfo.isActive)
  111. {
  112. if (tweenAssetGrpupInfo.maxDuration > maxDuration)
  113. {
  114. maxDuration = tweenAssetGrpupInfo.maxDuration;
  115. }
  116. if (tweenAssetGrpupInfo.isLoop)
  117. {
  118. isLoop = true;
  119. }
  120. tweenAssetGrpupInfo.StartPlay(this);
  121. _currPlay.Add(tweenAssetGrpupInfo);
  122. }
  123. }
  124. if (isChildren)
  125. {
  126. UITweenController[] uiTweenControllers =
  127. gameObject.transform.GetComponentsInChildren<UITweenController>();
  128. _uiTweenControllers.AddRange(uiTweenControllers);
  129. _uiTweenControllers.Remove(this);
  130. for (int i = 0; i < _uiTweenControllers.Count; i++)
  131. {
  132. UITweenController uiTweenController = _uiTweenControllers[i];
  133. uiTweenController.StartPlay(false);
  134. if (uiTweenController.maxDuration > maxDuration)
  135. {
  136. maxDuration = uiTweenController.maxDuration;
  137. }
  138. if (uiTweenController.isLoop)
  139. {
  140. isLoop = true;
  141. }
  142. }
  143. }
  144. isUpdate = true;
  145. currTime = 0;
  146. }
  147. public void SetMaxTime()
  148. {
  149. maxDuration = 0;
  150. for (int i = 0; i < GrpupInfos.Count; i++)
  151. {
  152. TweenAssetGrpupInfo tweenAssetGrpupInfo = GrpupInfos[i];
  153. tweenAssetGrpupInfo.SetMaxTime();
  154. if (tweenAssetGrpupInfo.isActive)
  155. {
  156. if (tweenAssetGrpupInfo.maxDuration > maxDuration)
  157. {
  158. maxDuration = tweenAssetGrpupInfo.maxDuration;
  159. }
  160. }
  161. }
  162. for (int i = 0; i < _uiTweenControllers.Count; i++)
  163. {
  164. UITweenController uiTweenController = _uiTweenControllers[i];
  165. uiTweenController.SetMaxTime();
  166. if (uiTweenController.maxDuration > maxDuration)
  167. {
  168. maxDuration = uiTweenController.maxDuration;
  169. }
  170. }
  171. }
  172. public void JumpToTime(float t)
  173. {
  174. _currPlay.Clear();
  175. for (int i = 0; i < GrpupInfos.Count; i++)
  176. {
  177. TweenAssetGrpupInfo tweenAssetGrpupInfo = GrpupInfos[i];
  178. tweenAssetGrpupInfo.SetMaxTime();
  179. if (tweenAssetGrpupInfo.isActive)
  180. {
  181. if (tweenAssetGrpupInfo.maxDuration > maxDuration)
  182. {
  183. maxDuration = tweenAssetGrpupInfo.maxDuration;
  184. }
  185. //
  186. // if (tweenAssetGrpupInfo.isLoop)
  187. // {
  188. // isLoop = true;
  189. // }
  190. tweenAssetGrpupInfo.StartPlay(this);
  191. _currPlay.Add(tweenAssetGrpupInfo);
  192. }
  193. }
  194. for (int i = 0; i < _currPlay.Count; i++)
  195. {
  196. _currPlay[i].JumpToTime(this, t);
  197. // allTweenInfo[i].Rest();
  198. }
  199. for (int i = 0; i < _currPlay.Count; i++)
  200. {
  201. _currPlay[i].PlayTween(this, t, true);
  202. }
  203. UITweenController[] uiTweenControllers =
  204. gameObject.transform.GetComponentsInChildren<UITweenController>();
  205. _uiTweenControllers.AddRange(uiTweenControllers);
  206. _uiTweenControllers.Remove(this);
  207. for (int i = 0; i < _uiTweenControllers.Count; i++)
  208. {
  209. UITweenController uiTweenController = _uiTweenControllers[i];
  210. uiTweenController.JumpToTime(t);
  211. if (uiTweenController.maxDuration > maxDuration)
  212. {
  213. maxDuration = uiTweenController.maxDuration;
  214. }
  215. }
  216. // PlayTween(t, true);
  217. }
  218. private void PlayTween(float t, bool isFallBack)
  219. {
  220. for (int i = 0; i < GrpupInfos.Count; i++)
  221. {
  222. GrpupInfos[i].PlayTween(this, t, isFallBack);
  223. }
  224. }
  225. private void Update()
  226. {
  227. if (!isUpdate)
  228. {
  229. return;
  230. }
  231. currTime += Time.deltaTime;
  232. for (int i = 0; i < _currPlay.Count; i++)
  233. {
  234. _currPlay[i].PlayTween(this, currTime, false);
  235. }
  236. for (int i = 0; i < _uiTweenControllers.Count; i++)
  237. {
  238. _uiTweenControllers[i].PlayTween(currTime, false);
  239. }
  240. if (!isLoop && currTime >= maxDuration)
  241. {
  242. isUpdate = false;
  243. _currPlay.Clear();
  244. }
  245. }
  246. }
  247. }