UITweenController.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. [ExecuteInEditMode]
  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. public void Play(string animName,bool isChildren)
  27. {
  28. _uiTweenControllers.Clear();
  29. maxDuration = 0;
  30. _currPlay.Clear();
  31. isLoop = false;
  32. for (int i = 0; i < GrpupInfos.Count; i++)
  33. {
  34. TweenAssetGrpupInfo tweenAssetGrpupInfo = GrpupInfos[i];
  35. tweenAssetGrpupInfo.SetMaxTime();
  36. if (tweenAssetGrpupInfo.animName.Equals(animName))
  37. {
  38. if (tweenAssetGrpupInfo.maxDuration > maxDuration)
  39. {
  40. maxDuration = tweenAssetGrpupInfo.maxDuration;
  41. }
  42. if (tweenAssetGrpupInfo.isLoop)
  43. {
  44. isLoop = true;
  45. }
  46. tweenAssetGrpupInfo.StartPlay();
  47. _currPlay.Add(tweenAssetGrpupInfo);
  48. }
  49. }
  50. if (isChildren)
  51. {
  52. UITweenController[] uiTweenControllers =
  53. gameObject.transform.GetComponentsInChildren<UITweenController>();
  54. _uiTweenControllers.AddRange(uiTweenControllers);
  55. _uiTweenControllers.Remove(this);
  56. for (int i = 0; i < _uiTweenControllers.Count; i++)
  57. {
  58. UITweenController uiTweenController = _uiTweenControllers[i];
  59. // if(uiTweenController.)
  60. uiTweenController.StartPlay(false);
  61. if (uiTweenController.maxDuration > maxDuration)
  62. {
  63. maxDuration = uiTweenController.maxDuration;
  64. }
  65. if (uiTweenController.isLoop)
  66. {
  67. isLoop = true;
  68. }
  69. }
  70. }
  71. isUpdate = true;
  72. currTime = 0;
  73. }
  74. public void Stop()
  75. {
  76. isUpdate = false;
  77. }
  78. public void StartPlay(bool isChildren)
  79. {
  80. _uiTweenControllers.Clear();
  81. maxDuration = 0;
  82. _currPlay.Clear();
  83. isLoop = false;
  84. for (int i = 0; i < GrpupInfos.Count; i++)
  85. {
  86. TweenAssetGrpupInfo tweenAssetGrpupInfo = GrpupInfos[i];
  87. tweenAssetGrpupInfo.SetMaxTime();
  88. if (tweenAssetGrpupInfo.isActive)
  89. {
  90. if (tweenAssetGrpupInfo.maxDuration > maxDuration)
  91. {
  92. maxDuration = tweenAssetGrpupInfo.maxDuration;
  93. }
  94. if (tweenAssetGrpupInfo.isLoop)
  95. {
  96. isLoop = true;
  97. }
  98. tweenAssetGrpupInfo.StartPlay();
  99. _currPlay.Add(tweenAssetGrpupInfo);
  100. }
  101. }
  102. if (isChildren)
  103. {
  104. UITweenController[] uiTweenControllers =
  105. gameObject.transform.GetComponentsInChildren<UITweenController>();
  106. _uiTweenControllers.AddRange(uiTweenControllers);
  107. _uiTweenControllers.Remove(this);
  108. for (int i = 0; i < _uiTweenControllers.Count; i++)
  109. {
  110. UITweenController uiTweenController = _uiTweenControllers[i];
  111. uiTweenController.StartPlay(false);
  112. if (uiTweenController.maxDuration > maxDuration)
  113. {
  114. maxDuration = uiTweenController.maxDuration;
  115. }
  116. if (uiTweenController.isLoop)
  117. {
  118. isLoop = true;
  119. }
  120. }
  121. }
  122. isUpdate = true;
  123. currTime = 0;
  124. }
  125. public void SetMaxTime()
  126. {
  127. maxDuration = 0;
  128. for (int i = 0; i < GrpupInfos.Count; i++)
  129. {
  130. TweenAssetGrpupInfo tweenAssetGrpupInfo = GrpupInfos[i];
  131. tweenAssetGrpupInfo.SetMaxTime();
  132. if (tweenAssetGrpupInfo.isActive)
  133. {
  134. if (tweenAssetGrpupInfo.maxDuration > maxDuration)
  135. {
  136. maxDuration = tweenAssetGrpupInfo.maxDuration;
  137. }
  138. }
  139. }
  140. for (int i = 0; i < _uiTweenControllers.Count; i++)
  141. {
  142. UITweenController uiTweenController = _uiTweenControllers[i];
  143. uiTweenController.SetMaxTime();
  144. if (uiTweenController.maxDuration > maxDuration)
  145. {
  146. maxDuration = uiTweenController.maxDuration;
  147. }
  148. }
  149. }
  150. public void JumpToTime(float t)
  151. {
  152. for (int i = 0; i < GrpupInfos.Count; i++)
  153. {
  154. GrpupInfos[i].JumpToTime(t);
  155. // allTweenInfo[i].Rest();
  156. }
  157. for (int i = 0; i < _uiTweenControllers.Count; i++)
  158. {
  159. _uiTweenControllers[i].JumpToTime(t);
  160. }
  161. PlayTween(t, true);
  162. }
  163. private void PlayTween(float t, bool isFallBack)
  164. {
  165. for (int i = 0; i < GrpupInfos.Count; i++)
  166. {
  167. GrpupInfos[i].PlayTween(t, isFallBack);
  168. }
  169. }
  170. private void Update()
  171. {
  172. if (!isUpdate)
  173. {
  174. return;
  175. }
  176. currTime += Time.deltaTime;
  177. for (int i = 0; i < _currPlay.Count; i++)
  178. {
  179. _currPlay[i].PlayTween(currTime, false);
  180. }
  181. for (int i = 0; i < _uiTweenControllers.Count; i++)
  182. {
  183. _uiTweenControllers[i].PlayTween(currTime, false);
  184. }
  185. if (!isLoop && currTime >= maxDuration)
  186. {
  187. isUpdate = false;
  188. }
  189. }
  190. }
  191. }