You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

209 lines
4.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using UnityEngine;
  5. namespace Tweening
  6. {
  7. public sealed class Tweener : MonoBehaviour
  8. {
  9. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  10. public event Tweener.FinishedPlayingEventHandler FinishedPlaying;
  11. private void Awake()
  12. {
  13. this.IsPlaying = false;
  14. this.IsPaused = false;
  15. this._tracks.Sort((TweenerTrackBase a, TweenerTrackBase b) => a.Delay.CompareTo(b.Delay));
  16. this.AnimationTime = this.CalcHighestEndTime();
  17. }
  18. private void Start()
  19. {
  20. if (this._playOnStart)
  21. {
  22. this.Play();
  23. }
  24. }
  25. private void Update()
  26. {
  27. if (!this.IsPlaying || this.IsPaused)
  28. {
  29. return;
  30. }
  31. if (this._time >= this.AnimationTime && this._loop)
  32. {
  33. this._time = 0f;
  34. this.UpdateTrackComponentValues();
  35. }
  36. this._time += Time.deltaTime;
  37. this.UpdateTrackComponentValues();
  38. if (this._time >= this.AnimationTime && !this._loop)
  39. {
  40. this.UpdateTrackComponentValues((!this.IsPlaybackReversed) ? this.AnimationTime : 0f);
  41. this.IsPlaying = false;
  42. if (this.FinishedPlaying != null)
  43. {
  44. this.FinishedPlaying(this);
  45. }
  46. }
  47. }
  48. public void Play()
  49. {
  50. this.Play(false, 0f);
  51. }
  52. public void PlayReverse()
  53. {
  54. this.Play(true, 0f);
  55. }
  56. public void PlayWithOffset(float offset)
  57. {
  58. if (this._loop)
  59. {
  60. offset %= this.AnimationTime;
  61. }
  62. else
  63. {
  64. offset = Mathf.Clamp(offset, 0f, this.AnimationTime);
  65. }
  66. this.Play(false, offset);
  67. }
  68. public void TogglePause()
  69. {
  70. this.IsPaused = !this.IsPaused;
  71. }
  72. public void Stop()
  73. {
  74. if (!this.IsPlaying)
  75. {
  76. UnityEngine.Debug.LogWarning("Tweener isn't playing.");
  77. return;
  78. }
  79. this.IsPlaying = false;
  80. }
  81. public void Reset(bool resetToEnd = false)
  82. {
  83. if (this.IsPlaying)
  84. {
  85. UnityEngine.Debug.LogWarning("Can't reset while playing.");
  86. return;
  87. }
  88. for (int i = this._tracks.Count - 1; i >= 0; i--)
  89. {
  90. this._tracks[i].ResetTrack();
  91. }
  92. if (resetToEnd)
  93. {
  94. this.UpdateTrackComponentValues(this.CalcHighestEndTime());
  95. }
  96. }
  97. public void StopAndReset(bool resetToEnd = false)
  98. {
  99. if (this.IsPlaying)
  100. {
  101. this.Stop();
  102. }
  103. this.Reset(resetToEnd);
  104. }
  105. public bool IsPlaying { get; private set; }
  106. public bool IsPaused { get; private set; }
  107. public bool IsPlaybackReversed { get; private set; }
  108. public float AnimationTime { get; private set; }
  109. private void Play(bool playReversed, float offset)
  110. {
  111. if (this.IsPlaying)
  112. {
  113. UnityEngine.Debug.LogWarning("Tweener is already playing.");
  114. return;
  115. }
  116. this.IsPlaybackReversed = playReversed;
  117. this.IsPlaying = true;
  118. this.IsPaused = false;
  119. this._time = offset;
  120. this.AnimationTime = this.CalcHighestEndTime();
  121. for (int i = this._tracks.Count - 1; i >= 0; i--)
  122. {
  123. this._tracks[i].ResetTrack();
  124. }
  125. this.UpdateTrackComponentValues();
  126. }
  127. private void UpdateTrackComponentValues()
  128. {
  129. this.UpdateTrackComponentValues((!this.IsPlaybackReversed) ? this._time : (this.AnimationTime - this._time));
  130. }
  131. private void UpdateTrackComponentValues(float trackTime)
  132. {
  133. int count = this._tracks.Count;
  134. for (int i = 0; i < count; i++)
  135. {
  136. Tweener.UpdateComponentValue(this._tracks[i], trackTime);
  137. }
  138. }
  139. private static void UpdateComponentValue(TweenerTrackBase track, float time)
  140. {
  141. if (time >= track.Delay)
  142. {
  143. track.InitializeTrack();
  144. WrapMode postWrapMode = track.Curve.postWrapMode;
  145. float time2;
  146. if (postWrapMode != WrapMode.Loop)
  147. {
  148. if (postWrapMode != WrapMode.PingPong)
  149. {
  150. time2 = Mathf.Clamp01((time - track.Delay) / track.Duration);
  151. }
  152. else
  153. {
  154. time2 = Mathf.PingPong((time - track.Delay) / track.Duration, 1f);
  155. }
  156. }
  157. else
  158. {
  159. time2 = (time - track.Delay) / track.Duration;
  160. }
  161. track.UpdateComponentValue(track.Curve.Evaluate(time2));
  162. }
  163. }
  164. private float CalcHighestEndTime()
  165. {
  166. float num = 0f;
  167. int count = this._tracks.Count;
  168. for (int i = 0; i < count; i++)
  169. {
  170. num = Mathf.Max(num, this._tracks[i].EndTime);
  171. }
  172. return num;
  173. }
  174. [SerializeField]
  175. private bool _playOnStart;
  176. [SerializeField]
  177. private bool _loop;
  178. [SerializeField]
  179. private List<TweenerTrackBase> _tracks;
  180. private float _time;
  181. public delegate void FinishedPlayingEventHandler(Tweener tweener);
  182. }
  183. }