DOTweenAnimation.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2015/03/12 15:55
  3. using System;
  4. using System.Collections.Generic;
  5. using DG.Tweening.Core;
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using UnityEngine.UI;
  9. #if DOTWEEN_TMP
  10. using TMPro;
  11. #endif
  12. #pragma warning disable 1591
  13. namespace DG.Tweening
  14. {
  15. /// <summary>
  16. /// Attach this to a GameObject to create a tween
  17. /// </summary>
  18. [AddComponentMenu("DOTween/DOTween Animation")]
  19. public class DOTweenAnimation : ABSAnimationComponent
  20. {
  21. public float delay;
  22. public float duration = 1;
  23. public Ease easeType = Ease.OutQuad;
  24. public AnimationCurve easeCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
  25. public LoopType loopType = LoopType.Restart;
  26. public int loops = 1;
  27. public string id = "";
  28. public bool isRelative;
  29. public bool isFrom;
  30. public bool isIndependentUpdate = false;
  31. public bool autoKill = true;
  32. public bool isActive = true;
  33. public bool isValid;
  34. public Component target;
  35. public DOTweenAnimationType animationType;
  36. public bool autoPlay = true;
  37. public float endValueFloat;
  38. public Vector3 endValueV3;
  39. public Color endValueColor = new Color(1, 1, 1, 1);
  40. public string endValueString = "";
  41. public Rect endValueRect = new Rect(0, 0, 0, 0);
  42. public bool optionalBool0;
  43. public float optionalFloat0;
  44. public int optionalInt0;
  45. public RotateMode optionalRotationMode = RotateMode.Fast;
  46. public ScrambleMode optionalScrambleMode = ScrambleMode.None;
  47. public string optionalString;
  48. int _playCount = -1; // Used when calling DOPlayNext
  49. #region Unity Methods
  50. void Awake()
  51. {
  52. if (!isActive || !isValid) return;
  53. CreateTween();
  54. }
  55. void OnDestroy()
  56. {
  57. if (tween != null && tween.IsActive()) tween.Kill();
  58. tween = null;
  59. }
  60. // Used also by DOTweenAnimationInspector when applying runtime changes and restarting
  61. public void CreateTween()
  62. {
  63. if (target == null) {
  64. Debug.LogWarning(string.Format("{0} :: This tween's target is NULL, because the animation was created with a DOTween Pro version older than 0.9.255. To fix this, exit Play mode then simply select this object, and it will update automatically", this.gameObject.name), this.gameObject);
  65. return;
  66. }
  67. Type t = target.GetType();
  68. // Component c;
  69. switch (animationType) {
  70. case DOTweenAnimationType.None:
  71. break;
  72. case DOTweenAnimationType.Move:
  73. if (t.IsSameOrSubclassOf(typeof(RectTransform))) tween = ((RectTransform)target).DOAnchorPos3D(endValueV3, duration, optionalBool0);
  74. else if (t.IsSameOrSubclassOf(typeof(Transform))) tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  75. else if (t.IsSameOrSubclassOf(typeof(Rigidbody2D))) tween = ((Rigidbody2D)target).DOMove(endValueV3, duration, optionalBool0);
  76. else if (t.IsSameOrSubclassOf(typeof(Rigidbody))) tween = ((Rigidbody)target).DOMove(endValueV3, duration, optionalBool0);
  77. // c = this.GetComponent<Rigidbody2D>();
  78. // if (c != null) {
  79. // tween = ((Rigidbody2D)c).DOMove(endValueV3, duration, optionalBool0);
  80. // goto SetupTween;
  81. // }
  82. // c = this.GetComponent<Rigidbody>();
  83. // if (c != null) {
  84. // tween = ((Rigidbody)c).DOMove(endValueV3, duration, optionalBool0);
  85. // goto SetupTween;
  86. // }
  87. // c = this.GetComponent<RectTransform>();
  88. // if (c != null) {
  89. // tween = ((RectTransform)c).DOAnchorPos3D(endValueV3, duration, optionalBool0);
  90. // goto SetupTween;
  91. // }
  92. // tween = transform.DOMove(endValueV3, duration, optionalBool0);
  93. break;
  94. case DOTweenAnimationType.LocalMove:
  95. tween = transform.DOLocalMove(endValueV3, duration, optionalBool0);
  96. break;
  97. case DOTweenAnimationType.Rotate:
  98. if (t.IsSameOrSubclassOf(typeof(Transform))) tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode);
  99. else if (t.IsSameOrSubclassOf(typeof(Rigidbody2D))) tween = ((Rigidbody2D)target).DORotate(endValueFloat, duration);
  100. else if (t.IsSameOrSubclassOf(typeof(Rigidbody))) tween = ((Rigidbody)target).DORotate(endValueV3, duration, optionalRotationMode);
  101. // c = this.GetComponent<Rigidbody2D>();
  102. // if (c != null) {
  103. // tween = ((Rigidbody2D)c).DORotate(endValueFloat, duration);
  104. // goto SetupTween;
  105. // }
  106. // c = this.GetComponent<Rigidbody>();
  107. // if (c != null) {
  108. // tween = ((Rigidbody)c).DORotate(endValueV3, duration, optionalRotationMode);
  109. // goto SetupTween;
  110. // }
  111. // tween = transform.DORotate(endValueV3, duration, optionalRotationMode);
  112. break;
  113. case DOTweenAnimationType.LocalRotate:
  114. tween = transform.DOLocalRotate(endValueV3, duration, optionalRotationMode);
  115. break;
  116. case DOTweenAnimationType.Scale:
  117. tween = transform.DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  118. break;
  119. case DOTweenAnimationType.Color:
  120. isRelative = false;
  121. if (t.IsSameOrSubclassOf(typeof(SpriteRenderer))) tween = ((SpriteRenderer)target).DOColor(endValueColor, duration);
  122. else if (t.IsSameOrSubclassOf(typeof(Renderer))) tween = ((Renderer)target).material.DOColor(endValueColor, duration);
  123. else if (t.IsSameOrSubclassOf(typeof(Image))) tween = ((Image)target).DOColor(endValueColor, duration);
  124. else if (t.IsSameOrSubclassOf(typeof(Text))) tween = ((Text)target).DOColor(endValueColor, duration);
  125. #if DOTWEEN_TK2D
  126. else if (t.IsSameOrSubclassOf(typeof(tk2dTextMesh))) tween = ((tk2dTextMesh)target).DOColor(endValueColor, duration);
  127. else if (t.IsSameOrSubclassOf(typeof(tk2dBaseSprite))) tween = ((tk2dBaseSprite)target).DOColor(endValueColor, duration);
  128. // c = this.GetComponent<tk2dBaseSprite>();
  129. // if (c != null) {
  130. // tween = ((tk2dBaseSprite)c).DOColor(endValueColor, duration);
  131. // goto SetupTween;
  132. // }
  133. #endif
  134. #if DOTWEEN_TMP
  135. else if (t.IsSameOrSubclassOf(typeof(TextMeshProUGUI))) tween = ((TextMeshProUGUI)target).DOColor(endValueColor, duration);
  136. else if (t.IsSameOrSubclassOf(typeof(TextMeshPro))) tween = ((TextMeshPro)target).DOColor(endValueColor, duration);
  137. // c = this.GetComponent<TextMeshPro>();
  138. // if (c != null) {
  139. // tween = ((TextMeshPro)c).DOColor(endValueColor, duration);
  140. // goto SetupTween;
  141. // }
  142. // c = this.GetComponent<TextMeshProUGUI>();
  143. // if (c != null) {
  144. // tween = ((TextMeshProUGUI)c).DOColor(endValueColor, duration);
  145. // goto SetupTween;
  146. // }
  147. #endif
  148. // c = this.GetComponent<SpriteRenderer>();
  149. // if (c != null) {
  150. // tween = ((SpriteRenderer)c).DOColor(endValueColor, duration);
  151. // goto SetupTween;
  152. // }
  153. // c = this.GetComponent<Renderer>();
  154. // if (c != null) {
  155. // tween = ((Renderer)c).material.DOColor(endValueColor, duration);
  156. // goto SetupTween;
  157. // }
  158. // c = this.GetComponent<Image>();
  159. // if (c != null) {
  160. // tween = ((Image)c).DOColor(endValueColor, duration);
  161. // goto SetupTween;
  162. // }
  163. // c = this.GetComponent<Text>();
  164. // if (c != null) {
  165. // tween = ((Text)c).DOColor(endValueColor, duration);
  166. // goto SetupTween;
  167. // }
  168. break;
  169. case DOTweenAnimationType.Fade:
  170. isRelative = false;
  171. if (t.IsSameOrSubclassOf(typeof(SpriteRenderer))) tween = ((SpriteRenderer)target).DOFade(endValueFloat, duration);
  172. else if (t.IsSameOrSubclassOf(typeof(Renderer))) tween = ((Renderer)target).material.DOFade(endValueFloat, duration);
  173. else if (t.IsSameOrSubclassOf(typeof(Image))) tween = ((Image)target).DOFade(endValueFloat, duration);
  174. else if (t.IsSameOrSubclassOf(typeof(Text))) tween = ((Text)target).DOFade(endValueFloat, duration);
  175. #if DOTWEEN_TK2D
  176. else if (t.IsSameOrSubclassOf(typeof(tk2dTextMesh))) tween = ((tk2dTextMesh)target).DOFade(endValueFloat, duration);
  177. else if (t.IsSameOrSubclassOf(typeof(tk2dBaseSprite))) tween = ((tk2dBaseSprite)target).DOFade(endValueFloat, duration);
  178. // c = this.GetComponent<tk2dBaseSprite>();
  179. // if (c != null) {
  180. // tween = ((tk2dBaseSprite)c).DOFade(endValueFloat, duration);
  181. // goto SetupTween;
  182. // }
  183. #endif
  184. #if DOTWEEN_TMP
  185. else if (t.IsSameOrSubclassOf(typeof(TextMeshProUGUI))) tween = ((TextMeshProUGUI)target).DOFade(endValueFloat, duration);
  186. else if (t.IsSameOrSubclassOf(typeof(TextMeshPro))) tween = ((TextMeshPro)target).DOFade(endValueFloat, duration);
  187. // c = this.GetComponent<TextMeshPro>();
  188. // if (c != null) {
  189. // tween = ((TextMeshPro)c).DOFade(endValueFloat, duration);
  190. // goto SetupTween;
  191. // }
  192. // c = this.GetComponent<TextMeshProUGUI>();
  193. // if (c != null) {
  194. // tween = ((TextMeshProUGUI)c).DOFade(endValueFloat, duration);
  195. // goto SetupTween;
  196. // }
  197. #endif
  198. // c = this.GetComponent<SpriteRenderer>();
  199. // if (c != null) {
  200. // tween = ((SpriteRenderer)c).DOFade(endValueFloat, duration);
  201. // goto SetupTween;
  202. // }
  203. // c = this.GetComponent<Renderer>();
  204. // if (c != null) {
  205. // tween = ((Renderer)c).material.DOFade(endValueFloat, duration);
  206. // goto SetupTween;
  207. // }
  208. // c = this.GetComponent<Image>();
  209. // if (c != null) {
  210. // tween = ((Image)c).DOFade(endValueFloat, duration);
  211. // goto SetupTween;
  212. // }
  213. // c = this.GetComponent<Text>();
  214. // if (c != null) {
  215. // tween = ((Text)c).DOFade(endValueFloat, duration);
  216. // goto SetupTween;
  217. // }
  218. break;
  219. case DOTweenAnimationType.Text:
  220. if (t.IsSameOrSubclassOf(typeof(Text))) tween = ((Text)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  221. // c = this.GetComponent<Text>();
  222. // if (c != null) {
  223. // tween = ((Text)c).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  224. // goto SetupTween;
  225. // }
  226. #if DOTWEEN_TK2D
  227. else if (t.IsSameOrSubclassOf(typeof(tk2dTextMesh))) tween = ((tk2dTextMesh)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  228. // c = this.GetComponent<tk2dTextMesh>();
  229. // if (c != null) {
  230. // tween = ((tk2dTextMesh)c).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  231. // goto SetupTween;
  232. // }
  233. #endif
  234. #if DOTWEEN_TMP
  235. else if (t.IsSameOrSubclassOf(typeof(TextMeshProUGUI))) tween = ((TextMeshProUGUI)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  236. else if (t.IsSameOrSubclassOf(typeof(TextMeshPro))) tween = ((TextMeshPro)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  237. // c = this.GetComponent<TextMeshPro>();
  238. // if (c != null) {
  239. // tween = ((TextMeshPro)c).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  240. // goto SetupTween;
  241. // }
  242. // c = this.GetComponent<TextMeshProUGUI>();
  243. // if (c != null) {
  244. // tween = ((TextMeshProUGUI)c).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  245. // goto SetupTween;
  246. // }
  247. #endif
  248. break;
  249. case DOTweenAnimationType.PunchPosition:
  250. if (t.IsSameOrSubclassOf(typeof(RectTransform))) tween = ((RectTransform)target).DOPunchAnchorPos(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  251. else if (t.IsSameOrSubclassOf(typeof(Transform))) tween = ((Transform)target).DOPunchPosition(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  252. // tween = transform.DOPunchPosition(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  253. break;
  254. case DOTweenAnimationType.PunchScale:
  255. tween = transform.DOPunchScale(endValueV3, duration, optionalInt0, optionalFloat0);
  256. break;
  257. case DOTweenAnimationType.PunchRotation:
  258. tween = transform.DOPunchRotation(endValueV3, duration, optionalInt0, optionalFloat0);
  259. break;
  260. case DOTweenAnimationType.ShakePosition:
  261. if (t.IsSameOrSubclassOf(typeof(RectTransform))) tween = ((RectTransform)target).DOShakeAnchorPos(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  262. if (t.IsSameOrSubclassOf(typeof(Transform))) tween = ((Transform)target).DOShakePosition(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  263. // tween = transform.DOShakePosition(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  264. break;
  265. case DOTweenAnimationType.ShakeScale:
  266. tween = transform.DOShakeScale(duration, endValueV3, optionalInt0, optionalFloat0);
  267. break;
  268. case DOTweenAnimationType.ShakeRotation:
  269. tween = transform.DOShakeRotation(duration, endValueV3, optionalInt0, optionalFloat0);
  270. break;
  271. case DOTweenAnimationType.CameraAspect:
  272. tween = ((Camera)target).DOAspect(endValueFloat, duration);
  273. break;
  274. case DOTweenAnimationType.CameraBackgroundColor:
  275. tween = ((Camera)target).DOColor(endValueColor, duration);
  276. break;
  277. case DOTweenAnimationType.CameraFieldOfView:
  278. tween = ((Camera)target).DOFieldOfView(endValueFloat, duration);
  279. break;
  280. case DOTweenAnimationType.CameraOrthoSize:
  281. tween = ((Camera)target).DOOrthoSize(endValueFloat, duration);
  282. break;
  283. case DOTweenAnimationType.CameraPixelRect:
  284. tween = ((Camera)target).DOPixelRect(endValueRect, duration);
  285. break;
  286. case DOTweenAnimationType.CameraRect:
  287. tween = ((Camera)target).DORect(endValueRect, duration);
  288. break;
  289. }
  290. // SetupTween:
  291. if (tween == null) return;
  292. if (isFrom) {
  293. ((Tweener)tween).From(isRelative);
  294. } else {
  295. tween.SetRelative(isRelative);
  296. }
  297. tween.SetTarget(this.gameObject).SetDelay(delay).SetLoops(loops, loopType).SetAutoKill(autoKill)
  298. .OnKill(()=> tween = null);
  299. if (easeType == Ease.INTERNAL_Custom) tween.SetEase(easeCurve);
  300. else tween.SetEase(easeType);
  301. if (!string.IsNullOrEmpty(id)) tween.SetId(id);
  302. tween.SetUpdate(isIndependentUpdate);
  303. if (hasOnStart) {
  304. if (onStart != null) tween.OnStart(onStart.Invoke);
  305. } else onStart = null;
  306. if (hasOnPlay) {
  307. if (onPlay != null) tween.OnPlay(onPlay.Invoke);
  308. } else onPlay = null;
  309. if (hasOnUpdate) {
  310. if (onUpdate != null) tween.OnUpdate(onUpdate.Invoke);
  311. } else onUpdate = null;
  312. if (hasOnStepComplete) {
  313. if (onStepComplete != null) tween.OnStepComplete(onStepComplete.Invoke);
  314. } else onStepComplete = null;
  315. if (hasOnComplete) {
  316. if (onComplete != null) tween.OnComplete(onComplete.Invoke);
  317. } else onComplete = null;
  318. if (autoPlay) tween.Play();
  319. else tween.Pause();
  320. }
  321. #endregion
  322. #region Public Methods
  323. // These methods are here so they can be called directly via Unity's UGUI event system
  324. public override void DOPlay()
  325. {
  326. DOTween.Play(this.gameObject);
  327. }
  328. public override void DOPlayBackwards()
  329. {
  330. DOTween.PlayBackwards(this.gameObject);
  331. }
  332. public override void DOPlayForward()
  333. {
  334. DOTween.PlayForward(this.gameObject);
  335. }
  336. public override void DOPause()
  337. {
  338. DOTween.Pause(this.gameObject);
  339. }
  340. public override void DOTogglePause()
  341. {
  342. DOTween.TogglePause(this.gameObject);
  343. }
  344. public override void DORewind()
  345. {
  346. _playCount = -1;
  347. // Rewind using Components order (in case there are multiple animations on the same property)
  348. DOTweenAnimation[] anims = this.gameObject.GetComponents<DOTweenAnimation>();
  349. for (int i = anims.Length - 1; i > -1; --i) {
  350. Tween t = anims[i].tween;
  351. if (t != null && t.IsInitialized()) anims[i].tween.Rewind();
  352. }
  353. // DOTween.Rewind(this.gameObject);
  354. }
  355. /// <summary>
  356. /// Restarts the tween
  357. /// </summary>
  358. /// <param name="fromHere">If TRUE, re-evaluates the tween's start and end values from its current position.
  359. /// Set it to TRUE when spawning the same DOTweenAnimation in different positions (like when using a pooling system)</param>
  360. public override void DORestart(bool fromHere = false)
  361. {
  362. _playCount = -1;
  363. if (tween == null) {
  364. if (Debugger.logPriority > 1) Debugger.LogNullTween(tween); return;
  365. }
  366. if (fromHere && isRelative) ReEvaluateRelativeTween();
  367. DOTween.Restart(this.gameObject);
  368. }
  369. public override void DOComplete()
  370. {
  371. DOTween.Complete(this.gameObject);
  372. }
  373. public override void DOKill()
  374. {
  375. DOTween.Kill(this.gameObject);
  376. tween = null;
  377. }
  378. #region Specifics
  379. public void DOPlayById(string id)
  380. {
  381. DOTween.Play(this.gameObject, id);
  382. }
  383. public void DOPlayAllById(string id)
  384. {
  385. DOTween.Play(id);
  386. }
  387. public void DOPlayNext()
  388. {
  389. DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>();
  390. while (_playCount < anims.Length - 1) {
  391. _playCount++;
  392. DOTweenAnimation anim = anims[_playCount];
  393. if (anim != null && anim.tween != null && !anim.tween.IsPlaying() && !anim.tween.IsComplete()) {
  394. anim.tween.Play();
  395. break;
  396. }
  397. }
  398. }
  399. public void DORewindAndPlayNext()
  400. {
  401. _playCount = -1;
  402. DOTween.Rewind(this.gameObject);
  403. DOPlayNext();
  404. }
  405. public void DORestartById(string id)
  406. {
  407. _playCount = -1;
  408. DOTween.Restart(this.gameObject, id);
  409. }
  410. public void DORestartAllById(string id)
  411. {
  412. _playCount = -1;
  413. DOTween.Restart(id);
  414. }
  415. public List<Tween> GetTweens()
  416. {
  417. return DOTween.TweensByTarget(this.gameObject);
  418. }
  419. #endregion
  420. #endregion
  421. #region Private
  422. // Re-evaluate relative position of path
  423. void ReEvaluateRelativeTween()
  424. {
  425. if (animationType == DOTweenAnimationType.Move) {
  426. ((Tweener)tween).ChangeEndValue(transform.position + endValueV3, true);
  427. } else if (animationType == DOTweenAnimationType.LocalMove) {
  428. ((Tweener)tween).ChangeEndValue(transform.localPosition + endValueV3, true);
  429. }
  430. }
  431. #endregion
  432. }
  433. public static class DOTweenAnimationExtensions
  434. {
  435. public static bool IsSameOrSubclassOf(this Type t, Type tBase)
  436. {
  437. return t.IsSubclassOf(tBase) || t == tBase;
  438. }
  439. }
  440. }