DOTweenAnimationInspector.cs 28 KB

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