Tweener.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. namespace Rokid.MRC
  7. {
  8. //基类
  9. public abstract class Tweener : MonoBehaviour
  10. {
  11. static public Tweener current;
  12. public enum Direction
  13. {
  14. eReverse = -1,
  15. eToggle = 0,
  16. eForward = 1
  17. }
  18. public enum Trigger
  19. {
  20. eOnPointerEnter,
  21. eOnPointerDown,
  22. eOnPointerClick,
  23. eOnPointerUp,
  24. eOnPointerExit,
  25. None
  26. }
  27. public enum ShakeType
  28. {
  29. ePosition,
  30. eScale,
  31. eRotation
  32. }
  33. public iTween.EaseType method = iTween.EaseType.linear;
  34. public iTween.LoopType style = iTween.LoopType.none;
  35. public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
  36. public bool ignoreTimeScale = true;
  37. public float delay = 0f;
  38. public float duration = 1f;
  39. public float fixedDelta = 0f;
  40. public int tweenGroup = 0;
  41. private UnityAction onFinished = null;
  42. private UnityAction onValueChange = null;
  43. //[HideInInspector]
  44. //protected GameObject eventReceiver;
  45. //[HideInInspector]
  46. //protected string callWhenFinished;
  47. private bool mStarted = false;
  48. private float mStartTime = 0f;
  49. private float mDuration = 0f;
  50. private float mAmountPerDelta = 1000f;
  51. private float mFactor = 0f;
  52. public bool currentDirection
  53. {
  54. get
  55. {
  56. return mFactor > 0;
  57. }
  58. }
  59. /// <summary>
  60. /// Amount advanced per delta time.
  61. /// </summary>
  62. protected float amountPerDelta
  63. {
  64. get
  65. {
  66. if(mDuration != duration)
  67. {
  68. mDuration = duration;
  69. mAmountPerDelta = Mathf.Abs((duration > 0f) ? 1f / duration : 1000f) * Mathf.Sign(mAmountPerDelta);
  70. }
  71. return mAmountPerDelta;
  72. }
  73. }
  74. protected void Reset()
  75. {
  76. if(!mStarted)
  77. {
  78. SetStartToCurrentValue();
  79. SetEndToCurrentValue();
  80. }
  81. }
  82. protected virtual void Start()
  83. {
  84. Update();
  85. }
  86. //实时进行缓动
  87. void Update()
  88. {
  89. float delta = ignoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
  90. float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
  91. if(fixedDelta > float.Epsilon)
  92. {
  93. delta = fixedDelta;
  94. }
  95. if(!mStarted)
  96. {
  97. mStarted = true;
  98. mStartTime = time + delay;
  99. }
  100. if(time < mStartTime)
  101. return;
  102. // Advance the sampling factor
  103. mFactor += amountPerDelta * delta;
  104. // Loop style simply resets the play factor after it exceeds 1.
  105. if(style == iTween.LoopType.loop)
  106. {
  107. if(mFactor > 1f)
  108. {
  109. mFactor -= Mathf.Floor(mFactor);
  110. }
  111. }
  112. else if(style == iTween.LoopType.pingPong)
  113. {
  114. // Ping-pong style reverses the direction
  115. if(mFactor > 1f)
  116. {
  117. mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
  118. mAmountPerDelta = -mAmountPerDelta;
  119. }
  120. else if(mFactor < 0f)
  121. {
  122. mFactor = -mFactor;
  123. mFactor -= Mathf.Floor(mFactor);
  124. mAmountPerDelta = -mAmountPerDelta;
  125. }
  126. }
  127. // If the factor goes out of range and this is a one-time tweening operation, disable the script
  128. if((style == iTween.LoopType.none) && (duration == 0f || mFactor > 1f || mFactor < 0f))
  129. {
  130. mFactor = Mathf.Clamp01(mFactor);
  131. Sample(mFactor, true);
  132. enabled = false;
  133. if(current != this)
  134. {
  135. Tweener before = current;
  136. current = this;
  137. if(onFinished != null)
  138. onFinished();
  139. // Deprecated legacy functionality support
  140. //if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
  141. // eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
  142. current = before;
  143. }
  144. }
  145. else
  146. Sample(mFactor, false);
  147. }
  148. public void SetOnFinished(UnityAction finishedCallBack)
  149. {
  150. onFinished = finishedCallBack;
  151. }
  152. public void AddOnFinished(UnityAction finishedCallBack)
  153. {
  154. onFinished += finishedCallBack;
  155. }
  156. public void RemoveOnFinished(UnityAction finishedCallBack)
  157. {
  158. if(onFinished != null)
  159. onFinished -= finishedCallBack;
  160. }
  161. void OnDisable()
  162. {
  163. mStarted = false;
  164. }
  165. /// <summary>
  166. /// Sample the tween at the specified factor.
  167. /// </summary>
  168. protected void Sample(float factor, bool isFinished)
  169. {
  170. // Calculate the sampling value
  171. float val = Mathf.Clamp01(factor);
  172. val = (method == iTween.EaseType.None) ? animationCurve.Evaluate(val) : iTween.easeValue(method, 0, 1, val);
  173. // Call the virtual update
  174. OnUpdate((method == iTween.EaseType.None) ? animationCurve.Evaluate(val) : val, isFinished);
  175. if(onValueChange != null)
  176. {
  177. onValueChange.Invoke();
  178. }
  179. }
  180. /// <summary>
  181. /// Manually activate the tweening process, reversing it if necessary.
  182. /// true Play the tween forward.
  183. /// false Play the tween in reverse.
  184. /// </summary>
  185. public void Play(bool forward)
  186. {
  187. mAmountPerDelta = Mathf.Abs(amountPerDelta);
  188. if(!forward)
  189. mAmountPerDelta = -mAmountPerDelta;
  190. enabled = true;
  191. Update();
  192. }
  193. /// <summary>
  194. /// Manually reset the tweener's state to the beginning.
  195. /// If the tween is playing forward, this means the tween's start.
  196. /// If the tween is playing in reverse, this means the tween's end.
  197. /// </summary>
  198. public void ResetToBeginning()
  199. {
  200. mStarted = false;
  201. mFactor = (amountPerDelta < 0f) ? 1f : 0f;
  202. Sample(mFactor, false);
  203. }
  204. /// <summary>
  205. /// Manually start the tweening process, reversing its direction.
  206. /// </summary>
  207. public void Toggle()
  208. {
  209. if(mFactor > 0f)
  210. {
  211. mAmountPerDelta = -amountPerDelta;
  212. }
  213. else
  214. {
  215. mAmountPerDelta = Mathf.Abs(amountPerDelta);
  216. }
  217. enabled = true;
  218. }
  219. /// <summary>
  220. /// Actual tweening logic should go here.
  221. /// </summary>
  222. abstract protected void OnUpdate(float factor, bool isFinished);
  223. /// <summary>
  224. /// Starts the tweening operation.
  225. /// </summary>
  226. static protected T Begin<T>(GameObject go, float duration) where T : Tweener
  227. {
  228. T comp = go.GetComponent<T>();
  229. #if UNITY_FLASH
  230. if ((object)comp == null) comp = (T)go.AddComponent<T>();
  231. #else
  232. // Find the tween with an unset group ID (group ID of 0).
  233. if(comp != null && comp.tweenGroup != 0)
  234. {
  235. comp = null;
  236. T[] comps = go.GetComponents<T>();
  237. for(int i = 0, imax = comps.Length;i < imax;++i)
  238. {
  239. comp = comps[i];
  240. if(comp != null && comp.tweenGroup == 0)
  241. break;
  242. comp = null;
  243. }
  244. }
  245. if(comp == null)
  246. {
  247. comp = go.AddComponent<T>();
  248. if(comp == null)
  249. {
  250. Debug.LogError("Unable to add " + typeof(T) + " to " + go);
  251. return null;
  252. }
  253. }
  254. #endif
  255. comp.mStarted = false;
  256. comp.duration = duration;
  257. comp.mFactor = 0f;
  258. comp.mAmountPerDelta = Mathf.Abs(comp.amountPerDelta);
  259. comp.style = iTween.LoopType.none;
  260. comp.animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
  261. //comp.eventReceiver = null;
  262. //comp.callWhenFinished = null;
  263. comp.onFinished = null;
  264. comp.enabled = true;
  265. return comp;
  266. }
  267. protected virtual void SetStartToCurrentValue()
  268. {
  269. }
  270. protected virtual void SetEndToCurrentValue()
  271. {
  272. }
  273. protected virtual void SetCurrentValueToStart()
  274. {
  275. }
  276. protected virtual void SetCurrentValueToEnd()
  277. {
  278. }
  279. }
  280. }