DOTweenAnimation.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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) 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. tween = transform.DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  152. break;
  153. case DOTweenAnimationType.UIWidthHeight:
  154. tween = ((RectTransform)target).DOSizeDelta(optionalBool0 ? new Vector2(endValueFloat, endValueFloat) : endValueV2, duration);
  155. break;
  156. case DOTweenAnimationType.Color:
  157. isRelative = false;
  158. switch (targetType) {
  159. case TargetType.SpriteRenderer:
  160. tween = ((SpriteRenderer)target).DOColor(endValueColor, duration);
  161. break;
  162. case TargetType.Renderer:
  163. tween = ((Renderer)target).material.DOColor(endValueColor, duration);
  164. break;
  165. case TargetType.Image:
  166. tween = ((Image)target).DOColor(endValueColor, duration);
  167. break;
  168. case TargetType.Text:
  169. tween = ((Text)target).DOColor(endValueColor, duration);
  170. break;
  171. case TargetType.Light:
  172. tween = ((Light)target).DOColor(endValueColor, duration);
  173. break;
  174. #if DOTWEEN_TK2D
  175. case TargetType.tk2dTextMesh:
  176. tween = ((tk2dTextMesh)target).DOColor(endValueColor, duration);
  177. break;
  178. case TargetType.tk2dBaseSprite:
  179. tween = ((tk2dBaseSprite)target).DOColor(endValueColor, duration);
  180. break;
  181. #endif
  182. #if DOTWEEN_TMP
  183. case TargetType.TextMeshProUGUI:
  184. tween = ((TextMeshProUGUI)target).DOColor(endValueColor, duration);
  185. break;
  186. case TargetType.TextMeshPro:
  187. tween = ((TextMeshPro)target).DOColor(endValueColor, duration);
  188. break;
  189. #endif
  190. }
  191. break;
  192. case DOTweenAnimationType.Fade:
  193. isRelative = false;
  194. switch (targetType) {
  195. case TargetType.SpriteRenderer:
  196. tween = ((SpriteRenderer)target).DOFade(endValueFloat, duration);
  197. break;
  198. case TargetType.Renderer:
  199. tween = ((Renderer)target).material.DOFade(endValueFloat, duration);
  200. break;
  201. case TargetType.Image:
  202. tween = ((Image)target).DOFade(endValueFloat, duration);
  203. break;
  204. case TargetType.Text:
  205. tween = ((Text)target).DOFade(endValueFloat, duration);
  206. break;
  207. case TargetType.Light:
  208. tween = ((Light)target).DOIntensity(endValueFloat, duration);
  209. break;
  210. case TargetType.CanvasGroup:
  211. tween = ((CanvasGroup)target).DOFade(endValueFloat, duration);
  212. break;
  213. #if DOTWEEN_TK2D
  214. case TargetType.tk2dTextMesh:
  215. tween = ((tk2dTextMesh)target).DOFade(endValueFloat, duration);
  216. break;
  217. case TargetType.tk2dBaseSprite:
  218. tween = ((tk2dBaseSprite)target).DOFade(endValueFloat, duration);
  219. break;
  220. #endif
  221. #if DOTWEEN_TMP
  222. case TargetType.TextMeshProUGUI:
  223. tween = ((TextMeshProUGUI)target).DOFade(endValueFloat, duration);
  224. break;
  225. case TargetType.TextMeshPro:
  226. tween = ((TextMeshPro)target).DOFade(endValueFloat, duration);
  227. break;
  228. #endif
  229. }
  230. break;
  231. case DOTweenAnimationType.Text:
  232. switch (targetType) {
  233. case TargetType.Text:
  234. tween = ((Text)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  235. break;
  236. #if DOTWEEN_TK2D
  237. case TargetType.tk2dTextMesh:
  238. tween = ((tk2dTextMesh)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  239. break;
  240. #endif
  241. #if DOTWEEN_TMP
  242. case TargetType.TextMeshProUGUI:
  243. tween = ((TextMeshProUGUI)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  244. break;
  245. case TargetType.TextMeshPro:
  246. tween = ((TextMeshPro)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  247. break;
  248. #endif
  249. }
  250. break;
  251. case DOTweenAnimationType.PunchPosition:
  252. switch (targetType) {
  253. case TargetType.RectTransform:
  254. tween = ((RectTransform)target).DOPunchAnchorPos(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  255. break;
  256. case TargetType.Transform:
  257. tween = ((Transform)target).DOPunchPosition(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  258. break;
  259. }
  260. break;
  261. case DOTweenAnimationType.PunchScale:
  262. tween = transform.DOPunchScale(endValueV3, duration, optionalInt0, optionalFloat0);
  263. break;
  264. case DOTweenAnimationType.PunchRotation:
  265. tween = transform.DOPunchRotation(endValueV3, duration, optionalInt0, optionalFloat0);
  266. break;
  267. case DOTweenAnimationType.ShakePosition:
  268. switch (targetType) {
  269. case TargetType.RectTransform:
  270. tween = ((RectTransform)target).DOShakeAnchorPos(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  271. break;
  272. case TargetType.Transform:
  273. tween = ((Transform)target).DOShakePosition(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  274. break;
  275. }
  276. break;
  277. case DOTweenAnimationType.ShakeScale:
  278. tween = transform.DOShakeScale(duration, endValueV3, optionalInt0, optionalFloat0);
  279. break;
  280. case DOTweenAnimationType.ShakeRotation:
  281. tween = transform.DOShakeRotation(duration, endValueV3, optionalInt0, optionalFloat0);
  282. break;
  283. case DOTweenAnimationType.CameraAspect:
  284. tween = ((Camera)target).DOAspect(endValueFloat, duration);
  285. break;
  286. case DOTweenAnimationType.CameraBackgroundColor:
  287. tween = ((Camera)target).DOColor(endValueColor, duration);
  288. break;
  289. case DOTweenAnimationType.CameraFieldOfView:
  290. tween = ((Camera)target).DOFieldOfView(endValueFloat, duration);
  291. break;
  292. case DOTweenAnimationType.CameraOrthoSize:
  293. tween = ((Camera)target).DOOrthoSize(endValueFloat, duration);
  294. break;
  295. case DOTweenAnimationType.CameraPixelRect:
  296. tween = ((Camera)target).DOPixelRect(endValueRect, duration);
  297. break;
  298. case DOTweenAnimationType.CameraRect:
  299. tween = ((Camera)target).DORect(endValueRect, duration);
  300. break;
  301. }
  302. if (tween == null) return;
  303. if (isFrom) {
  304. ((Tweener)tween).From(isRelative);
  305. } else {
  306. tween.SetRelative(isRelative);
  307. }
  308. tween.SetTarget(this.gameObject).SetDelay(delay).SetLoops(loops, loopType).SetAutoKill(autoKill)
  309. .OnKill(()=> tween = null);
  310. if (isSpeedBased) tween.SetSpeedBased();
  311. if (easeType == Ease.INTERNAL_Custom) tween.SetEase(easeCurve);
  312. else tween.SetEase(easeType);
  313. if (!string.IsNullOrEmpty(id)) tween.SetId(id);
  314. tween.SetUpdate(isIndependentUpdate);
  315. if (hasOnStart) {
  316. if (onStart != null) tween.OnStart(onStart.Invoke);
  317. } else onStart = null;
  318. if (hasOnPlay) {
  319. if (onPlay != null) tween.OnPlay(onPlay.Invoke);
  320. } else onPlay = null;
  321. if (hasOnUpdate) {
  322. if (onUpdate != null) tween.OnUpdate(onUpdate.Invoke);
  323. } else onUpdate = null;
  324. if (hasOnStepComplete) {
  325. if (onStepComplete != null) tween.OnStepComplete(onStepComplete.Invoke);
  326. } else onStepComplete = null;
  327. if (hasOnComplete) {
  328. if (onComplete != null) tween.OnComplete(onComplete.Invoke);
  329. } else onComplete = null;
  330. if (autoPlay) tween.Play();
  331. else tween.Pause();
  332. if (hasOnTweenCreated && onTweenCreated != null) onTweenCreated.Invoke();
  333. }
  334. #endregion
  335. #region Public Methods
  336. // These methods are here so they can be called directly via Unity's UGUI event system
  337. public override void DOPlay()
  338. {
  339. DOTween.Play(this.gameObject);
  340. }
  341. public override void DOPlayBackwards()
  342. {
  343. DOTween.PlayBackwards(this.gameObject);
  344. }
  345. public override void DOPlayForward()
  346. {
  347. DOTween.PlayForward(this.gameObject);
  348. }
  349. public override void DOPause()
  350. {
  351. DOTween.Pause(this.gameObject);
  352. }
  353. public override void DOTogglePause()
  354. {
  355. DOTween.TogglePause(this.gameObject);
  356. }
  357. public override void DORewind()
  358. {
  359. _playCount = -1;
  360. // Rewind using Components order (in case there are multiple animations on the same property)
  361. DOTweenAnimation[] anims = this.gameObject.GetComponents<DOTweenAnimation>();
  362. for (int i = anims.Length - 1; i > -1; --i) {
  363. Tween t = anims[i].tween;
  364. if (t != null && t.IsInitialized()) anims[i].tween.Rewind();
  365. }
  366. // DOTween.Rewind(this.gameObject);
  367. }
  368. /// <summary>
  369. /// Restarts the tween
  370. /// </summary>
  371. /// <param name="fromHere">If TRUE, re-evaluates the tween's start and end values from its current position.
  372. /// Set it to TRUE when spawning the same DOTweenAnimation in different positions (like when using a pooling system)</param>
  373. public override void DORestart(bool fromHere = false)
  374. {
  375. _playCount = -1;
  376. if (tween == null) {
  377. if (Debugger.logPriority > 1) Debugger.LogNullTween(tween); return;
  378. }
  379. if (fromHere && isRelative) ReEvaluateRelativeTween();
  380. DOTween.Restart(this.gameObject);
  381. }
  382. public override void DOComplete()
  383. {
  384. DOTween.Complete(this.gameObject);
  385. }
  386. public override void DOKill()
  387. {
  388. DOTween.Kill(this.gameObject);
  389. tween = null;
  390. }
  391. #region Specifics
  392. public void DOPlayById(string id)
  393. {
  394. DOTween.Play(this.gameObject, id);
  395. }
  396. public void DOPlayAllById(string id)
  397. {
  398. DOTween.Play(id);
  399. }
  400. public void DOPlayNext()
  401. {
  402. DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>();
  403. while (_playCount < anims.Length - 1) {
  404. _playCount++;
  405. DOTweenAnimation anim = anims[_playCount];
  406. if (anim != null && anim.tween != null && !anim.tween.IsPlaying() && !anim.tween.IsComplete()) {
  407. anim.tween.Play();
  408. break;
  409. }
  410. }
  411. }
  412. public void DORewindAndPlayNext()
  413. {
  414. _playCount = -1;
  415. DOTween.Rewind(this.gameObject);
  416. DOPlayNext();
  417. }
  418. public void DORestartById(string id)
  419. {
  420. _playCount = -1;
  421. DOTween.Restart(this.gameObject, id);
  422. }
  423. public void DORestartAllById(string id)
  424. {
  425. _playCount = -1;
  426. DOTween.Restart(id);
  427. }
  428. public List<Tween> GetTweens()
  429. {
  430. return DOTween.TweensByTarget(this.gameObject);
  431. }
  432. #endregion
  433. #region Internal Static Helpers (also used by Inspector)
  434. public static TargetType TypeToDOTargetType(Type t)
  435. {
  436. string str = t.ToString();
  437. int dotIndex = str.LastIndexOf(".");
  438. if (dotIndex != -1) str = str.Substring(dotIndex + 1);
  439. if (str.IndexOf("Renderer") != -1 && (str != "SpriteRenderer")) str = "Renderer";
  440. return (TargetType)Enum.Parse(typeof(TargetType), str);
  441. }
  442. #endregion
  443. #endregion
  444. #region Private
  445. // Re-evaluate relative position of path
  446. void ReEvaluateRelativeTween()
  447. {
  448. if (animationType == DOTweenAnimationType.Move) {
  449. ((Tweener)tween).ChangeEndValue(transform.position + endValueV3, true);
  450. } else if (animationType == DOTweenAnimationType.LocalMove) {
  451. ((Tweener)tween).ChangeEndValue(transform.localPosition + endValueV3, true);
  452. }
  453. }
  454. #endregion
  455. }
  456. public static class DOTweenAnimationExtensions
  457. {
  458. // // Doesn't work on Win 8.1
  459. // public static bool IsSameOrSubclassOf(this Type t, Type tBase)
  460. // {
  461. // return t.IsSubclassOf(tBase) || t == tBase;
  462. // }
  463. public static bool IsSameOrSubclassOf<T>(this Component t)
  464. {
  465. return t is T;
  466. }
  467. }
  468. }