DOTweenAnimationInspector.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2015/03/12 16:03
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using DG.DemiEditor;
  7. using DG.DOTweenEditor.Core;
  8. using DG.Tweening;
  9. using DG.Tweening.Core;
  10. using UnityEditor;
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. #if DOTWEEN_TMP
  14. using TMPro;
  15. #endif
  16. namespace DG.DOTweenEditor
  17. {
  18. [CustomEditor(typeof(DOTweenAnimation))]
  19. public class DOTweenAnimationInspector : ABSAnimationInspector
  20. {
  21. enum FadeTargetType
  22. {
  23. CanvasGroup,
  24. Image
  25. }
  26. enum ChooseTargetMode
  27. {
  28. None,
  29. BetweenCanvasGroupAndImage
  30. }
  31. static readonly Dictionary<DOTweenAnimationType, Type[]> _AnimationTypeToComponent = new Dictionary<DOTweenAnimationType, Type[]>() {
  32. { DOTweenAnimationType.Move, new[] { typeof(Rigidbody), typeof(Rigidbody2D), typeof(RectTransform), typeof(Transform) } },
  33. { DOTweenAnimationType.LocalMove, new[] { typeof(Transform) } },
  34. { DOTweenAnimationType.Rotate, new[] { typeof(Rigidbody), typeof(Rigidbody2D), typeof(Transform) } },
  35. { DOTweenAnimationType.LocalRotate, new[] { typeof(Transform) } },
  36. { DOTweenAnimationType.Scale, new[] { typeof(Transform) } },
  37. { DOTweenAnimationType.Color, new[] { typeof(SpriteRenderer), typeof(Renderer), typeof(Image), typeof(Text), typeof(Light) } },
  38. { DOTweenAnimationType.Fade, new[] { typeof(SpriteRenderer), typeof(Renderer), typeof(Image), typeof(Text), typeof(CanvasGroup), typeof(Light) } },
  39. { DOTweenAnimationType.Text, new[] { typeof(Text) } },
  40. { DOTweenAnimationType.PunchPosition, new[] { typeof(RectTransform), typeof(Transform) } },
  41. { DOTweenAnimationType.PunchRotation, new[] { typeof(Transform) } },
  42. { DOTweenAnimationType.PunchScale, new[] { typeof(Transform) } },
  43. { DOTweenAnimationType.ShakePosition, new[] { typeof(RectTransform), typeof(Transform) } },
  44. { DOTweenAnimationType.ShakeRotation, new[] { typeof(Transform) } },
  45. { DOTweenAnimationType.ShakeScale, new[] { typeof(Transform) } },
  46. { DOTweenAnimationType.CameraAspect, new[] { typeof(Camera) } },
  47. { DOTweenAnimationType.CameraBackgroundColor, new[] { typeof(Camera) } },
  48. { DOTweenAnimationType.CameraFieldOfView, new[] { typeof(Camera) } },
  49. { DOTweenAnimationType.CameraOrthoSize, new[] { typeof(Camera) } },
  50. { DOTweenAnimationType.CameraPixelRect, new[] { typeof(Camera) } },
  51. { DOTweenAnimationType.CameraRect, new[] { typeof(Camera) } },
  52. { DOTweenAnimationType.UIWidthHeight, new[] { typeof(RectTransform) } },
  53. };
  54. #if DOTWEEN_TK2D
  55. static readonly Dictionary<DOTweenAnimationType, Type[]> _Tk2dAnimationTypeToComponent = new Dictionary<DOTweenAnimationType, Type[]>() {
  56. { DOTweenAnimationType.Scale, new[] { typeof(tk2dBaseSprite), typeof(tk2dTextMesh) } },
  57. { DOTweenAnimationType.Color, new[] { typeof(tk2dBaseSprite), typeof(tk2dTextMesh) } },
  58. { DOTweenAnimationType.Fade, new[] { typeof(tk2dBaseSprite), typeof(tk2dTextMesh) } },
  59. { DOTweenAnimationType.Text, new[] { typeof(tk2dTextMesh) } }
  60. };
  61. #endif
  62. #if DOTWEEN_TMP
  63. static readonly Dictionary<DOTweenAnimationType, Type[]> _TMPAnimationTypeToComponent = new Dictionary<DOTweenAnimationType, Type[]>() {
  64. { DOTweenAnimationType.Color, new[] { typeof(TextMeshPro), typeof(TextMeshProUGUI) } },
  65. { DOTweenAnimationType.Fade, new[] { typeof(TextMeshPro), typeof(TextMeshProUGUI) } },
  66. { DOTweenAnimationType.Text, new[] { typeof(TextMeshPro), typeof(TextMeshProUGUI) } }
  67. };
  68. #endif
  69. static readonly string[] _AnimationType = new[] {
  70. "None",
  71. "Move", "LocalMove",
  72. "Rotate", "LocalRotate",
  73. "Scale",
  74. "Color", "Fade",
  75. "Text",
  76. "UIWidthHeight",
  77. "Punch/Position", "Punch/Rotation", "Punch/Scale",
  78. "Shake/Position", "Shake/Rotation", "Shake/Scale",
  79. "Camera/Aspect", "Camera/BackgroundColor", "Camera/FieldOfView", "Camera/OrthoSize", "Camera/PixelRect", "Camera/Rect"
  80. };
  81. static string[] _animationTypeNoSlashes; // _AnimationType list without slashes in values
  82. static string[] _datString; // String representation of DOTweenAnimation enum (here for caching reasons)
  83. DOTweenAnimation _src;
  84. bool _runtimeEditMode; // If TRUE allows to change and save stuff at runtime
  85. int _totComponentsOnSrc; // Used to determine if a Component is added or removed from the source
  86. bool _isLightSrc; // Used to determine if we're tweening a Light, to set the max Fade value to more than 1
  87. ChooseTargetMode _chooseTargetMode = ChooseTargetMode.None;
  88. #region MonoBehaviour Methods
  89. void OnEnable()
  90. {
  91. _src = target as DOTweenAnimation;
  92. onStartProperty = base.serializedObject.FindProperty("onStart");
  93. onPlayProperty = base.serializedObject.FindProperty("onPlay");
  94. onUpdateProperty = base.serializedObject.FindProperty("onUpdate");
  95. onStepCompleteProperty = base.serializedObject.FindProperty("onStepComplete");
  96. onCompleteProperty = base.serializedObject.FindProperty("onComplete");
  97. onTweenCreatedProperty = base.serializedObject.FindProperty("onTweenCreated");
  98. // Convert _AnimationType to _animationTypeNoSlashes
  99. int len = _AnimationType.Length;
  100. _animationTypeNoSlashes = new string[len];
  101. for (int i = 0; i < len; ++i) {
  102. string a = _AnimationType[i];
  103. a = a.Replace("/", "");
  104. _animationTypeNoSlashes[i] = a;
  105. }
  106. }
  107. override public void OnInspectorGUI()
  108. {
  109. base.OnInspectorGUI();
  110. GUILayout.Space(3);
  111. EditorGUIUtils.SetGUIStyles();
  112. bool playMode = Application.isPlaying;
  113. _runtimeEditMode = _runtimeEditMode && playMode;
  114. GUILayout.BeginHorizontal();
  115. EditorGUIUtils.InspectorLogo();
  116. GUILayout.Label(_src.animationType.ToString() + (string.IsNullOrEmpty(_src.id) ? "" : " [" + _src.id + "]"), EditorGUIUtils.sideLogoIconBoldLabelStyle);
  117. // Up-down buttons
  118. GUILayout.FlexibleSpace();
  119. if (GUILayout.Button("▲", DeGUI.styles.button.toolIco)) UnityEditorInternal.ComponentUtility.MoveComponentUp(_src);
  120. if (GUILayout.Button("▼", DeGUI.styles.button.toolIco)) UnityEditorInternal.ComponentUtility.MoveComponentDown(_src);
  121. GUILayout.EndHorizontal();
  122. if (playMode) {
  123. if (_runtimeEditMode) {
  124. } else {
  125. GUILayout.Space(8);
  126. GUILayout.Label("Animation Editor disabled while in play mode", EditorGUIUtils.wordWrapLabelStyle);
  127. if (!_src.isActive) {
  128. GUILayout.Label("This animation has been toggled as inactive and won't be generated", EditorGUIUtils.wordWrapLabelStyle);
  129. GUI.enabled = false;
  130. }
  131. if (GUILayout.Button(new GUIContent("Activate Edit Mode", "Switches to Runtime Edit Mode, where you can change animations values and restart them"))) {
  132. _runtimeEditMode = true;
  133. }
  134. GUILayout.Label("NOTE: when using DOPlayNext, the sequence is determined by the DOTweenAnimation Components order in the target GameObject's Inspector", EditorGUIUtils.wordWrapLabelStyle);
  135. GUILayout.Space(10);
  136. if (!_runtimeEditMode) return;
  137. }
  138. }
  139. Undo.RecordObject(_src, "DOTween Animation");
  140. // _src.isValid = Validate(); // Moved down
  141. EditorGUIUtility.labelWidth = 110;
  142. if (playMode) {
  143. GUILayout.Space(4);
  144. DeGUILayout.Toolbar("Edit Mode Commands");
  145. DeGUILayout.BeginVBox(DeGUI.styles.box.stickyTop);
  146. GUILayout.BeginHorizontal();
  147. if (GUILayout.Button("TogglePause")) _src.tween.TogglePause();
  148. if (GUILayout.Button("Rewind")) _src.tween.Rewind();
  149. if (GUILayout.Button("Restart")) _src.tween.Restart();
  150. GUILayout.EndHorizontal();
  151. if (GUILayout.Button("Commit changes and restart")) {
  152. _src.tween.Rewind();
  153. _src.tween.Kill();
  154. if (_src.isValid) {
  155. _src.CreateTween();
  156. _src.tween.Play();
  157. }
  158. }
  159. GUILayout.Label("To apply your changes when exiting Play mode, use the Component's upper right menu and choose \"Copy Component\", then \"Paste Component Values\" after exiting Play mode", DeGUI.styles.label.wordwrap);
  160. DeGUILayout.EndVBox();
  161. } else {
  162. bool hasManager = _src.GetComponent<DOTweenVisualManager>() != null;
  163. if (!hasManager) {
  164. if (GUILayout.Button(new GUIContent("Add Manager", "Adds a manager component which allows you to choose additional options for this gameObject"))) {
  165. _src.gameObject.AddComponent<DOTweenVisualManager>();
  166. }
  167. }
  168. }
  169. GUILayout.BeginHorizontal();
  170. DOTweenAnimationType prevAnimType = _src.animationType;
  171. // _src.animationType = (DOTweenAnimationType)EditorGUILayout.EnumPopup(_src.animationType, EditorGUIUtils.popupButton);
  172. _src.isActive = EditorGUILayout.Toggle(new GUIContent("", "If unchecked, this animation will not be created"), _src.isActive, GUILayout.Width(16));
  173. GUI.enabled = _src.isActive;
  174. _src.animationType = AnimationToDOTweenAnimationType(_AnimationType[EditorGUILayout.Popup(DOTweenAnimationTypeToPopupId(_src.animationType), _AnimationType)]);
  175. _src.autoPlay = DeGUILayout.ToggleButton(_src.autoPlay, new GUIContent("AutoPlay", "If selected, the tween will play automatically"));
  176. _src.autoKill = DeGUILayout.ToggleButton(_src.autoKill, new GUIContent("AutoKill", "If selected, the tween will be killed when it completes, and won't be reusable"));
  177. GUILayout.EndHorizontal();
  178. if (prevAnimType != _src.animationType) {
  179. // Set default optional values based on animation type
  180. _src.endValueTransform = null;
  181. _src.useTargetAsV3 = false;
  182. switch (_src.animationType) {
  183. case DOTweenAnimationType.Move:
  184. case DOTweenAnimationType.LocalMove:
  185. case DOTweenAnimationType.Rotate:
  186. case DOTweenAnimationType.LocalRotate:
  187. case DOTweenAnimationType.Scale:
  188. _src.endValueV3 = Vector3.zero;
  189. _src.endValueFloat = 0;
  190. _src.optionalBool0 = _src.animationType == DOTweenAnimationType.Scale;
  191. break;
  192. case DOTweenAnimationType.UIWidthHeight:
  193. _src.endValueV3 = Vector3.zero;
  194. _src.endValueFloat = 0;
  195. _src.optionalBool0 = _src.animationType == DOTweenAnimationType.UIWidthHeight;
  196. break;
  197. case DOTweenAnimationType.Color:
  198. case DOTweenAnimationType.Fade:
  199. _isLightSrc = _src.GetComponent<Light>() != null;
  200. _src.endValueFloat = 0;
  201. break;
  202. case DOTweenAnimationType.Text:
  203. _src.optionalBool0 = true;
  204. break;
  205. case DOTweenAnimationType.PunchPosition:
  206. case DOTweenAnimationType.PunchRotation:
  207. case DOTweenAnimationType.PunchScale:
  208. _src.endValueV3 = _src.animationType == DOTweenAnimationType.PunchRotation ? new Vector3(0,180,0) : Vector3.one;
  209. _src.optionalFloat0 = 1;
  210. _src.optionalInt0 = 10;
  211. _src.optionalBool0 = false;
  212. break;
  213. case DOTweenAnimationType.ShakePosition:
  214. case DOTweenAnimationType.ShakeRotation:
  215. case DOTweenAnimationType.ShakeScale:
  216. _src.endValueV3 = _src.animationType == DOTweenAnimationType.ShakeRotation ? new Vector3(90,90,90) : Vector3.one;
  217. _src.optionalInt0 = 10;
  218. _src.optionalFloat0 = 90;
  219. _src.optionalBool0 = false;
  220. break;
  221. case DOTweenAnimationType.CameraAspect:
  222. case DOTweenAnimationType.CameraFieldOfView:
  223. case DOTweenAnimationType.CameraOrthoSize:
  224. _src.endValueFloat = 0;
  225. break;
  226. case DOTweenAnimationType.CameraPixelRect:
  227. case DOTweenAnimationType.CameraRect:
  228. _src.endValueRect = new Rect(0, 0, 0, 0);
  229. break;
  230. }
  231. }
  232. if (_src.animationType == DOTweenAnimationType.None) {
  233. _src.isValid = false;
  234. if (GUI.changed) EditorUtility.SetDirty(_src);
  235. return;
  236. }
  237. if (prevAnimType != _src.animationType || ComponentsChanged()) {
  238. _src.isValid = Validate();
  239. // See if we need to choose between multiple targets
  240. if (_src.animationType == DOTweenAnimationType.Fade && _src.GetComponent<CanvasGroup>() != null && _src.GetComponent<Image>() != null) {
  241. _chooseTargetMode = ChooseTargetMode.BetweenCanvasGroupAndImage;
  242. // Reassign target and forcedTargetType if lost
  243. if (_src.forcedTargetType == TargetType.Unset) _src.forcedTargetType = _src.targetType;
  244. switch (_src.forcedTargetType) {
  245. case TargetType.CanvasGroup:
  246. _src.target = _src.GetComponent<CanvasGroup>();
  247. break;
  248. case TargetType.Image:
  249. _src.target = _src.GetComponent<Image>();
  250. break;
  251. }
  252. } else {
  253. _chooseTargetMode = ChooseTargetMode.None;
  254. _src.forcedTargetType = TargetType.Unset;
  255. }
  256. }
  257. if (!_src.isValid) {
  258. GUI.color = Color.red;
  259. GUILayout.BeginVertical(GUI.skin.box);
  260. GUILayout.Label("No valid Component was found for the selected animation", EditorGUIUtils.wordWrapLabelStyle);
  261. GUILayout.EndVertical();
  262. GUI.color = Color.white;
  263. if (GUI.changed) EditorUtility.SetDirty(_src);
  264. return;
  265. }
  266. // Special cases in which multiple target types could be used (set after validation)
  267. if (_chooseTargetMode == ChooseTargetMode.BetweenCanvasGroupAndImage && _src.forcedTargetType != TargetType.Unset) {
  268. FadeTargetType fadeTargetType = (FadeTargetType)Enum.Parse(typeof(FadeTargetType), _src.forcedTargetType.ToString());
  269. TargetType prevTargetType = _src.forcedTargetType;
  270. _src.forcedTargetType = (TargetType)Enum.Parse(typeof(TargetType), EditorGUILayout.EnumPopup(_src.animationType + " Target", fadeTargetType).ToString());
  271. if (_src.forcedTargetType != prevTargetType) {
  272. // Target type change > assign correct target
  273. switch (_src.forcedTargetType) {
  274. case TargetType.CanvasGroup:
  275. _src.target = _src.GetComponent<CanvasGroup>();
  276. break;
  277. case TargetType.Image:
  278. _src.target = _src.GetComponent<Image>();
  279. break;
  280. }
  281. }
  282. }
  283. GUILayout.BeginHorizontal();
  284. _src.duration = EditorGUILayout.FloatField("Duration", _src.duration);
  285. if (_src.duration < 0) _src.duration = 0;
  286. _src.isSpeedBased = DeGUILayout.ToggleButton(_src.isSpeedBased, new GUIContent("SpeedBased", "If selected, the duration will count as units/degree x second"), DeGUI.styles.button.tool, GUILayout.Width(75));
  287. GUILayout.EndHorizontal();
  288. _src.delay = EditorGUILayout.FloatField("Delay", _src.delay);
  289. if (_src.delay < 0) _src.delay = 0;
  290. _src.isIndependentUpdate = EditorGUILayout.Toggle("Ignore TimeScale", _src.isIndependentUpdate);
  291. _src.easeType = EditorGUIUtils.FilteredEasePopup(_src.easeType);
  292. if (_src.easeType == Ease.INTERNAL_Custom) {
  293. _src.easeCurve = EditorGUILayout.CurveField(" Ease Curve", _src.easeCurve);
  294. }
  295. _src.loops = EditorGUILayout.IntField(new GUIContent("Loops", "Set to -1 for infinite loops"), _src.loops);
  296. if (_src.loops < -1) _src.loops = -1;
  297. if (_src.loops > 1 || _src.loops == -1)
  298. _src.loopType = (LoopType)EditorGUILayout.EnumPopup(" Loop Type", _src.loopType);
  299. _src.id = EditorGUILayout.TextField("ID", _src.id);
  300. bool canBeRelative = true;
  301. // End value and eventual specific options
  302. switch (_src.animationType) {
  303. case DOTweenAnimationType.Move:
  304. case DOTweenAnimationType.LocalMove:
  305. GUIEndValueV3(_src.animationType == DOTweenAnimationType.Move);
  306. _src.optionalBool0 = EditorGUILayout.Toggle(" Snapping", _src.optionalBool0);
  307. canBeRelative = !_src.useTargetAsV3;
  308. break;
  309. case DOTweenAnimationType.Rotate:
  310. case DOTweenAnimationType.LocalRotate:
  311. if (_src.GetComponent<Rigidbody2D>()) GUIEndValueFloat();
  312. else {
  313. GUIEndValueV3();
  314. _src.optionalRotationMode = (RotateMode)EditorGUILayout.EnumPopup(" Rotation Mode", _src.optionalRotationMode);
  315. }
  316. break;
  317. case DOTweenAnimationType.Scale:
  318. if (_src.optionalBool0) GUIEndValueFloat();
  319. else GUIEndValueV3();
  320. _src.optionalBool0 = EditorGUILayout.Toggle("Uniform Scale", _src.optionalBool0);
  321. break;
  322. case DOTweenAnimationType.UIWidthHeight:
  323. if (_src.optionalBool0) GUIEndValueFloat();
  324. else GUIEndValueV2();
  325. _src.optionalBool0 = EditorGUILayout.Toggle("Uniform Scale", _src.optionalBool0);
  326. break;
  327. case DOTweenAnimationType.Color:
  328. GUIEndValueColor();
  329. canBeRelative = false;
  330. break;
  331. case DOTweenAnimationType.Fade:
  332. GUIEndValueFloat();
  333. if (_src.endValueFloat < 0) _src.endValueFloat = 0;
  334. if (!_isLightSrc && _src.endValueFloat > 1) _src.endValueFloat = 1;
  335. canBeRelative = false;
  336. break;
  337. case DOTweenAnimationType.Text:
  338. GUIEndValueString();
  339. _src.optionalBool0 = EditorGUILayout.Toggle("Rich Text Enabled", _src.optionalBool0);
  340. _src.optionalScrambleMode = (ScrambleMode)EditorGUILayout.EnumPopup("Scramble Mode", _src.optionalScrambleMode);
  341. _src.optionalString = EditorGUILayout.TextField(new GUIContent("Custom Scramble", "Custom characters to use in case of ScrambleMode.Custom"), _src.optionalString);
  342. break;
  343. case DOTweenAnimationType.PunchPosition:
  344. case DOTweenAnimationType.PunchRotation:
  345. case DOTweenAnimationType.PunchScale:
  346. GUIEndValueV3();
  347. canBeRelative = false;
  348. _src.optionalInt0 = EditorGUILayout.IntSlider(new GUIContent(" Vibrato", "How much will the punch vibrate"), _src.optionalInt0, 1, 50);
  349. _src.optionalFloat0 = EditorGUILayout.Slider(new GUIContent(" Elasticity", "How much the vector will go beyond the starting position when bouncing backwards"), _src.optionalFloat0, 0, 1);
  350. if (_src.animationType == DOTweenAnimationType.PunchPosition) _src.optionalBool0 = EditorGUILayout.Toggle(" Snapping", _src.optionalBool0);
  351. break;
  352. case DOTweenAnimationType.ShakePosition:
  353. case DOTweenAnimationType.ShakeRotation:
  354. case DOTweenAnimationType.ShakeScale:
  355. GUIEndValueV3();
  356. canBeRelative = false;
  357. _src.optionalInt0 = EditorGUILayout.IntSlider(new GUIContent(" Vibrato", "How much will the shake vibrate"), _src.optionalInt0, 1, 50);
  358. _src.optionalFloat0 = EditorGUILayout.Slider(new GUIContent(" Randomness", "The shake randomness"), _src.optionalFloat0, 0, 90);
  359. if (_src.animationType == DOTweenAnimationType.ShakePosition) _src.optionalBool0 = EditorGUILayout.Toggle(" Snapping", _src.optionalBool0);
  360. break;
  361. case DOTweenAnimationType.CameraAspect:
  362. case DOTweenAnimationType.CameraFieldOfView:
  363. case DOTweenAnimationType.CameraOrthoSize:
  364. GUIEndValueFloat();
  365. canBeRelative = false;
  366. break;
  367. case DOTweenAnimationType.CameraBackgroundColor:
  368. GUIEndValueColor();
  369. canBeRelative = false;
  370. break;
  371. case DOTweenAnimationType.CameraPixelRect:
  372. case DOTweenAnimationType.CameraRect:
  373. GUIEndValueRect();
  374. canBeRelative = false;
  375. break;
  376. }
  377. // Final settings
  378. if (canBeRelative) _src.isRelative = EditorGUILayout.Toggle(" Relative", _src.isRelative);
  379. // Events
  380. AnimationInspectorGUI.AnimationEvents(this, _src);
  381. if (GUI.changed) EditorUtility.SetDirty(_src);
  382. }
  383. #endregion
  384. #region Methods
  385. // Returns TRUE if the Component layout on the src gameObject changed (a Component was added or removed)
  386. bool ComponentsChanged()
  387. {
  388. int prevTotComponentsOnSrc = _totComponentsOnSrc;
  389. _totComponentsOnSrc = _src.gameObject.GetComponents<Component>().Length;
  390. return prevTotComponentsOnSrc != _totComponentsOnSrc;
  391. }
  392. // Checks if a Component that can be animated with the given animationType is attached to the src
  393. bool Validate()
  394. {
  395. if (_src.animationType == DOTweenAnimationType.None) return false;
  396. Component srcTarget;
  397. // First check for external plugins
  398. #if DOTWEEN_TK2D
  399. if (_Tk2dAnimationTypeToComponent.ContainsKey(_src.animationType)) {
  400. foreach (Type t in _Tk2dAnimationTypeToComponent[_src.animationType]) {
  401. srcTarget = _src.GetComponent(t);
  402. if (srcTarget != null) {
  403. _src.target = srcTarget;
  404. _src.targetType = DOTweenAnimation.TypeToDOTargetType(t);
  405. return true;
  406. }
  407. }
  408. }
  409. #endif
  410. #if DOTWEEN_TMP
  411. if (_TMPAnimationTypeToComponent.ContainsKey(_src.animationType)) {
  412. foreach (Type t in _TMPAnimationTypeToComponent[_src.animationType]) {
  413. srcTarget = _src.GetComponent(t);
  414. if (srcTarget != null) {
  415. _src.target = srcTarget;
  416. _src.targetType = DOTweenAnimation.TypeToDOTargetType(t);
  417. return true;
  418. }
  419. }
  420. }
  421. #endif
  422. // Then check for regular stuff
  423. if (_AnimationTypeToComponent.ContainsKey(_src.animationType)) {
  424. foreach (Type t in _AnimationTypeToComponent[_src.animationType]) {
  425. srcTarget = _src.GetComponent(t);
  426. if (srcTarget != null) {
  427. _src.target = srcTarget;
  428. _src.targetType = DOTweenAnimation.TypeToDOTargetType(t);
  429. return true;
  430. }
  431. }
  432. }
  433. return false;
  434. }
  435. DOTweenAnimationType AnimationToDOTweenAnimationType(string animation)
  436. {
  437. if (_datString == null) _datString = Enum.GetNames(typeof(DOTweenAnimationType));
  438. animation = animation.Replace("/", "");
  439. return (DOTweenAnimationType)(Array.IndexOf(_datString, animation));
  440. }
  441. int DOTweenAnimationTypeToPopupId(DOTweenAnimationType animation)
  442. {
  443. return Array.IndexOf(_animationTypeNoSlashes, animation.ToString());
  444. }
  445. #endregion
  446. #region GUI Draw Methods
  447. void GUIEndValueFloat()
  448. {
  449. GUILayout.BeginHorizontal();
  450. GUIToFromButton();
  451. _src.endValueFloat = EditorGUILayout.FloatField(_src.endValueFloat);
  452. GUILayout.EndHorizontal();
  453. }
  454. void GUIEndValueColor()
  455. {
  456. GUILayout.BeginHorizontal();
  457. GUIToFromButton();
  458. _src.endValueColor = EditorGUILayout.ColorField(_src.endValueColor);
  459. GUILayout.EndHorizontal();
  460. }
  461. void GUIEndValueV3(bool optionalTransform = false)
  462. {
  463. GUILayout.BeginHorizontal();
  464. GUIToFromButton();
  465. if (_src.useTargetAsV3) {
  466. Transform prevT = _src.endValueTransform;
  467. _src.endValueTransform = EditorGUILayout.ObjectField(_src.endValueTransform, typeof(Transform), true) as Transform;
  468. if (_src.endValueTransform != prevT && _src.endValueTransform != null) {
  469. // Check that it's a Transform for a Transform or a RectTransform for a RectTransform
  470. if (_src.GetComponent<RectTransform>() != null) {
  471. if (_src.endValueTransform.GetComponent<RectTransform>() == null) {
  472. EditorUtility.DisplayDialog("DOTween Pro", "For Unity UI elements, the target must also be a UI element", "Ok");
  473. _src.endValueTransform = null;
  474. }
  475. } else if (_src.endValueTransform.GetComponent<RectTransform>() != null) {
  476. EditorUtility.DisplayDialog("DOTween Pro", "You can't use a UI target for a non UI object", "Ok");
  477. _src.endValueTransform = null;
  478. }
  479. }
  480. } else {
  481. _src.endValueV3 = EditorGUILayout.Vector3Field("", _src.endValueV3, GUILayout.Height(16));
  482. }
  483. if (optionalTransform) {
  484. if (GUILayout.Button(_src.useTargetAsV3 ? "target" : "value", EditorGUIUtils.sideBtStyle, GUILayout.Width(44))) _src.useTargetAsV3 = !_src.useTargetAsV3;
  485. }
  486. GUILayout.EndHorizontal();
  487. if (_src.useTargetAsV3 && _src.endValueTransform != null && _src.target is RectTransform) {
  488. EditorGUILayout.HelpBox("NOTE: when using a UI target, the tween will be created during Start instead of Awake", MessageType.Info);
  489. }
  490. }
  491. void GUIEndValueV2()
  492. {
  493. GUILayout.BeginHorizontal();
  494. GUIToFromButton();
  495. _src.endValueV2 = EditorGUILayout.Vector2Field("", _src.endValueV2, GUILayout.Height(16));
  496. GUILayout.EndHorizontal();
  497. }
  498. void GUIEndValueString()
  499. {
  500. GUILayout.BeginHorizontal();
  501. GUIToFromButton();
  502. _src.endValueString = EditorGUILayout.TextArea(_src.endValueString, EditorGUIUtils.wordWrapTextArea);
  503. GUILayout.EndHorizontal();
  504. }
  505. void GUIEndValueRect()
  506. {
  507. GUILayout.BeginHorizontal();
  508. GUIToFromButton();
  509. _src.endValueRect = EditorGUILayout.RectField(_src.endValueRect);
  510. GUILayout.EndHorizontal();
  511. }
  512. void GUIToFromButton()
  513. {
  514. if (GUILayout.Button(_src.isFrom ? "FROM" : "TO", EditorGUIUtils.sideBtStyle, GUILayout.Width(90))) _src.isFrom = !_src.isFrom;
  515. GUILayout.Space(16);
  516. }
  517. #endregion
  518. }
  519. }