DOTweenAnimation.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. #if true // UI_MARKER
  8. using UnityEngine.UI;
  9. #endif
  10. #if true // TEXTMESHPRO_MARKER
  11. using TMPro;
  12. #endif
  13. #pragma warning disable 1591
  14. namespace DG.Tweening
  15. {
  16. /// <summary>
  17. /// Attach this to a GameObject to create a tween
  18. /// </summary>
  19. [AddComponentMenu("DOTween/DOTween Animation")]
  20. public class DOTweenAnimation : ABSAnimationComponent
  21. {
  22. public enum AnimationType
  23. {
  24. None,
  25. Move, LocalMove,
  26. Rotate, LocalRotate,
  27. Scale,
  28. Color, Fade,
  29. Text,
  30. PunchPosition, PunchRotation, PunchScale,
  31. ShakePosition, ShakeRotation, ShakeScale,
  32. CameraAspect, CameraBackgroundColor, CameraFieldOfView, CameraOrthoSize, CameraPixelRect, CameraRect,
  33. UIWidthHeight,
  34. FillAmount
  35. }
  36. public enum TargetType
  37. {
  38. Unset,
  39. Camera,
  40. CanvasGroup,
  41. Image,
  42. Light,
  43. RectTransform,
  44. Renderer, SpriteRenderer,
  45. Rigidbody, Rigidbody2D,
  46. Text,
  47. Transform,
  48. tk2dBaseSprite,
  49. tk2dTextMesh,
  50. TextMeshPro,
  51. TextMeshProUGUI
  52. }
  53. #region EVENTS - EDITOR-ONLY
  54. /// <summary>Used internally by the editor</summary>
  55. public static event Action<DOTweenAnimation> OnReset;
  56. static void Dispatch_OnReset(DOTweenAnimation anim) { if (OnReset != null) OnReset(anim); }
  57. #endregion
  58. public bool targetIsSelf = true; // If FALSE allows to set the target manually
  59. public GameObject targetGO = null; // Used in case targetIsSelf is FALSE
  60. // If FALSE always uses the GO containing this DOTweenAnimation (and not the one containing the target) as DOTween's SetTarget target
  61. public bool tweenTargetIsTargetGO = true;
  62. public float delay;
  63. public float duration = 1;
  64. public Ease easeType = Ease.OutQuad;
  65. public AnimationCurve easeCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
  66. public LoopType loopType = LoopType.Restart;
  67. public int loops = 1;
  68. public string id = "";
  69. public bool isRelative;
  70. public bool isFrom;
  71. public bool isIndependentUpdate = false;
  72. public bool autoKill = true;
  73. public bool autoGenerate = true; // If TRUE automatically creates the tween at startup
  74. public bool isActive = true;
  75. public bool isValid;
  76. public Component target;
  77. public AnimationType animationType;
  78. public TargetType targetType;
  79. public TargetType forcedTargetType; // Used when choosing between multiple targets
  80. public bool autoPlay = true;
  81. public bool useTargetAsV3;
  82. public float endValueFloat;
  83. public Vector3 endValueV3;
  84. public Vector2 endValueV2;
  85. public Color endValueColor = new Color(1, 1, 1, 1);
  86. public string endValueString = "";
  87. public Rect endValueRect = new Rect(0, 0, 0, 0);
  88. public Transform endValueTransform;
  89. public bool optionalBool0, optionalBool1;
  90. public float optionalFloat0;
  91. public int optionalInt0;
  92. public RotateMode optionalRotationMode = RotateMode.Fast;
  93. public ScrambleMode optionalScrambleMode = ScrambleMode.None;
  94. public ShakeRandomnessMode optionalShakeRandomnessMode = ShakeRandomnessMode.Full;
  95. public string optionalString;
  96. bool _tweenAutoGenerationCalled; // TRUE after the tweens have been autoGenerated
  97. int _playCount = -1; // Used when calling DOPlayNext
  98. readonly List<Tween> _tmpTweens = new List<Tween>();
  99. #region Unity Methods
  100. void Awake()
  101. {
  102. if (!isActive || !autoGenerate) return;
  103. if (animationType != AnimationType.Move || !useTargetAsV3) {
  104. // Don't create tweens if we're using a RectTransform as a Move target,
  105. // because that will work only inside Start
  106. CreateTween(false, autoPlay);
  107. _tweenAutoGenerationCalled = true;
  108. }
  109. }
  110. void Start()
  111. {
  112. if (_tweenAutoGenerationCalled || !isActive || !autoGenerate) return;
  113. CreateTween(false, autoPlay);
  114. _tweenAutoGenerationCalled = true;
  115. }
  116. void Reset()
  117. {
  118. Dispatch_OnReset(this);
  119. }
  120. void OnDestroy()
  121. {
  122. if (tween != null && tween.active) tween.Kill();
  123. tween = null;
  124. }
  125. /// <summary>
  126. /// Creates/recreates the tween without playing it, but first rewinding and killing the existing one if present.
  127. /// </summary>
  128. public void RewindThenRecreateTween()
  129. {
  130. if (tween != null && tween.active) tween.Rewind();
  131. CreateTween(true, false);
  132. }
  133. /// <summary>
  134. /// Creates/recreates the tween and plays it, first rewinding and killing the existing one if present.
  135. /// </summary>
  136. public void RewindThenRecreateTweenAndPlay()
  137. {
  138. if (tween != null && tween.active) tween.Rewind();
  139. CreateTween(true, true);
  140. }
  141. /// <summary>
  142. /// Creates/recreates the tween from its target's current value without playing it, but first killing the existing one if present.
  143. /// </summary>
  144. public void RecreateTween()
  145. { CreateTween(true, false); }
  146. /// <summary>
  147. /// Creates/recreates the tween from its target's current value and plays it, first killing the existing one if present.
  148. /// </summary>
  149. public void RecreateTweenAndPlay()
  150. { CreateTween(true, true); }
  151. // Used also by DOTweenAnimationInspector when applying runtime changes and restarting
  152. /// <summary>
  153. /// Creates the tween manually (called automatically if AutoGenerate is set in the Inspector)
  154. /// from its target's current value.
  155. /// </summary>
  156. /// <param name="regenerateIfExists">If TRUE and an existing tween was already created (and not killed), kills it and recreates it with the current
  157. /// parameters. Otherwise, if a tween already exists, does nothing.</param>
  158. /// <param name="andPlay">If TRUE also plays the tween, otherwise only creates it</param>
  159. public void CreateTween(bool regenerateIfExists = false, bool andPlay = true)
  160. {
  161. if (!isValid) {
  162. if (regenerateIfExists) { // Called manually: warn users
  163. Debug.LogWarning(string.Format("{0} :: This DOTweenAnimation isn't valid and its tween won't be created", this.gameObject.name), this.gameObject);
  164. }
  165. return;
  166. }
  167. if (tween != null) {
  168. if (tween.active) {
  169. if (regenerateIfExists) tween.Kill();
  170. else return;
  171. }
  172. tween = null;
  173. }
  174. // if (target == null) {
  175. // Debug.LogWarning(string.Format("{0} :: This DOTweenAnimation'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);
  176. // return;
  177. // }
  178. GameObject tweenGO = GetTweenGO();
  179. if (target == null || tweenGO == null) {
  180. if (targetIsSelf && target == null) {
  181. // Old error caused during upgrade from DOTween Pro 0.9.255
  182. Debug.LogWarning(string.Format("{0} :: This DOTweenAnimation'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);
  183. } else {
  184. // Missing non-self target
  185. Debug.LogWarning(string.Format("{0} :: This DOTweenAnimation's target/GameObject is unset: the tween will not be created.", this.gameObject.name), this.gameObject);
  186. }
  187. return;
  188. }
  189. if (forcedTargetType != TargetType.Unset) targetType = forcedTargetType;
  190. if (targetType == TargetType.Unset) {
  191. // Legacy DOTweenAnimation (made with a version older than 0.9.450) without stored targetType > assign it now
  192. targetType = TypeToDOTargetType(target.GetType());
  193. }
  194. switch (animationType) {
  195. case AnimationType.None:
  196. break;
  197. case AnimationType.Move:
  198. if (useTargetAsV3) {
  199. isRelative = false;
  200. if (endValueTransform == null) {
  201. 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);
  202. endValueV3 = Vector3.zero;
  203. } else {
  204. #if true // UI_MARKER
  205. if (targetType == TargetType.RectTransform) {
  206. RectTransform endValueT = endValueTransform as RectTransform;
  207. if (endValueT == null) {
  208. 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);
  209. endValueV3 = Vector3.zero;
  210. } else {
  211. RectTransform rTarget = target as RectTransform;
  212. if (rTarget == null) {
  213. 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);
  214. } else {
  215. // Problem: doesn't work inside Awake (ararargh!)
  216. endValueV3 = DOTweenModuleUI.Utils.SwitchToRectTransform(endValueT, rTarget);
  217. }
  218. }
  219. } else
  220. #endif
  221. endValueV3 = endValueTransform.position;
  222. }
  223. }
  224. switch (targetType) {
  225. case TargetType.Transform:
  226. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  227. break;
  228. case TargetType.RectTransform:
  229. #if true // UI_MARKER
  230. tween = ((RectTransform)target).DOAnchorPos3D(endValueV3, duration, optionalBool0);
  231. #else
  232. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  233. #endif
  234. break;
  235. case TargetType.Rigidbody:
  236. #if true // PHYSICS_MARKER
  237. tween = ((Rigidbody)target).DOMove(endValueV3, duration, optionalBool0);
  238. #else
  239. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  240. #endif
  241. break;
  242. case TargetType.Rigidbody2D:
  243. #if true // PHYSICS2D_MARKER
  244. tween = ((Rigidbody2D)target).DOMove(endValueV3, duration, optionalBool0);
  245. #else
  246. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  247. #endif
  248. break;
  249. }
  250. break;
  251. case AnimationType.LocalMove:
  252. tween = tweenGO.transform.DOLocalMove(endValueV3, duration, optionalBool0);
  253. break;
  254. case AnimationType.Rotate:
  255. switch (targetType) {
  256. case TargetType.Transform:
  257. tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode);
  258. break;
  259. case TargetType.Rigidbody:
  260. #if true // PHYSICS_MARKER
  261. tween = ((Rigidbody)target).DORotate(endValueV3, duration, optionalRotationMode);
  262. #else
  263. tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode);
  264. #endif
  265. break;
  266. case TargetType.Rigidbody2D:
  267. #if true // PHYSICS2D_MARKER
  268. tween = ((Rigidbody2D)target).DORotate(endValueFloat, duration);
  269. #else
  270. tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode);
  271. #endif
  272. break;
  273. }
  274. break;
  275. case AnimationType.LocalRotate:
  276. tween = tweenGO.transform.DOLocalRotate(endValueV3, duration, optionalRotationMode);
  277. break;
  278. case AnimationType.Scale:
  279. switch (targetType) {
  280. #if false // TK2D_MARKER
  281. case TargetType.tk2dTextMesh:
  282. tween = ((tk2dTextMesh)target).DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  283. break;
  284. case TargetType.tk2dBaseSprite:
  285. tween = ((tk2dBaseSprite)target).DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  286. break;
  287. #endif
  288. default:
  289. tween = tweenGO.transform.DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  290. break;
  291. }
  292. break;
  293. #if true // UI_MARKER
  294. case AnimationType.UIWidthHeight:
  295. tween = ((RectTransform)target).DOSizeDelta(optionalBool0 ? new Vector2(endValueFloat, endValueFloat) : endValueV2, duration);
  296. break;
  297. case AnimationType.FillAmount:
  298. tween = ((Image)target).DOFillAmount(endValueFloat, duration);
  299. break;
  300. #endif
  301. case AnimationType.Color:
  302. isRelative = false;
  303. switch (targetType) {
  304. case TargetType.Renderer:
  305. tween = ((Renderer)target).material.DOColor(endValueColor, duration);
  306. break;
  307. case TargetType.Light:
  308. tween = ((Light)target).DOColor(endValueColor, duration);
  309. break;
  310. #if true // SPRITE_MARKER
  311. case TargetType.SpriteRenderer:
  312. tween = ((SpriteRenderer)target).DOColor(endValueColor, duration);
  313. break;
  314. #endif
  315. #if true // UI_MARKER
  316. case TargetType.Image:
  317. tween = ((Graphic)target).DOColor(endValueColor, duration);
  318. break;
  319. case TargetType.Text:
  320. tween = ((Text)target).DOColor(endValueColor, duration);
  321. break;
  322. #endif
  323. #if false // TK2D_MARKER
  324. case TargetType.tk2dTextMesh:
  325. tween = ((tk2dTextMesh)target).DOColor(endValueColor, duration);
  326. break;
  327. case TargetType.tk2dBaseSprite:
  328. tween = ((tk2dBaseSprite)target).DOColor(endValueColor, duration);
  329. break;
  330. #endif
  331. #if true // TEXTMESHPRO_MARKER
  332. case TargetType.TextMeshProUGUI:
  333. tween = ((TextMeshProUGUI)target).DOColor(endValueColor, duration);
  334. break;
  335. case TargetType.TextMeshPro:
  336. tween = ((TextMeshPro)target).DOColor(endValueColor, duration);
  337. break;
  338. #endif
  339. }
  340. break;
  341. case AnimationType.Fade:
  342. isRelative = false;
  343. switch (targetType) {
  344. case TargetType.Renderer:
  345. tween = ((Renderer)target).material.DOFade(endValueFloat, duration);
  346. break;
  347. case TargetType.Light:
  348. tween = ((Light)target).DOIntensity(endValueFloat, duration);
  349. break;
  350. #if true // SPRITE_MARKER
  351. case TargetType.SpriteRenderer:
  352. tween = ((SpriteRenderer)target).DOFade(endValueFloat, duration);
  353. break;
  354. #endif
  355. #if true // UI_MARKER
  356. case TargetType.Image:
  357. tween = ((Graphic)target).DOFade(endValueFloat, duration);
  358. break;
  359. case TargetType.Text:
  360. tween = ((Text)target).DOFade(endValueFloat, duration);
  361. break;
  362. case TargetType.CanvasGroup:
  363. tween = ((CanvasGroup)target).DOFade(endValueFloat, duration);
  364. break;
  365. #endif
  366. #if false // TK2D_MARKER
  367. case TargetType.tk2dTextMesh:
  368. tween = ((tk2dTextMesh)target).DOFade(endValueFloat, duration);
  369. break;
  370. case TargetType.tk2dBaseSprite:
  371. tween = ((tk2dBaseSprite)target).DOFade(endValueFloat, duration);
  372. break;
  373. #endif
  374. #if true // TEXTMESHPRO_MARKER
  375. case TargetType.TextMeshProUGUI:
  376. tween = ((TextMeshProUGUI)target).DOFade(endValueFloat, duration);
  377. break;
  378. case TargetType.TextMeshPro:
  379. tween = ((TextMeshPro)target).DOFade(endValueFloat, duration);
  380. break;
  381. #endif
  382. }
  383. break;
  384. case AnimationType.Text:
  385. #if true // UI_MARKER
  386. switch (targetType) {
  387. case TargetType.Text:
  388. tween = ((Text)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  389. break;
  390. }
  391. #endif
  392. #if false // TK2D_MARKER
  393. switch (targetType) {
  394. case TargetType.tk2dTextMesh:
  395. tween = ((tk2dTextMesh)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  396. break;
  397. }
  398. #endif
  399. #if true // TEXTMESHPRO_MARKER
  400. switch (targetType) {
  401. case TargetType.TextMeshProUGUI:
  402. tween = ((TextMeshProUGUI)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  403. break;
  404. case TargetType.TextMeshPro:
  405. tween = ((TextMeshPro)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  406. break;
  407. }
  408. #endif
  409. break;
  410. case AnimationType.PunchPosition:
  411. switch (targetType) {
  412. case TargetType.Transform:
  413. tween = ((Transform)target).DOPunchPosition(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  414. break;
  415. #if true // UI_MARKER
  416. case TargetType.RectTransform:
  417. tween = ((RectTransform)target).DOPunchAnchorPos(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  418. break;
  419. #endif
  420. }
  421. break;
  422. case AnimationType.PunchScale:
  423. tween = tweenGO.transform.DOPunchScale(endValueV3, duration, optionalInt0, optionalFloat0);
  424. break;
  425. case AnimationType.PunchRotation:
  426. tween = tweenGO.transform.DOPunchRotation(endValueV3, duration, optionalInt0, optionalFloat0);
  427. break;
  428. case AnimationType.ShakePosition:
  429. switch (targetType) {
  430. case TargetType.Transform:
  431. tween = ((Transform)target).DOShakePosition(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0, optionalBool1, optionalShakeRandomnessMode);
  432. break;
  433. #if true // UI_MARKER
  434. case TargetType.RectTransform:
  435. tween = ((RectTransform)target).DOShakeAnchorPos(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0, optionalBool1, optionalShakeRandomnessMode);
  436. break;
  437. #endif
  438. }
  439. break;
  440. case AnimationType.ShakeScale:
  441. tween = tweenGO.transform.DOShakeScale(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool1, optionalShakeRandomnessMode);
  442. break;
  443. case AnimationType.ShakeRotation:
  444. tween = tweenGO.transform.DOShakeRotation(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool1, optionalShakeRandomnessMode);
  445. break;
  446. case AnimationType.CameraAspect:
  447. tween = ((Camera)target).DOAspect(endValueFloat, duration);
  448. break;
  449. case AnimationType.CameraBackgroundColor:
  450. tween = ((Camera)target).DOColor(endValueColor, duration);
  451. break;
  452. case AnimationType.CameraFieldOfView:
  453. tween = ((Camera)target).DOFieldOfView(endValueFloat, duration);
  454. break;
  455. case AnimationType.CameraOrthoSize:
  456. tween = ((Camera)target).DOOrthoSize(endValueFloat, duration);
  457. break;
  458. case AnimationType.CameraPixelRect:
  459. tween = ((Camera)target).DOPixelRect(endValueRect, duration);
  460. break;
  461. case AnimationType.CameraRect:
  462. tween = ((Camera)target).DORect(endValueRect, duration);
  463. break;
  464. }
  465. if (tween == null) return;
  466. // Created
  467. if (isFrom) {
  468. ((Tweener)tween).From(isRelative);
  469. } else {
  470. tween.SetRelative(isRelative);
  471. }
  472. GameObject setTarget = GetTweenTarget();
  473. tween.SetTarget(setTarget).SetDelay(delay).SetLoops(loops, loopType).SetAutoKill(autoKill)
  474. .OnKill(()=> tween = null);
  475. if (isSpeedBased) tween.SetSpeedBased();
  476. if (easeType == Ease.INTERNAL_Custom) tween.SetEase(easeCurve);
  477. else tween.SetEase(easeType);
  478. if (!string.IsNullOrEmpty(id)) tween.SetId(id);
  479. tween.SetUpdate(isIndependentUpdate);
  480. if (hasOnStart) {
  481. if (onStart != null) tween.OnStart(onStart.Invoke);
  482. } else onStart = null;
  483. if (hasOnPlay) {
  484. if (onPlay != null) tween.OnPlay(onPlay.Invoke);
  485. } else onPlay = null;
  486. if (hasOnUpdate) {
  487. if (onUpdate != null) tween.OnUpdate(onUpdate.Invoke);
  488. } else onUpdate = null;
  489. if (hasOnStepComplete) {
  490. if (onStepComplete != null) tween.OnStepComplete(onStepComplete.Invoke);
  491. } else onStepComplete = null;
  492. if (hasOnComplete) {
  493. if (onComplete != null) tween.OnComplete(onComplete.Invoke);
  494. } else onComplete = null;
  495. if (hasOnRewind) {
  496. if (onRewind != null) tween.OnRewind(onRewind.Invoke);
  497. } else onRewind = null;
  498. if (andPlay) tween.Play();
  499. else tween.Pause();
  500. if (hasOnTweenCreated && onTweenCreated != null) onTweenCreated.Invoke();
  501. }
  502. #endregion
  503. #region Public Methods
  504. #region Special
  505. /// <summary>
  506. /// Returns the tweens (if generated and not killed) created by all DOTweenAnimations on this gameObject,
  507. /// in the same order as they appear in the Inspector (top to bottom).<para/>
  508. /// Note that a tween is generated inside the Awake call (except RectTransform tweens which are generated inside Start),
  509. /// so this method won't return them before that
  510. /// </summary>
  511. public List<Tween> GetTweens()
  512. {
  513. List<Tween> result = new List<Tween>();
  514. DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>();
  515. foreach (DOTweenAnimation anim in anims) {
  516. if (anim.tween != null && anim.tween.active) result.Add(anim.tween);
  517. }
  518. return result;
  519. }
  520. /// <summary>
  521. /// Sets the animation target (which must be of the same type of the one set in the Inspector).
  522. /// This is useful if you want to change it BEFORE this <see cref="DOTweenAnimation"/>
  523. /// creates a tween, while after that it won't have any effect.<para/>
  524. /// Consider that a <see cref="DOTweenAnimation"/> creates its tween inside its Awake (except for special tweens),
  525. /// so you will need to sure your code runs before this object's Awake (via ScriptExecutionOrder or enabling/disabling methods)
  526. /// </summary>
  527. /// <param name="tweenTarget">
  528. /// New target for the animation (must be of the same type of the previous one)</param>
  529. /// <param name="useTweenTargetGameObjectForGroupOperations">If TRUE also uses tweenTarget's gameObject when settings the target-ID of the tween
  530. /// (which is used with DOPlay/DORestart/etc to apply the same operation on all tweens that have the same target-id).<para/>
  531. /// You should usually leave this to TRUE if you change the target.
  532. /// </param>
  533. public void SetAnimationTarget(Component tweenTarget, bool useTweenTargetGameObjectForGroupOperations = true)
  534. {
  535. TargetType newTargetType = TypeToDOTargetType(target.GetType());
  536. if (newTargetType != targetType) {
  537. Debug.LogError("DOTweenAnimation ► SetAnimationTarget: the new target is of a different type from the one set in the Inspector");
  538. return;
  539. }
  540. target = tweenTarget;
  541. targetGO = target.gameObject;
  542. tweenTargetIsTargetGO = useTweenTargetGameObjectForGroupOperations;
  543. }
  544. #endregion
  545. /// <summary>
  546. /// Plays all tweens whose target-id is the same as the one set by this animation
  547. /// </summary>
  548. public override void DOPlay()
  549. {
  550. DOTween.Play(GetTweenTarget());
  551. }
  552. /// <summary>
  553. /// Plays backwards all tweens whose target-id is the same as the one set by this animation
  554. /// </summary>
  555. public override void DOPlayBackwards()
  556. {
  557. DOTween.PlayBackwards(GetTweenTarget());
  558. }
  559. /// <summary>
  560. /// Plays foward all tweens whose target-id is the same as the one set by this animation
  561. /// </summary>
  562. public override void DOPlayForward()
  563. {
  564. DOTween.PlayForward(GetTweenTarget());
  565. }
  566. /// <summary>
  567. /// Pauses all tweens whose target-id is the same as the one set by this animation
  568. /// </summary>
  569. public override void DOPause()
  570. {
  571. DOTween.Pause(GetTweenTarget());
  572. }
  573. /// <summary>
  574. /// Pauses/unpauses (depending on the current state) all tweens whose target-id is the same as the one set by this animation
  575. /// </summary>
  576. public override void DOTogglePause()
  577. {
  578. DOTween.TogglePause(GetTweenTarget());
  579. }
  580. /// <summary>
  581. /// Rewinds all tweens created by this animation in the correct order
  582. /// </summary>
  583. public override void DORewind()
  584. {
  585. _playCount = -1;
  586. // Rewind using Components order (in case there are multiple animations on the same property)
  587. DOTweenAnimation[] anims = this.gameObject.GetComponents<DOTweenAnimation>();
  588. for (int i = anims.Length - 1; i > -1; --i) {
  589. Tween t = anims[i].tween;
  590. if (t != null && t.IsInitialized()) anims[i].tween.Rewind();
  591. }
  592. // DOTween.Rewind(GetTweenTarget());
  593. }
  594. /// <summary>
  595. /// Restarts all tweens whose target-id is the same as the one set by this animation
  596. /// </summary>
  597. public override void DORestart()
  598. { DORestart(false); }
  599. /// <summary>
  600. /// Restarts all tweens whose target-id is the same as the one set by this animation
  601. /// </summary>
  602. /// <param name="fromHere">If TRUE, re-evaluates the tween's start and end values from its current position.
  603. /// Set it to TRUE when spawning the same DOTweenAnimation in different positions (like when using a pooling system)</param>
  604. public override void DORestart(bool fromHere)
  605. {
  606. _playCount = -1;
  607. if (tween == null) {
  608. if (Debugger.logPriority > 1) Debugger.LogNullTween(tween); return;
  609. }
  610. if (fromHere && isRelative) ReEvaluateRelativeTween();
  611. DOTween.Restart(GetTweenTarget());
  612. }
  613. /// <summary>
  614. /// Completes all tweens whose target-id is the same as the one set by this animation
  615. /// </summary>
  616. public override void DOComplete()
  617. {
  618. DOTween.Complete(GetTweenTarget());
  619. }
  620. /// <summary>
  621. /// Sends to the given time (and pauses) all the tweens whose target-id is the one set by this animation
  622. /// </summary>
  623. /// <param name="time">Time to send the tween to</param>
  624. public override void DOGotoAndPause(float time)
  625. { DOGoto(time, false); }
  626. /// <summary>
  627. /// Sends to the given time (and plays) all the tweens whose target-id is the one set by this animation
  628. /// </summary>
  629. /// <param name="time">Time to send the tween to</param>
  630. public override void DOGotoAndPlay(float time)
  631. { DOGoto(time, true); }
  632. void DOGoto(float time, bool andPlay)
  633. {
  634. _tmpTweens.Clear();
  635. DOTween.TweensByTarget(GetTweenTarget(), false, _tmpTweens);
  636. int len = _tmpTweens.Count;
  637. if (len == 0) {
  638. Debugger.LogWarning((andPlay ? "DOGotoAndPlay" : "DoGotoAndPause") + " ► tween doesn't exist");
  639. } else {
  640. for (int i = 0; i < _tmpTweens.Count; ++i) {
  641. _tmpTweens[i].Goto(time, andPlay);
  642. }
  643. }
  644. _tmpTweens.Clear();
  645. }
  646. /// <summary>
  647. /// Kills all tweens whose target-id is the same as the one set by this animation
  648. /// </summary>
  649. public override void DOKill()
  650. {
  651. DOTween.Kill(GetTweenTarget());
  652. tween = null;
  653. }
  654. #region Specifics
  655. /// <summary>
  656. /// Plays all tweens with the given ID and whose target-id is the same as the one set by this animation
  657. /// </summary>
  658. public void DOPlayById(string id)
  659. {
  660. DOTween.Play(GetTweenTarget(), id);
  661. }
  662. /// <summary>
  663. /// Plays all tweens with the given ID (regardless of their target gameObject)
  664. /// </summary>
  665. public void DOPlayAllById(string id)
  666. {
  667. DOTween.Play(id);
  668. }
  669. /// <summary>
  670. /// Pauses all tweens that with the given ID (regardless of their target gameObject)
  671. /// </summary>
  672. public void DOPauseAllById(string id)
  673. {
  674. DOTween.Pause(id);
  675. }
  676. /// <summary>
  677. /// Plays backwards all tweens with the given ID and whose target-id is the same as the one set by this animation
  678. /// </summary>
  679. public void DOPlayBackwardsById(string id)
  680. {
  681. DOTween.PlayBackwards(GetTweenTarget(), id);
  682. }
  683. /// <summary>
  684. /// Plays backwards all tweens with the given ID (regardless of their target gameObject)
  685. /// </summary>
  686. public void DOPlayBackwardsAllById(string id)
  687. {
  688. DOTween.PlayBackwards(id);
  689. }
  690. /// <summary>
  691. /// Plays forward all tweens with the given ID and whose target-id is the same as the one set by this animation
  692. /// </summary>
  693. public void DOPlayForwardById(string id)
  694. {
  695. DOTween.PlayForward(GetTweenTarget(), id);
  696. }
  697. /// <summary>
  698. /// Plays forward all tweens with the given ID (regardless of their target gameObject)
  699. /// </summary>
  700. public void DOPlayForwardAllById(string id)
  701. {
  702. DOTween.PlayForward(id);
  703. }
  704. /// <summary>
  705. /// Plays the next animation on this animation's gameObject (if any)
  706. /// </summary>
  707. public void DOPlayNext()
  708. {
  709. DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>();
  710. while (_playCount < anims.Length - 1) {
  711. _playCount++;
  712. DOTweenAnimation anim = anims[_playCount];
  713. if (anim != null && anim.tween != null && anim.tween.active && !anim.tween.IsPlaying() && !anim.tween.IsComplete()) {
  714. anim.tween.Play();
  715. break;
  716. }
  717. }
  718. }
  719. /// <summary>
  720. /// Rewinds all tweens with the given ID and whose target-id is the same as the one set by this animation,
  721. /// then plays the next animation on this animation's gameObject (if any)
  722. /// </summary>
  723. public void DORewindAndPlayNext()
  724. {
  725. _playCount = -1;
  726. DOTween.Rewind(GetTweenTarget());
  727. DOPlayNext();
  728. }
  729. /// <summary>
  730. /// Rewinds all tweens with the given ID (regardless of their target gameObject)
  731. /// </summary>
  732. public void DORewindAllById(string id)
  733. {
  734. _playCount = -1;
  735. DOTween.Rewind(id);
  736. }
  737. /// <summary>
  738. /// Restarts all tweens with the given ID and whose target-id is the same as the one set by this animation
  739. /// </summary>
  740. public void DORestartById(string id)
  741. {
  742. _playCount = -1;
  743. DOTween.Restart(GetTweenTarget(), id);
  744. }
  745. /// <summary>
  746. /// Restarts all tweens with the given ID (regardless of their target gameObject)
  747. /// </summary>
  748. public void DORestartAllById(string id)
  749. {
  750. _playCount = -1;
  751. DOTween.Restart(id);
  752. }
  753. /// <summary>
  754. /// Kills all tweens with the given ID and whose target-id is the same as the one set by this animation
  755. /// </summary>
  756. public void DOKillById(string id)
  757. {
  758. DOTween.Kill(GetTweenTarget(), id);
  759. }
  760. /// <summary>
  761. /// Kills all tweens with the given ID (regardless of their target gameObject)
  762. /// </summary>
  763. public void DOKillAllById(string id)
  764. {
  765. DOTween.Kill(id);
  766. }
  767. #endregion
  768. #region Internal (also used by Inspector)
  769. public static TargetType TypeToDOTargetType(Type t)
  770. {
  771. string str = t.ToString();
  772. int dotIndex = str.LastIndexOf(".");
  773. if (dotIndex != -1) str = str.Substring(dotIndex + 1);
  774. if (str.IndexOf("Renderer") != -1 && (str != "SpriteRenderer")) str = "Renderer";
  775. //#if true // PHYSICS_MARKER
  776. // if (str == "Rigidbody") str = "Transform";
  777. //#endif
  778. //#if true // PHYSICS2D_MARKER
  779. // if (str == "Rigidbody2D") str = "Transform";
  780. //#endif
  781. #if true // UI_MARKER
  782. // if (str == "RectTransform") str = "Transform";
  783. if (str == "RawImage" || str == "Graphic") str = "Image"; // RawImages/Graphics are managed like Images for DOTweenAnimation (color and fade use Graphic target anyway)
  784. #endif
  785. return (TargetType)Enum.Parse(typeof(TargetType), str);
  786. }
  787. // Editor preview system
  788. /// <summary>
  789. /// Previews the tween in the editor. Only for DOTween internal usage: don't use otherwise.
  790. /// </summary>
  791. public Tween CreateEditorPreview()
  792. {
  793. if (Application.isPlaying) return null;
  794. // CHANGE: first param switched to TRUE otherwise changing an animation and replaying in editor would still play old one
  795. CreateTween(true, autoPlay);
  796. return tween;
  797. }
  798. #endregion
  799. #endregion
  800. #region Private
  801. /// <summary>
  802. /// Returns the gameObject whose target component should be animated
  803. /// </summary>
  804. /// <returns></returns>
  805. GameObject GetTweenGO()
  806. {
  807. return targetIsSelf ? this.gameObject : targetGO;
  808. }
  809. /// <summary>
  810. /// Returns the GameObject which should be used/retrieved for SetTarget
  811. /// </summary>
  812. GameObject GetTweenTarget()
  813. {
  814. return targetIsSelf || !tweenTargetIsTargetGO ? this.gameObject : targetGO;
  815. }
  816. // Re-evaluate relative position of path
  817. void ReEvaluateRelativeTween()
  818. {
  819. GameObject tweenGO = GetTweenGO();
  820. if (tweenGO == null) {
  821. Debug.LogWarning(string.Format("{0} :: This DOTweenAnimation's target/GameObject is unset: the tween will not be created.", this.gameObject.name), this.gameObject);
  822. return;
  823. }
  824. if (animationType == AnimationType.Move) {
  825. ((Tweener)tween).ChangeEndValue(tweenGO.transform.position + endValueV3, true);
  826. } else if (animationType == AnimationType.LocalMove) {
  827. ((Tweener)tween).ChangeEndValue(tweenGO.transform.localPosition + endValueV3, true);
  828. }
  829. }
  830. #endregion
  831. }
  832. public static class DOTweenAnimationExtensions
  833. {
  834. // // Doesn't work on Win 8.1
  835. // public static bool IsSameOrSubclassOf(this Type t, Type tBase)
  836. // {
  837. // return t.IsSubclassOf(tBase) || t == tBase;
  838. // }
  839. public static bool IsSameOrSubclassOf<T>(this Component t)
  840. {
  841. return t is T;
  842. }
  843. }
  844. }