UITweenController.cs 8.5 KB

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