DOTweenAnimation.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 TargetType targetType;
  37. public TargetType forcedTargetType; // Used when choosing between multiple targets
  38. public bool autoPlay = true;
  39. public bool useTargetAsV3;
  40. public float endValueFloat;
  41. public Vector3 endValueV3;
  42. public Vector2 endValueV2;
  43. public Color endValueColor = new Color(1, 1, 1, 1);
  44. public string endValueString = "";
  45. public Rect endValueRect = new Rect(0, 0, 0, 0);
  46. public Transform endValueTransform;
  47. public bool optionalBool0;
  48. public float optionalFloat0;
  49. public int optionalInt0;
  50. public RotateMode optionalRotationMode = RotateMode.Fast;
  51. public ScrambleMode optionalScrambleMode = ScrambleMode.None;
  52. public string optionalString;
  53. bool _tweenCreated; // TRUE after the tweens have been created
  54. int _playCount = -1; // Used when calling DOPlayNext
  55. #region Unity Methods
  56. void Awake()
  57. {
  58. if (!isActive || !isValid) return;
  59. if (animationType != DOTweenAnimationType.Move || !useTargetAsV3) {
  60. // Don't create tweens if we're using a RectTransform as a Move target,
  61. // because that will work only inside Start
  62. CreateTween();
  63. _tweenCreated = true;
  64. }
  65. }
  66. void Start()
  67. {
  68. if (_tweenCreated || !isActive || !isValid) return;
  69. CreateTween();
  70. _tweenCreated = true;
  71. }
  72. void OnDestroy()
  73. {
  74. if (tween != null && tween.IsActive()) tween.Kill();
  75. tween = null;
  76. }
  77. // Used also by DOTweenAnimationInspector when applying runtime changes and restarting
  78. public void CreateTween()
  79. {
  80. if (target == null) {
  81. 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);
  82. return;
  83. }
  84. if (forcedTargetType != TargetType.Unset) targetType = forcedTargetType;
  85. if (targetType == TargetType.Unset) {
  86. // Legacy DOTweenAnimation (made with a version older than 0.9.450) without stored targetType > assign it now
  87. targetType = TypeToDOTargetType(target.GetType());
  88. }
  89. switch (animationType) {
  90. case DOTweenAnimationType.None:
  91. break;
  92. case DOTweenAnimationType.Move:
  93. if (useTargetAsV3) {
  94. isRelative = false;
  95. if (endValueTransform == null) {
  96. Debug.LogWarning(string.Format("{0} :: This tween's TO target is NULL, a Vector3 of (0,0,0) will be used instead", this.gameObject.name), this.gameObject);
  97. endValueV3 = Vector3.zero;
  98. } else {
  99. if (targetType == TargetType.RectTransform) {
  100. RectTransform endValueT = endValueTransform as RectTransform;
  101. if (endValueT == null) {
  102. Debug.LogWarning(string.Format("{0} :: This tween's TO target should be a RectTransform, a Vector3 of (0,0,0) will be used instead", this.gameObject.name), this.gameObject);
  103. endValueV3 = Vector3.zero;
  104. } else {
  105. RectTransform rTarget = target as RectTransform;
  106. if (rTarget == null) {
  107. Debug.LogWarning(string.Format("{0} :: This tween's target and TO target are not of the same type. Please reassign the values", this.gameObject.name), this.gameObject);
  108. } else {
  109. // Problem: doesn't work inside Awake (ararargh!)
  110. endValueV3 = DOTweenUtils46.SwitchToRectTransform(endValueT, rTarget);
  111. }
  112. }
  113. } else endValueV3 = endValueTransform.position;
  114. }
  115. }
  116. switch (targetType) {
  117. case TargetType.RectTransform:
  118. tween = ((RectTransform)target).DOAnchorPos3D(endValueV3, duration, optionalBool0);
  119. break;
  120. case TargetType.Transform:
  121. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  122. break;
  123. case TargetType.Rigidbody2D:
  124. tween = ((Rigidbody2D)target).DOMove(endValueV3, duration, optionalBool0);
  125. break;
  126. case TargetType.Rigidbody:
  127. tween = ((Rigidbody)target).DOMove(endValueV3, duration, optionalBool0);
  128. break;
  129. }
  130. break;
  131. case DOTweenAnimationType.LocalMove:
  132. tween = transform.DOLocalMove(endValueV3, duration, optionalBool0);
  133. break;
  134. case DOTweenAnimationType.Rotate:
  135. switch (targetType) {
  136. case TargetType.Transform:
  137. tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode);
  138. break;
  139. case TargetType.Rigidbody2D:
  140. tween = ((Rigidbody2D)target).DORotate(endValueFloat, duration);
  141. break;
  142. case TargetType.Rigidbody:
  143. tween = ((Rigidbody)target).DORotate(endValueV3, duration, optionalRotationMode);
  144. break;
  145. }
  146. break;
  147. case DOTweenAnimationType.LocalRotate:
  148. tween = transform.DOLocalRotate(endValueV3, duration, optionalRotationMode);
  149. break;
  150. case DOTweenAnimationType.Scale:
  151. switch (targetType) {
  152. #if DOTWEEN_TK2D
  153. case TargetType.tk2dTextMesh:
  154. tween = ((tk2dTextMesh)target).DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  155. break;
  156. case TargetType.tk2dBaseSprite:
  157. tween = ((tk2dBaseSprite)target).DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  158. break;
  159. #endif
  160. default:
  161. tween = transform.DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  162. break;
  163. }
  164. break;
  165. case DOTweenAnimationType.UIWidthHeight:
  166. tween = ((RectTransform)target).DOSizeDelta(optionalBool0 ? new Vector2(endValueFloat, endValueFloat) : endValueV2, duration);
  167. break;
  168. case DOTweenAnimationType.Color:
  169. isRelative = false;
  170. switch (targetType) {
  171. case TargetType.SpriteRenderer:
  172. tween = ((SpriteRenderer)target).DOColor(endValueColor, duration);
  173. break;
  174. case TargetType.Renderer:
  175. tween = ((Renderer)target).material.DOColor(endValueColor, duration);
  176. break;
  177. case TargetType.Image:
  178. tween = ((Image)target).DOColor(endValueColor, duration);
  179. break;
  180. case TargetType.Text:
  181. tween = ((Text)target).DOColor(endValueColor, duration);
  182. break;
  183. case TargetType.Light:
  184. tween = ((Light)target).DOColor(endValueColor, duration);
  185. break;
  186. #if DOTWEEN_TK2D
  187. case TargetType.tk2dTextMesh:
  188. tween = ((tk2dTextMesh)target).DOColor(endValueColor, duration);
  189. break;
  190. case TargetType.tk2dBaseSprite:
  191. tween = ((tk2dBaseSprite)target).DOColor(endValueColor, duration);
  192. break;
  193. #endif
  194. #if DOTWEEN_TMP
  195. case TargetType.TextMeshProUGUI:
  196. tween = ((TextMeshProUGUI)target).DOColor(endValueColor, duration);
  197. break;
  198. case TargetType.TextMeshPro:
  199. tween = ((TextMeshPro)target).DOColor(endValueColor, duration);
  200. break;
  201. #endif
  202. }
  203. break;
  204. case DOTweenAnimationType.Fade:
  205. isRelative = false;
  206. switch (targetType) {
  207. case TargetType.SpriteRenderer:
  208. tween = ((SpriteRenderer)target).DOFade(endValueFloat, duration);
  209. break;
  210. case TargetType.Renderer:
  211. tween = ((Renderer)target).material.DOFade(endValueFloat, duration);
  212. break;
  213. case TargetType.Image:
  214. tween = ((Image)target).DOFade(endValueFloat, duration);
  215. break;
  216. case TargetType.Text:
  217. tween = ((Text)target).DOFade(endValueFloat, duration);
  218. break;
  219. case TargetType.Light:
  220. tween = ((Light)target).DOIntensity(endValueFloat, duration);
  221. break;
  222. case TargetType.CanvasGroup:
  223. tween = ((CanvasGroup)target).DOFade(endValueFloat, duration);
  224. break;
  225. #if DOTWEEN_TK2D
  226. case TargetType.tk2dTextMesh:
  227. tween = ((tk2dTextMesh)target).DOFade(endValueFloat, duration);
  228. break;
  229. case TargetType.tk2dBaseSprite:
  230. tween = ((tk2dBaseSprite)target).DOFade(endValueFloat, duration);
  231. break;
  232. #endif
  233. #if DOTWEEN_TMP
  234. case TargetType.TextMeshProUGUI:
  235. tween = ((TextMeshProUGUI)target).DOFade(endValueFloat, duration);
  236. break;
  237. case TargetType.TextMeshPro:
  238. tween = ((TextMeshPro)target).DOFade(endValueFloat, duration);
  239. break;
  240. #endif
  241. }
  242. break;
  243. case DOTweenAnimationType.Text:
  244. switch (targetType) {
  245. case TargetType.Text:
  246. tween = ((Text)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  247. break;
  248. #if DOTWEEN_TK2D
  249. case TargetType.tk2dTextMesh:
  250. tween = ((tk2dTextMesh)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  251. break;
  252. #endif
  253. #if DOTWEEN_TMP
  254. case TargetType.TextMeshProUGUI:
  255. tween = ((TextMeshProUGUI)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  256. break;
  257. case TargetType.TextMeshPro:
  258. tween = ((TextMeshPro)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  259. break;
  260. #endif
  261. }
  262. break;
  263. case DOTweenAnimationType.PunchPosition:
  264. switch (targetType) {
  265. case TargetType.RectTransform:
  266. tween = ((RectTransform)target).DOPunchAnchorPos(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  267. break;
  268. case TargetType.Transform:
  269. tween = ((Transform)target).DOPunchPosition(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  270. break;
  271. }
  272. break;
  273. case DOTweenAnimationType.PunchScale:
  274. tween = transform.DOPunchScale(endValueV3, duration, optionalInt0, optionalFloat0);
  275. break;
  276. case DOTweenAnimationType.PunchRotation:
  277. tween = transform.DOPunchRotation(endValueV3, duration, optionalInt0, optionalFloat0);
  278. break;
  279. case DOTweenAnimationType.ShakePosition:
  280. switch (targetType) {
  281. case TargetType.RectTransform:
  282. tween = ((RectTransform)target).DOShakeAnchorPos(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  283. break;
  284. case TargetType.Transform:
  285. tween = ((Transform)target).DOShakePosition(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  286. break;
  287. }
  288. break;
  289. case DOTweenAnimationType.ShakeScale:
  290. tween = transform.DOShakeScale(duration, endValueV3, optionalInt0, optionalFloat0);
  291. break;
  292. case DOTweenAnimationType.ShakeRotation:
  293. tween = transform.DOShakeRotation(duration, endValueV3, optionalInt0, optionalFloat0);
  294. break;
  295. case DOTweenAnimationType.CameraAspect:
  296. tween = ((Camera)target).DOAspect(endValueFloat, duration);
  297. break;
  298. case DOTweenAnimationType.CameraBackgroundColor:
  299. tween = ((Camera)target).DOColor(endValueColor, duration);
  300. break;
  301. case DOTweenAnimationType.CameraFieldOfView:
  302. tween = ((Camera)target).DOFieldOfView(endValueFloat, duration);
  303. break;
  304. case DOTweenAnimationType.CameraOrthoSize:
  305. tween = ((Camera)target).DOOrthoSize(endValueFloat, duration);
  306. break;
  307. case DOTweenAnimationType.CameraPixelRect:
  308. tween = ((Camera)target).DOPixelRect(endValueRect, duration);
  309. break;
  310. case DOTweenAnimationType.CameraRect:
  311. tween = ((Camera)target).DORect(endValueRect, duration);
  312. break;
  313. }
  314. if (tween == null) return;
  315. if (isFrom) {
  316. ((Tweener)tween).From(isRelative);
  317. } else {
  318. tween.SetRelative(isRelative);
  319. }
  320. tween.SetTarget(this.gameObject).SetDelay(delay).SetLoops(loops, loopType).SetAutoKill(autoKill)
  321. .OnKill(()=> tween = null);
  322. if (isSpeedBased) tween.SetSpeedBased();
  323. if (easeType == Ease.INTERNAL_Custom) tween.SetEase(easeCurve);
  324. else tween.SetEase(easeType);
  325. if (!string.IsNullOrEmpty(id)) tween.SetId(id);
  326. tween.SetUpdate(isIndependentUpdate);
  327. if (hasOnStart) {
  328. if (onStart != null) tween.OnStart(onStart.Invoke);
  329. } else onStart = null;
  330. if (hasOnPlay) {
  331. if (onPlay != null) tween.OnPlay(onPlay.Invoke);
  332. } else onPlay = null;
  333. if (hasOnUpdate) {
  334. if (onUpdate != null) tween.OnUpdate(onUpdate.Invoke);
  335. } else onUpdate = null;
  336. if (hasOnStepComplete) {
  337. if (onStepComplete != null) tween.OnStepComplete(onStepComplete.Invoke);
  338. } else onStepComplete = null;
  339. if (hasOnComplete) {
  340. if (onComplete != null) tween.OnComplete(onComplete.Invoke);
  341. } else onComplete = null;
  342. if (hasOnRewind) {
  343. if (onRewind != null) tween.OnRewind(onRewind.Invoke);
  344. } else onRewind = null;
  345. if (autoPlay) tween.Play();
  346. else tween.Pause();
  347. if (hasOnTweenCreated && onTweenCreated != null) onTweenCreated.Invoke();
  348. }
  349. #endregion
  350. #region Public Methods
  351. // These methods are here so they can be called directly via Unity's UGUI event system
  352. public override void DOPlay()
  353. {
  354. DOTween.Play(this.gameObject);
  355. }
  356. public override void DOPlayBackwards()
  357. {
  358. DOTween.PlayBackwards(this.gameObject);
  359. }
  360. public override void DOPlayForward()
  361. {
  362. DOTween.PlayForward(this.gameObject);
  363. }
  364. public override void DOPause()
  365. {
  366. DOTween.Pause(this.gameObject);
  367. }
  368. public override void DOTogglePause()
  369. {
  370. DOTween.TogglePause(this.gameObject);
  371. }
  372. public override void DORewind()
  373. {
  374. _playCount = -1;
  375. // Rewind using Components order (in case there are multiple animations on the same property)
  376. DOTweenAnimation[] anims = this.gameObject.GetComponents<DOTweenAnimation>();
  377. for (int i = anims.Length - 1; i > -1; --i) {
  378. Tween t = anims[i].tween;
  379. if (t != null && t.IsInitialized()) anims[i].tween.Rewind();
  380. }
  381. // DOTween.Rewind(this.gameObject);
  382. }
  383. /// <summary>
  384. /// Restarts the tween
  385. /// </summary>
  386. /// <param name="fromHere">If TRUE, re-evaluates the tween's start and end values from its current position.
  387. /// Set it to TRUE when spawning the same DOTweenAnimation in different positions (like when using a pooling system)</param>
  388. public override void DORestart(bool fromHere = false)
  389. {
  390. _playCount = -1;
  391. if (tween == null) {
  392. if (Debugger.logPriority > 1) Debugger.LogNullTween(tween); return;
  393. }
  394. if (fromHere && isRelative) ReEvaluateRelativeTween();
  395. DOTween.Restart(this.gameObject);
  396. }
  397. public override void DOComplete()
  398. {
  399. DOTween.Complete(this.gameObject);
  400. }
  401. public override void DOKill()
  402. {
  403. DOTween.Kill(this.gameObject);
  404. tween = null;
  405. }
  406. #region Specifics
  407. public void DOPlayById(string id)
  408. {
  409. DOTween.Play(this.gameObject, id);
  410. }
  411. public void DOPlayAllById(string id)
  412. {
  413. DOTween.Play(id);
  414. }
  415. public void DOPauseAllById(string id)
  416. {
  417. DOTween.Pause(id);
  418. }
  419. public void DOPlayBackwardsById(string id)
  420. {
  421. DOTween.PlayBackwards(this.gameObject, id);
  422. }
  423. public void DOPlayBackwardsAllById(string id)
  424. {
  425. DOTween.PlayBackwards(id);
  426. }
  427. public void DOPlayForwardById(string id)
  428. {
  429. DOTween.PlayForward(this.gameObject, id);
  430. }
  431. public void DOPlayForwardAllById(string id)
  432. {
  433. DOTween.PlayForward(id);
  434. }
  435. public void DOPlayNext()
  436. {
  437. DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>();
  438. while (_playCount < anims.Length - 1) {
  439. _playCount++;
  440. DOTweenAnimation anim = anims[_playCount];
  441. if (anim != null && anim.tween != null && !anim.tween.IsPlaying() && !anim.tween.IsComplete()) {
  442. anim.tween.Play();
  443. break;
  444. }
  445. }
  446. }
  447. public void DORewindAndPlayNext()
  448. {
  449. _playCount = -1;
  450. DOTween.Rewind(this.gameObject);
  451. DOPlayNext();
  452. }
  453. public void DORestartById(string id)
  454. {
  455. _playCount = -1;
  456. DOTween.Restart(this.gameObject, id);
  457. }
  458. public void DORestartAllById(string id)
  459. {
  460. _playCount = -1;
  461. DOTween.Restart(id);
  462. }
  463. /// <summary>
  464. /// Returns the tweens created by this DOTweenAnimation, in the same order as they appear in the Inspector (top to bottom)
  465. /// </summary>
  466. public List<Tween> GetTweens()
  467. {
  468. // return DOTween.TweensByTarget(this.gameObject);
  469. List<Tween> result = new List<Tween>();
  470. DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>();
  471. foreach (DOTweenAnimation anim in anims) result.Add(anim.tween);
  472. return result;
  473. }
  474. #endregion
  475. #region Internal Static Helpers (also used by Inspector)
  476. public static TargetType TypeToDOTargetType(Type t)
  477. {
  478. string str = t.ToString();
  479. int dotIndex = str.LastIndexOf(".");
  480. if (dotIndex != -1) str = str.Substring(dotIndex + 1);
  481. if (str.IndexOf("Renderer") != -1 && (str != "SpriteRenderer")) str = "Renderer";
  482. return (TargetType)Enum.Parse(typeof(TargetType), str);
  483. }
  484. #endregion
  485. #endregion
  486. #region Private
  487. // Re-evaluate relative position of path
  488. void ReEvaluateRelativeTween()
  489. {
  490. if (animationType == DOTweenAnimationType.Move) {
  491. ((Tweener)tween).ChangeEndValue(transform.position + endValueV3, true);
  492. } else if (animationType == DOTweenAnimationType.LocalMove) {
  493. ((Tweener)tween).ChangeEndValue(transform.localPosition + endValueV3, true);
  494. }
  495. }
  496. #endregion
  497. }
  498. public static class DOTweenAnimationExtensions
  499. {
  500. // // Doesn't work on Win 8.1
  501. // public static bool IsSameOrSubclassOf(this Type t, Type tBase)
  502. // {
  503. // return t.IsSubclassOf(tBase) || t == tBase;
  504. // }
  505. public static bool IsSameOrSubclassOf<T>(this Component t)
  506. {
  507. return t is T;
  508. }
  509. }
  510. }