Unity 框架

DOTweenAnimation.cs 36KB

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