DOTweenAnimation.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 false // 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. }
  35. public enum TargetType
  36. {
  37. Unset,
  38. Camera,
  39. CanvasGroup,
  40. Image,
  41. Light,
  42. RectTransform,
  43. Renderer, SpriteRenderer,
  44. Rigidbody, Rigidbody2D,
  45. Text,
  46. Transform,
  47. tk2dBaseSprite,
  48. tk2dTextMesh,
  49. TextMeshPro,
  50. TextMeshProUGUI
  51. }
  52. #region EVENTS - EDITOR-ONLY
  53. /// <summary>Used internally by the editor</summary>
  54. public static event Action<DOTweenAnimation> OnReset;
  55. static void Dispatch_OnReset(DOTweenAnimation anim) { if (OnReset != null) OnReset(anim); }
  56. #endregion
  57. public bool targetIsSelf = true; // If FALSE allows to set the target manually
  58. public GameObject targetGO = null; // Used in case targetIsSelf is FALSE
  59. // If TRUE always uses the GO containing this DOTweenAnimation (and not the one containing the target) as DOTween's SetTarget target
  60. public bool tweenTargetIsTargetGO = true;
  61. public float delay;
  62. public float duration = 1;
  63. public Ease easeType = Ease.OutQuad;
  64. public AnimationCurve easeCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
  65. public LoopType loopType = LoopType.Restart;
  66. public int loops = 1;
  67. public string id = "";
  68. public bool isRelative;
  69. public bool isFrom;
  70. public bool isIndependentUpdate = false;
  71. public bool autoKill = true;
  72. public bool isActive = true;
  73. public bool isValid;
  74. public Component target;
  75. public AnimationType animationType;
  76. public TargetType targetType;
  77. public TargetType forcedTargetType; // Used when choosing between multiple targets
  78. public bool autoPlay = true;
  79. public bool useTargetAsV3;
  80. public float endValueFloat;
  81. public Vector3 endValueV3;
  82. public Vector2 endValueV2;
  83. public Color endValueColor = new Color(1, 1, 1, 1);
  84. public string endValueString = "";
  85. public Rect endValueRect = new Rect(0, 0, 0, 0);
  86. public Transform endValueTransform;
  87. public bool optionalBool0;
  88. public float optionalFloat0;
  89. public int optionalInt0;
  90. public RotateMode optionalRotationMode = RotateMode.Fast;
  91. public ScrambleMode optionalScrambleMode = ScrambleMode.None;
  92. public string optionalString;
  93. bool _tweenCreated; // TRUE after the tweens have been created
  94. int _playCount = -1; // Used when calling DOPlayNext
  95. #region Unity Methods
  96. void Awake()
  97. {
  98. if (!isActive || !isValid) return;
  99. if (animationType != AnimationType.Move || !useTargetAsV3) {
  100. // Don't create tweens if we're using a RectTransform as a Move target,
  101. // because that will work only inside Start
  102. CreateTween();
  103. _tweenCreated = true;
  104. }
  105. }
  106. void Start()
  107. {
  108. if (_tweenCreated || !isActive || !isValid) return;
  109. CreateTween();
  110. _tweenCreated = true;
  111. }
  112. void Reset()
  113. {
  114. Dispatch_OnReset(this);
  115. }
  116. void OnDestroy()
  117. {
  118. if (tween != null && tween.IsActive()) tween.Kill();
  119. tween = null;
  120. }
  121. // Used also by DOTweenAnimationInspector when applying runtime changes and restarting
  122. public void CreateTween()
  123. {
  124. // if (target == null) {
  125. // 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);
  126. // return;
  127. // }
  128. GameObject tweenGO = GetTweenGO();
  129. if (target == null || tweenGO == null) {
  130. if (targetIsSelf && target == null) {
  131. // Old error caused during upgrade from DOTween Pro 0.9.255
  132. 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);
  133. } else {
  134. // Missing non-self target
  135. Debug.LogWarning(string.Format("{0} :: This DOTweenAnimation's target/GameObject is unset: the tween will not be created.", this.gameObject.name), this.gameObject);
  136. }
  137. return;
  138. }
  139. if (forcedTargetType != TargetType.Unset) targetType = forcedTargetType;
  140. if (targetType == TargetType.Unset) {
  141. // Legacy DOTweenAnimation (made with a version older than 0.9.450) without stored targetType > assign it now
  142. targetType = TypeToDOTargetType(target.GetType());
  143. }
  144. switch (animationType) {
  145. case AnimationType.None:
  146. break;
  147. case AnimationType.Move:
  148. if (useTargetAsV3) {
  149. isRelative = false;
  150. if (endValueTransform == null) {
  151. 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);
  152. endValueV3 = Vector3.zero;
  153. } else {
  154. #if true // UI_MARKER
  155. if (targetType == TargetType.RectTransform) {
  156. RectTransform endValueT = endValueTransform as RectTransform;
  157. if (endValueT == null) {
  158. 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);
  159. endValueV3 = Vector3.zero;
  160. } else {
  161. RectTransform rTarget = target as RectTransform;
  162. if (rTarget == null) {
  163. 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);
  164. } else {
  165. // Problem: doesn't work inside Awake (ararargh!)
  166. endValueV3 = DOTweenModuleUI.Utils.SwitchToRectTransform(endValueT, rTarget);
  167. }
  168. }
  169. } else
  170. #endif
  171. endValueV3 = endValueTransform.position;
  172. }
  173. }
  174. switch (targetType) {
  175. case TargetType.Transform:
  176. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  177. break;
  178. case TargetType.RectTransform:
  179. #if true // UI_MARKER
  180. tween = ((RectTransform)target).DOAnchorPos3D(endValueV3, duration, optionalBool0);
  181. #else
  182. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  183. #endif
  184. break;
  185. case TargetType.Rigidbody:
  186. #if true // PHYSICS_MARKER
  187. tween = ((Rigidbody)target).DOMove(endValueV3, duration, optionalBool0);
  188. #else
  189. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  190. #endif
  191. break;
  192. case TargetType.Rigidbody2D:
  193. #if true // PHYSICS2D_MARKER
  194. tween = ((Rigidbody2D)target).DOMove(endValueV3, duration, optionalBool0);
  195. #else
  196. tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0);
  197. #endif
  198. break;
  199. }
  200. break;
  201. case AnimationType.LocalMove:
  202. tween = tweenGO.transform.DOLocalMove(endValueV3, duration, optionalBool0);
  203. break;
  204. case AnimationType.Rotate:
  205. switch (targetType) {
  206. case TargetType.Transform:
  207. tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode);
  208. break;
  209. case TargetType.Rigidbody:
  210. #if true // PHYSICS_MARKER
  211. tween = ((Rigidbody)target).DORotate(endValueV3, duration, optionalRotationMode);
  212. #else
  213. tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode);
  214. #endif
  215. break;
  216. case TargetType.Rigidbody2D:
  217. #if true // PHYSICS2D_MARKER
  218. tween = ((Rigidbody2D)target).DORotate(endValueFloat, duration);
  219. #else
  220. tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode);
  221. #endif
  222. break;
  223. }
  224. break;
  225. case AnimationType.LocalRotate:
  226. tween = tweenGO.transform.DOLocalRotate(endValueV3, duration, optionalRotationMode);
  227. break;
  228. case AnimationType.Scale:
  229. switch (targetType) {
  230. #if false // TK2D_MARKER
  231. case TargetType.tk2dTextMesh:
  232. tween = ((tk2dTextMesh)target).DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  233. break;
  234. case TargetType.tk2dBaseSprite:
  235. tween = ((tk2dBaseSprite)target).DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  236. break;
  237. #endif
  238. default:
  239. tween = tweenGO.transform.DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration);
  240. break;
  241. }
  242. break;
  243. #if true // UI_MARKER
  244. case AnimationType.UIWidthHeight:
  245. tween = ((RectTransform)target).DOSizeDelta(optionalBool0 ? new Vector2(endValueFloat, endValueFloat) : endValueV2, duration);
  246. break;
  247. #endif
  248. case AnimationType.Color:
  249. isRelative = false;
  250. switch (targetType) {
  251. case TargetType.Renderer:
  252. tween = ((Renderer)target).material.DOColor(endValueColor, duration);
  253. break;
  254. case TargetType.Light:
  255. tween = ((Light)target).DOColor(endValueColor, duration);
  256. break;
  257. #if true // SPRITE_MARKER
  258. case TargetType.SpriteRenderer:
  259. tween = ((SpriteRenderer)target).DOColor(endValueColor, duration);
  260. break;
  261. #endif
  262. #if true // UI_MARKER
  263. case TargetType.Image:
  264. tween = ((Graphic)target).DOColor(endValueColor, duration);
  265. break;
  266. case TargetType.Text:
  267. tween = ((Text)target).DOColor(endValueColor, duration);
  268. break;
  269. #endif
  270. #if false // TK2D_MARKER
  271. case TargetType.tk2dTextMesh:
  272. tween = ((tk2dTextMesh)target).DOColor(endValueColor, duration);
  273. break;
  274. case TargetType.tk2dBaseSprite:
  275. tween = ((tk2dBaseSprite)target).DOColor(endValueColor, duration);
  276. break;
  277. #endif
  278. #if false // TEXTMESHPRO_MARKER
  279. case TargetType.TextMeshProUGUI:
  280. tween = ((TextMeshProUGUI)target).DOColor(endValueColor, duration);
  281. break;
  282. case TargetType.TextMeshPro:
  283. tween = ((TextMeshPro)target).DOColor(endValueColor, duration);
  284. break;
  285. #endif
  286. }
  287. break;
  288. case AnimationType.Fade:
  289. isRelative = false;
  290. switch (targetType) {
  291. case TargetType.Renderer:
  292. tween = ((Renderer)target).material.DOFade(endValueFloat, duration);
  293. break;
  294. case TargetType.Light:
  295. tween = ((Light)target).DOIntensity(endValueFloat, duration);
  296. break;
  297. #if true // SPRITE_MARKER
  298. case TargetType.SpriteRenderer:
  299. tween = ((SpriteRenderer)target).DOFade(endValueFloat, duration);
  300. break;
  301. #endif
  302. #if true // UI_MARKER
  303. case TargetType.Image:
  304. tween = ((Graphic)target).DOFade(endValueFloat, duration);
  305. break;
  306. case TargetType.Text:
  307. tween = ((Text)target).DOFade(endValueFloat, duration);
  308. break;
  309. case TargetType.CanvasGroup:
  310. tween = ((CanvasGroup)target).DOFade(endValueFloat, duration);
  311. break;
  312. #endif
  313. #if false // TK2D_MARKER
  314. case TargetType.tk2dTextMesh:
  315. tween = ((tk2dTextMesh)target).DOFade(endValueFloat, duration);
  316. break;
  317. case TargetType.tk2dBaseSprite:
  318. tween = ((tk2dBaseSprite)target).DOFade(endValueFloat, duration);
  319. break;
  320. #endif
  321. #if false // TEXTMESHPRO_MARKER
  322. case TargetType.TextMeshProUGUI:
  323. tween = ((TextMeshProUGUI)target).DOFade(endValueFloat, duration);
  324. break;
  325. case TargetType.TextMeshPro:
  326. tween = ((TextMeshPro)target).DOFade(endValueFloat, duration);
  327. break;
  328. #endif
  329. }
  330. break;
  331. case AnimationType.Text:
  332. #if true // UI_MARKER
  333. switch (targetType) {
  334. case TargetType.Text:
  335. tween = ((Text)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  336. break;
  337. }
  338. #endif
  339. #if false // TK2D_MARKER
  340. switch (targetType) {
  341. case TargetType.tk2dTextMesh:
  342. tween = ((tk2dTextMesh)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  343. break;
  344. }
  345. #endif
  346. #if false // TEXTMESHPRO_MARKER
  347. switch (targetType) {
  348. case TargetType.TextMeshProUGUI:
  349. tween = ((TextMeshProUGUI)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  350. break;
  351. case TargetType.TextMeshPro:
  352. tween = ((TextMeshPro)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString);
  353. break;
  354. }
  355. #endif
  356. break;
  357. case AnimationType.PunchPosition:
  358. switch (targetType) {
  359. case TargetType.Transform:
  360. tween = ((Transform)target).DOPunchPosition(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  361. break;
  362. #if true // UI_MARKER
  363. case TargetType.RectTransform:
  364. tween = ((RectTransform)target).DOPunchAnchorPos(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0);
  365. break;
  366. #endif
  367. }
  368. break;
  369. case AnimationType.PunchScale:
  370. tween = tweenGO.transform.DOPunchScale(endValueV3, duration, optionalInt0, optionalFloat0);
  371. break;
  372. case AnimationType.PunchRotation:
  373. tween = tweenGO.transform.DOPunchRotation(endValueV3, duration, optionalInt0, optionalFloat0);
  374. break;
  375. case AnimationType.ShakePosition:
  376. switch (targetType) {
  377. case TargetType.Transform:
  378. tween = ((Transform)target).DOShakePosition(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  379. break;
  380. #if true // UI_MARKER
  381. case TargetType.RectTransform:
  382. tween = ((RectTransform)target).DOShakeAnchorPos(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0);
  383. break;
  384. #endif
  385. }
  386. break;
  387. case AnimationType.ShakeScale:
  388. tween = tweenGO.transform.DOShakeScale(duration, endValueV3, optionalInt0, optionalFloat0);
  389. break;
  390. case AnimationType.ShakeRotation:
  391. tween = tweenGO.transform.DOShakeRotation(duration, endValueV3, optionalInt0, optionalFloat0);
  392. break;
  393. case AnimationType.CameraAspect:
  394. tween = ((Camera)target).DOAspect(endValueFloat, duration);
  395. break;
  396. case AnimationType.CameraBackgroundColor:
  397. tween = ((Camera)target).DOColor(endValueColor, duration);
  398. break;
  399. case AnimationType.CameraFieldOfView:
  400. tween = ((Camera)target).DOFieldOfView(endValueFloat, duration);
  401. break;
  402. case AnimationType.CameraOrthoSize:
  403. tween = ((Camera)target).DOOrthoSize(endValueFloat, duration);
  404. break;
  405. case AnimationType.CameraPixelRect:
  406. tween = ((Camera)target).DOPixelRect(endValueRect, duration);
  407. break;
  408. case AnimationType.CameraRect:
  409. tween = ((Camera)target).DORect(endValueRect, duration);
  410. break;
  411. }
  412. if (tween == null) return;
  413. if (isFrom) {
  414. ((Tweener)tween).From(isRelative);
  415. } else {
  416. tween.SetRelative(isRelative);
  417. }
  418. GameObject setTarget = targetIsSelf || !tweenTargetIsTargetGO ? this.gameObject : targetGO;
  419. tween.SetTarget(setTarget).SetDelay(delay).SetLoops(loops, loopType).SetAutoKill(autoKill)
  420. .OnKill(()=> tween = null);
  421. if (isSpeedBased) tween.SetSpeedBased();
  422. if (easeType == Ease.INTERNAL_Custom) tween.SetEase(easeCurve);
  423. else tween.SetEase(easeType);
  424. if (!string.IsNullOrEmpty(id)) tween.SetId(id);
  425. tween.SetUpdate(isIndependentUpdate);
  426. if (hasOnStart) {
  427. if (onStart != null) tween.OnStart(onStart.Invoke);
  428. } else onStart = null;
  429. if (hasOnPlay) {
  430. if (onPlay != null) tween.OnPlay(onPlay.Invoke);
  431. } else onPlay = null;
  432. if (hasOnUpdate) {
  433. if (onUpdate != null) tween.OnUpdate(onUpdate.Invoke);
  434. } else onUpdate = null;
  435. if (hasOnStepComplete) {
  436. if (onStepComplete != null) tween.OnStepComplete(onStepComplete.Invoke);
  437. } else onStepComplete = null;
  438. if (hasOnComplete) {
  439. if (onComplete != null) tween.OnComplete(onComplete.Invoke);
  440. } else onComplete = null;
  441. if (hasOnRewind) {
  442. if (onRewind != null) tween.OnRewind(onRewind.Invoke);
  443. } else onRewind = null;
  444. if (autoPlay) tween.Play();
  445. else tween.Pause();
  446. if (hasOnTweenCreated && onTweenCreated != null) onTweenCreated.Invoke();
  447. }
  448. #endregion
  449. #region Public Methods
  450. // These methods are here so they can be called directly via Unity's UGUI event system
  451. public override void DOPlay()
  452. {
  453. DOTween.Play(this.gameObject);
  454. }
  455. public override void DOPlayBackwards()
  456. {
  457. DOTween.PlayBackwards(this.gameObject);
  458. }
  459. public override void DOPlayForward()
  460. {
  461. DOTween.PlayForward(this.gameObject);
  462. }
  463. public override void DOPause()
  464. {
  465. DOTween.Pause(this.gameObject);
  466. }
  467. public override void DOTogglePause()
  468. {
  469. DOTween.TogglePause(this.gameObject);
  470. }
  471. public override void DORewind()
  472. {
  473. _playCount = -1;
  474. // Rewind using Components order (in case there are multiple animations on the same property)
  475. DOTweenAnimation[] anims = this.gameObject.GetComponents<DOTweenAnimation>();
  476. for (int i = anims.Length - 1; i > -1; --i) {
  477. Tween t = anims[i].tween;
  478. if (t != null && t.IsInitialized()) anims[i].tween.Rewind();
  479. }
  480. // DOTween.Rewind(this.gameObject);
  481. }
  482. /// <summary>
  483. /// Restarts the tween
  484. /// </summary>
  485. public override void DORestart()
  486. { DORestart(false); }
  487. /// <summary>
  488. /// Restarts the tween
  489. /// </summary>
  490. /// <param name="fromHere">If TRUE, re-evaluates the tween's start and end values from its current position.
  491. /// Set it to TRUE when spawning the same DOTweenAnimation in different positions (like when using a pooling system)</param>
  492. public override void DORestart(bool fromHere)
  493. {
  494. _playCount = -1;
  495. if (tween == null) {
  496. if (Debugger.logPriority > 1) Debugger.LogNullTween(tween); return;
  497. }
  498. if (fromHere && isRelative) ReEvaluateRelativeTween();
  499. DOTween.Restart(this.gameObject);
  500. }
  501. public override void DOComplete()
  502. {
  503. DOTween.Complete(this.gameObject);
  504. }
  505. public override void DOKill()
  506. {
  507. DOTween.Kill(this.gameObject);
  508. tween = null;
  509. }
  510. #region Specifics
  511. public void DOPlayById(string id)
  512. {
  513. DOTween.Play(this.gameObject, id);
  514. }
  515. public void DOPlayAllById(string id)
  516. {
  517. DOTween.Play(id);
  518. }
  519. public void DOPauseAllById(string id)
  520. {
  521. DOTween.Pause(id);
  522. }
  523. public void DOPlayBackwardsById(string id)
  524. {
  525. DOTween.PlayBackwards(this.gameObject, id);
  526. }
  527. public void DOPlayBackwardsAllById(string id)
  528. {
  529. DOTween.PlayBackwards(id);
  530. }
  531. public void DOPlayForwardById(string id)
  532. {
  533. DOTween.PlayForward(this.gameObject, id);
  534. }
  535. public void DOPlayForwardAllById(string id)
  536. {
  537. DOTween.PlayForward(id);
  538. }
  539. public void DOPlayNext()
  540. {
  541. DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>();
  542. while (_playCount < anims.Length - 1) {
  543. _playCount++;
  544. DOTweenAnimation anim = anims[_playCount];
  545. if (anim != null && anim.tween != null && !anim.tween.IsPlaying() && !anim.tween.IsComplete()) {
  546. anim.tween.Play();
  547. break;
  548. }
  549. }
  550. }
  551. public void DORewindAndPlayNext()
  552. {
  553. _playCount = -1;
  554. DOTween.Rewind(this.gameObject);
  555. DOPlayNext();
  556. }
  557. public void DORewindAllById(string id)
  558. {
  559. _playCount = -1;
  560. DOTween.Rewind(id);
  561. }
  562. public void DORestartById(string id)
  563. {
  564. _playCount = -1;
  565. DOTween.Restart(this.gameObject, id);
  566. }
  567. public void DORestartAllById(string id)
  568. {
  569. _playCount = -1;
  570. DOTween.Restart(id);
  571. }
  572. /// <summary>
  573. /// Returns the tweens created by this DOTweenAnimation, in the same order as they appear in the Inspector (top to bottom)
  574. /// </summary>
  575. public List<Tween> GetTweens()
  576. {
  577. // return DOTween.TweensByTarget(this.gameObject);
  578. List<Tween> result = new List<Tween>();
  579. DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>();
  580. foreach (DOTweenAnimation anim in anims) result.Add(anim.tween);
  581. return result;
  582. }
  583. #endregion
  584. #region Internal (also used by Inspector)
  585. public static TargetType TypeToDOTargetType(Type t)
  586. {
  587. string str = t.ToString();
  588. int dotIndex = str.LastIndexOf(".");
  589. if (dotIndex != -1) str = str.Substring(dotIndex + 1);
  590. if (str.IndexOf("Renderer") != -1 && (str != "SpriteRenderer")) str = "Renderer";
  591. //#if true // PHYSICS_MARKER
  592. // if (str == "Rigidbody") str = "Transform";
  593. //#endif
  594. //#if true // PHYSICS2D_MARKER
  595. // if (str == "Rigidbody2D") str = "Transform";
  596. //#endif
  597. #if true // UI_MARKER
  598. // if (str == "RectTransform") str = "Transform";
  599. if (str == "RawImage" || str == "Graphic") str = "Image"; // RawImages/Graphics are managed like Images for DOTweenAnimation (color and fade use Graphic target anyway)
  600. #endif
  601. return (TargetType)Enum.Parse(typeof(TargetType), str);
  602. }
  603. // Editor preview system
  604. /// <summary>
  605. /// Previews the tween in the editor. Only for DOTween internal usage: don't use otherwise.
  606. /// </summary>
  607. public Tween CreateEditorPreview()
  608. {
  609. if (Application.isPlaying) return null;
  610. CreateTween();
  611. return tween;
  612. }
  613. #endregion
  614. #endregion
  615. #region Private
  616. // Returns the gameObject whose target component should be animated
  617. GameObject GetTweenGO()
  618. {
  619. return targetIsSelf ? this.gameObject : targetGO;
  620. }
  621. // Re-evaluate relative position of path
  622. void ReEvaluateRelativeTween()
  623. {
  624. GameObject tweenGO = GetTweenGO();
  625. if (tweenGO == null) {
  626. Debug.LogWarning(string.Format("{0} :: This DOTweenAnimation's target/GameObject is unset: the tween will not be created.", this.gameObject.name), this.gameObject);
  627. return;
  628. }
  629. if (animationType == AnimationType.Move) {
  630. ((Tweener)tween).ChangeEndValue(tweenGO.transform.position + endValueV3, true);
  631. } else if (animationType == AnimationType.LocalMove) {
  632. ((Tweener)tween).ChangeEndValue(tweenGO.transform.localPosition + endValueV3, true);
  633. }
  634. }
  635. #endregion
  636. }
  637. public static class DOTweenAnimationExtensions
  638. {
  639. // // Doesn't work on Win 8.1
  640. // public static bool IsSameOrSubclassOf(this Type t, Type tBase)
  641. // {
  642. // return t.IsSubclassOf(tBase) || t == tBase;
  643. // }
  644. public static bool IsSameOrSubclassOf<T>(this Component t)
  645. {
  646. return t is T;
  647. }
  648. }
  649. }