Tweener.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. /// <summary>
  9. /// Base class for all tweening operations.
  10. /// </summary>
  11. public abstract class Tweener : MonoBehaviour
  12. {
  13. public delegate void TweenerDelegate();
  14. /// <summary>
  15. /// Current tween that triggered the callback function.
  16. /// </summary>
  17. static public Tweener current;
  18. public enum Method
  19. {
  20. Linear,
  21. EaseIn,
  22. EaseOut,
  23. EaseInOut,
  24. BounceIn,
  25. BounceOut,
  26. }
  27. public enum Style
  28. {
  29. Once,
  30. Loop,
  31. PingPong,
  32. }
  33. /// <summary>
  34. /// Tweening method used.
  35. /// </summary>
  36. //[HideInInspector]
  37. public Method method = Method.Linear;
  38. /// <summary>
  39. /// Does it play once? Does it loop?
  40. /// </summary>
  41. //[HideInInspector]
  42. public Style style = Style.Once;
  43. /// <summary>
  44. /// Optional curve to apply to the tween's time factor value.
  45. /// </summary>
  46. //[HideInInspector]
  47. public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
  48. /// <summary>
  49. /// Whether the tween will ignore the timescale, making it work while the game is paused.
  50. /// </summary>
  51. //[HideInInspector]
  52. public bool ignoreTimeScale = false;
  53. /// <summary>
  54. /// How long will the tweener wait before starting the tween?
  55. /// </summary>
  56. //[HideInInspector]
  57. public float delay = 0f;
  58. /// <summary>
  59. /// How long is the duration of the tween?
  60. /// </summary>
  61. //[HideInInspector]
  62. public float duration = 1f;
  63. /// <summary>
  64. /// Whether the tweener will use steeper curves for ease in / out style interpolation.
  65. /// </summary>
  66. //[HideInInspector]
  67. public bool steeperCurves = false;
  68. /// <summary>
  69. /// PingPong的次数
  70. /// </summary>
  71. public int SpringCount = -1;
  72. /// <summary>
  73. /// Used by buttons and tween sequences. Group of '0' means not in a sequence.
  74. /// </summary>
  75. [HideInInspector]
  76. public int tweenGroup = 0;
  77. /// <summary>
  78. /// Event delegates called when the animation finishes.
  79. /// </summary>
  80. [HideInInspector]
  81. public TweenerDelegate onFinished = null;
  82. public bool needEnableReset = false;
  83. bool mStarted = false;
  84. float mStartTime = 0f;
  85. float mDuration = 0f;
  86. float mAmountPerDelta = 1000f;
  87. float mFactor = 0f;
  88. int mSpringedTimes = 0;
  89. float mPassedTime = 0;
  90. /// <summary>
  91. /// Amount advanced per delta time.
  92. /// </summary>
  93. public float amountPerDelta
  94. {
  95. get
  96. {
  97. if (mDuration != duration)
  98. {
  99. mDuration = duration;
  100. mAmountPerDelta = Mathf.Abs((duration > 0f) ? 1f / duration : 1000f) * Mathf.Sign(mAmountPerDelta);
  101. }
  102. return mAmountPerDelta;
  103. }
  104. }
  105. /// <summary>
  106. /// Tween factor, 0-1 range.
  107. /// </summary>
  108. public float tweenFactor { get { return mFactor; } set { mFactor = Mathf.Clamp01(value); } }
  109. /// <summary>
  110. /// This function is called by Unity when you add a component. Automatically set the starting values for convenience.
  111. /// </summary>
  112. void Reset ()
  113. {
  114. if (!mStarted)
  115. {
  116. SetStartToCurrentValue();
  117. SetEndToCurrentValue();
  118. }
  119. }
  120. /// <summary>
  121. /// Update as soon as it's started so that there is no delay.
  122. /// </summary>
  123. protected virtual void Start () { Update(); }
  124. /// <summary>
  125. /// Update the tweening factor and call the virtual update function.
  126. /// </summary>
  127. void Update ()
  128. {
  129. float delta = ignoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
  130. float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
  131. if (!mStarted)
  132. {
  133. mStarted = true;
  134. mStartTime = time + delay;
  135. }
  136. if (time < mStartTime) return;
  137. // Advance the sampling factor
  138. mFactor += amountPerDelta * delta;
  139. mPassedTime += delta;
  140. // Loop style simply resets the play factor after it exceeds 1.
  141. if (style == Style.Loop)
  142. {
  143. if (mFactor > 1f)
  144. {
  145. mFactor -= Mathf.Floor(mFactor);
  146. }
  147. }
  148. else if (style == Style.PingPong)
  149. {
  150. // Ping-pong style reverses the direction
  151. if (mFactor > 1f)
  152. {
  153. mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
  154. mAmountPerDelta = -mAmountPerDelta;
  155. }
  156. else if (mFactor < 0f)
  157. {
  158. mFactor = -mFactor;
  159. mFactor -= Mathf.Floor(mFactor);
  160. mAmountPerDelta = -mAmountPerDelta;
  161. }
  162. if(mPassedTime>=2*mDuration)
  163. {
  164. mSpringedTimes++;
  165. mPassedTime = 0;
  166. }
  167. if (SpringCount > 0 && mSpringedTimes >= SpringCount)
  168. {
  169. enabled = false;
  170. mSpringedTimes = 0;
  171. if (onFinished != null)
  172. {
  173. onFinished();
  174. }
  175. return;
  176. }
  177. }
  178. // If the factor goes out of range and this is a one-time tweening operation, disable the script
  179. if ((style == Style.Once) && (duration == 0f || mFactor > 1f || mFactor < 0f))
  180. {
  181. mFactor = Mathf.Clamp01(mFactor);
  182. Sample(mFactor, true);
  183. // Disable this script unless the function calls above changed something
  184. if (duration == 0f || (mFactor == 1f && mAmountPerDelta > 0f || mFactor == 0f && mAmountPerDelta < 0f))
  185. enabled = false;
  186. if (current == null)
  187. {
  188. current = this;
  189. if (onFinished != null)
  190. {
  191. onFinished();
  192. }
  193. current = null;
  194. }
  195. }
  196. else Sample(mFactor, false);
  197. }
  198. /// <summary>
  199. /// Convenience function -- set a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
  200. /// </summary>
  201. public void SetOnFinished(TweenerDelegate del) { onFinished = del; }
  202. /// <summary>
  203. /// Convenience function -- add a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
  204. /// </summary>
  205. public void AddOnFinished(TweenerDelegate del) { onFinished += del; }
  206. /// <summary>
  207. /// Remove an OnFinished delegate. Will work even while iterating through the list when the tweener has finished its operation.
  208. /// </summary>
  209. public void RemoveOnFinished (TweenerDelegate del)
  210. {
  211. if (onFinished != null) onFinished -= del;
  212. }
  213. /// <summary>
  214. /// Mark as not started when finished to enable delay on next play.
  215. /// </summary>
  216. void OnDisable () { mStarted = false; }
  217. /// <summary>
  218. /// Sample the tween at the specified factor.
  219. /// </summary>
  220. public void Sample (float factor, bool isFinished)
  221. {
  222. // Calculate the sampling value
  223. float val = Mathf.Clamp01(factor);
  224. if (method == Method.EaseIn)
  225. {
  226. val = 1f - Mathf.Sin(0.5f * Mathf.PI * (1f - val));
  227. if (steeperCurves) val *= val;
  228. }
  229. else if (method == Method.EaseOut)
  230. {
  231. val = Mathf.Sin(0.5f * Mathf.PI * val);
  232. if (steeperCurves)
  233. {
  234. val = 1f - val;
  235. val = 1f - val * val;
  236. }
  237. }
  238. else if (method == Method.EaseInOut)
  239. {
  240. const float pi2 = Mathf.PI * 2f;
  241. val = val - Mathf.Sin(val * pi2) / pi2;
  242. if (steeperCurves)
  243. {
  244. val = val * 2f - 1f;
  245. float sign = Mathf.Sign(val);
  246. val = 1f - Mathf.Abs(val);
  247. val = 1f - val * val;
  248. val = sign * val * 0.5f + 0.5f;
  249. }
  250. }
  251. else if (method == Method.BounceIn)
  252. {
  253. val = BounceLogic(val);
  254. }
  255. else if (method == Method.BounceOut)
  256. {
  257. val = 1f - BounceLogic(1f - val);
  258. }
  259. // Call the virtual update
  260. OnUpdate((animationCurve != null) ? animationCurve.Evaluate(val) : val, isFinished);
  261. }
  262. /// <summary>
  263. /// Main Bounce logic to simplify the Sample function
  264. /// </summary>
  265. float BounceLogic (float val)
  266. {
  267. if (val < 0.363636f) // 0.363636 = (1/ 2.75)
  268. {
  269. val = 7.5685f * val * val;
  270. }
  271. else if (val < 0.727272f) // 0.727272 = (2 / 2.75)
  272. {
  273. val = 7.5625f * (val -= 0.545454f) * val + 0.75f; // 0.545454f = (1.5 / 2.75)
  274. }
  275. else if (val < 0.909090f) // 0.909090 = (2.5 / 2.75)
  276. {
  277. val = 7.5625f * (val -= 0.818181f) * val + 0.9375f; // 0.818181 = (2.25 / 2.75)
  278. }
  279. else
  280. {
  281. val = 7.5625f * (val -= 0.9545454f) * val + 0.984375f; // 0.9545454 = (2.625 / 2.75)
  282. }
  283. return val;
  284. }
  285. /// <summary>
  286. /// Play the tween.
  287. /// </summary>
  288. [System.Obsolete("Use PlayForward() instead")]
  289. public void Play () { Play(true); }
  290. /// <summary>
  291. /// Play the tween forward.
  292. /// </summary>
  293. public void PlayForward () { Play(true); }
  294. /// <summary>
  295. /// Play the tween in reverse.
  296. /// </summary>
  297. public void PlayReverse () { Play(false); }
  298. /// <summary>
  299. /// Manually activate the tweening process, reversing it if necessary.
  300. /// </summary>
  301. public void Play (bool forward)
  302. {
  303. mAmountPerDelta = Mathf.Abs(amountPerDelta);
  304. if (!forward) mAmountPerDelta = -mAmountPerDelta;
  305. enabled = true;
  306. Update();
  307. }
  308. /// <summary>
  309. /// Manually reset the tweener's state to the beginning.
  310. /// If the tween is playing forward, this means the tween's start.
  311. /// If the tween is playing in reverse, this means the tween's end.
  312. /// </summary>
  313. public void ResetToBeginning ()
  314. {
  315. mStarted = false;
  316. mFactor = (amountPerDelta < 0f) ? 1f : 0f;
  317. Sample(mFactor, false);
  318. }
  319. /// <summary>
  320. /// Manually start the tweening process, reversing its direction.
  321. /// </summary>
  322. public void Toggle ()
  323. {
  324. if (mFactor > 0f)
  325. {
  326. mAmountPerDelta = -amountPerDelta;
  327. }
  328. else
  329. {
  330. mAmountPerDelta = Mathf.Abs(amountPerDelta);
  331. }
  332. enabled = true;
  333. }
  334. /// <summary>
  335. /// Actual tweening logic should go here.
  336. /// </summary>
  337. abstract protected void OnUpdate (float factor, bool isFinished);
  338. /// <summary>
  339. /// Starts the tweening operation.
  340. /// </summary>
  341. static public T Begin<T> (GameObject go, float duration) where T : Tweener
  342. {
  343. T comp = go.GetComponent<T>();
  344. if (comp != null && comp.tweenGroup != 0)
  345. {
  346. comp = null;
  347. T[] comps = go.GetComponents<T>();
  348. for (int i = 0, imax = comps.Length; i < imax; ++i)
  349. {
  350. comp = comps[i];
  351. if (comp != null && comp.tweenGroup == 0) break;
  352. comp = null;
  353. }
  354. }
  355. if (comp == null)
  356. {
  357. comp = go.AddComponent<T>();
  358. comp.animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
  359. }
  360. comp.mStarted = false;
  361. if(duration > 0)
  362. comp.duration = duration;
  363. comp.mFactor = 0f;
  364. comp.mAmountPerDelta = Mathf.Abs(comp.amountPerDelta);
  365. comp.style = Style.Once;
  366. comp.enabled = true;
  367. if (duration <= 0f)
  368. {
  369. comp.Sample(1f, true);
  370. comp.enabled = false;
  371. }
  372. return comp;
  373. }
  374. /// <summary>
  375. /// Set the 'from' value to the current one.
  376. /// </summary>
  377. public virtual void SetStartToCurrentValue () { }
  378. /// <summary>
  379. /// Set the 'to' value to the current one.
  380. /// </summary>
  381. public virtual void SetEndToCurrentValue () { }
  382. static public Vector3 SpringDampen(ref Vector3 velocity, float strength, float deltaTime)
  383. {
  384. if (deltaTime > 1f) deltaTime = 1f;
  385. float dampeningFactor = 1f - strength * 0.001f;
  386. int ms = Mathf.RoundToInt(deltaTime * 1000f);
  387. float totalDampening = Mathf.Pow(dampeningFactor, ms);
  388. Vector3 vTotal = velocity * ((totalDampening - 1f) / Mathf.Log(dampeningFactor));
  389. velocity = velocity * totalDampening;
  390. return vTotal * 0.06f;
  391. }
  392. /// <summary>
  393. /// Same as the Vector3 version, it's a framerate-independent Lerp.
  394. /// </summary>
  395. static public Vector2 SpringDampen(ref Vector2 velocity, float strength, float deltaTime)
  396. {
  397. if (deltaTime > 1f) deltaTime = 1f;
  398. float dampeningFactor = 1f - strength * 0.001f;
  399. int ms = Mathf.RoundToInt(deltaTime * 1000f);
  400. float totalDampening = Mathf.Pow(dampeningFactor, ms);
  401. Vector2 vTotal = velocity * ((totalDampening - 1f) / Mathf.Log(dampeningFactor));
  402. velocity = velocity * totalDampening;
  403. return vTotal * 0.06f;
  404. }
  405. /// <summary>
  406. /// Calculate how much to interpolate by.
  407. /// </summary>
  408. static public float SpringLerp(float strength, float deltaTime)
  409. {
  410. if (deltaTime > 1f) deltaTime = 1f;
  411. int ms = Mathf.RoundToInt(deltaTime * 1000f);
  412. deltaTime = 0.001f * strength;
  413. float cumulative = 0f;
  414. for (int i = 0; i < ms; ++i) cumulative = Mathf.Lerp(cumulative, 1f, deltaTime);
  415. return cumulative;
  416. }
  417. /// <summary>
  418. /// Mathf.Lerp(from, to, Time.deltaTime * strength) is not framerate-independent. This function is.
  419. /// </summary>
  420. static public float SpringLerp(float from, float to, float strength, float deltaTime)
  421. {
  422. if (deltaTime > 1f) deltaTime = 1f;
  423. int ms = Mathf.RoundToInt(deltaTime * 1000f);
  424. deltaTime = 0.001f * strength;
  425. for (int i = 0; i < ms; ++i) from = Mathf.Lerp(from, to, deltaTime);
  426. return from;
  427. }
  428. /// <summary>
  429. /// Vector2.Lerp(from, to, Time.deltaTime * strength) is not framerate-independent. This function is.
  430. /// </summary>
  431. static public Vector2 SpringLerp(Vector2 from, Vector2 to, float strength, float deltaTime)
  432. {
  433. return Vector2.Lerp(from, to, SpringLerp(strength, deltaTime));
  434. }
  435. /// <summary>
  436. /// Vector3.Lerp(from, to, Time.deltaTime * strength) is not framerate-independent. This function is.
  437. /// </summary>
  438. static public Vector3 SpringLerp(Vector3 from, Vector3 to, float strength, float deltaTime)
  439. {
  440. return Vector3.Lerp(from, to, SpringLerp(strength, deltaTime));
  441. }
  442. /// <summary>
  443. /// Quaternion.Slerp(from, to, Time.deltaTime * strength) is not framerate-independent. This function is.
  444. /// </summary>
  445. static public Quaternion SpringLerp(Quaternion from, Quaternion to, float strength, float deltaTime)
  446. {
  447. return Quaternion.Slerp(from, to, SpringLerp(strength, deltaTime));
  448. }
  449. private void OnEnable()
  450. {
  451. if(needEnableReset)
  452. {
  453. duration = 1;
  454. mDuration = 0;
  455. mAmountPerDelta = 1000;
  456. ResetToBeginning();
  457. PlayForward();
  458. }
  459. }
  460. }