DOTweenPreviewManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 DG.DemiEditor;
  6. using DG.DemiLib;
  7. using DG.Tweening;
  8. using DG.Tweening.Core;
  9. using UnityEditor;
  10. using UnityEditorInternal;
  11. using UnityEngine;
  12. using Object = UnityEngine.Object;
  13. namespace DG.DOTweenEditor
  14. {
  15. public static class DOTweenPreviewManager
  16. {
  17. static bool _previewOnlyIfSetToAutoPlay = true;
  18. static readonly Dictionary<DOTweenAnimation,TweenInfo> _AnimationToTween = new Dictionary<DOTweenAnimation,TweenInfo>();
  19. static readonly List<DOTweenAnimation> _TmpKeys = new List<DOTweenAnimation>();
  20. #region Public Methods & GUI
  21. /// <summary>
  22. /// Returns TRUE if its actually previewing animations
  23. /// </summary>
  24. public static bool PreviewGUI(DOTweenAnimation src)
  25. {
  26. if (EditorApplication.isPlaying) return false;
  27. Styles.Init();
  28. bool isPreviewing = _AnimationToTween.Count > 0;
  29. bool isPreviewingThis = isPreviewing && _AnimationToTween.ContainsKey(src);
  30. // Preview in editor
  31. GUI.backgroundColor = isPreviewing
  32. ? new DeSkinColor(new Color(0.49f, 0.8f, 0.86f), new Color(0.15f, 0.26f, 0.35f))
  33. : new DeSkinColor(Color.white, new Color(0.13f, 0.13f, 0.13f));
  34. GUILayout.BeginVertical(Styles.previewBox);
  35. DeGUI.ResetGUIColors();
  36. GUILayout.BeginHorizontal();
  37. GUILayout.Label("Preview Mode - Experimental", Styles.previewLabel);
  38. _previewOnlyIfSetToAutoPlay = DeGUILayout.ToggleButton(
  39. _previewOnlyIfSetToAutoPlay,
  40. new GUIContent("AutoPlay only", "If toggled only previews animations that have AutoPlay turned ON"),
  41. Styles.btOption
  42. );
  43. GUILayout.EndHorizontal();
  44. GUILayout.Space(1);
  45. // Preview - Play
  46. GUILayout.BeginHorizontal();
  47. EditorGUI.BeginDisabledGroup(
  48. isPreviewingThis || src.animationType == DOTweenAnimation.AnimationType.None
  49. || !src.isActive || _previewOnlyIfSetToAutoPlay && !src.autoPlay
  50. );
  51. if (GUILayout.Button("► Play", Styles.btPreview)) {
  52. if (!isPreviewing) StartupGlobalPreview();
  53. AddAnimationToGlobalPreview(src);
  54. }
  55. EditorGUI.EndDisabledGroup();
  56. EditorGUI.BeginDisabledGroup(isPreviewing);
  57. if (GUILayout.Button("► Play All <i>on GameObject</i>", Styles.btPreview)) {
  58. if (!isPreviewing) StartupGlobalPreview();
  59. DOTweenAnimation[] anims = src.gameObject.GetComponents<DOTweenAnimation>();
  60. foreach (DOTweenAnimation anim in anims) AddAnimationToGlobalPreview(anim);
  61. }
  62. if (GUILayout.Button("► Play All <i>in Scene</i>", Styles.btPreview)) {
  63. if (!isPreviewing) StartupGlobalPreview();
  64. DOTweenAnimation[] anims = Object.FindObjectsOfType<DOTweenAnimation>();
  65. foreach (DOTweenAnimation anim in anims) AddAnimationToGlobalPreview(anim);
  66. }
  67. EditorGUI.EndDisabledGroup();
  68. GUILayout.EndHorizontal();
  69. // Preview - Stop
  70. GUILayout.BeginHorizontal();
  71. EditorGUI.BeginDisabledGroup(!isPreviewingThis);
  72. if (GUILayout.Button("■ Stop", Styles.btPreview)) {
  73. if (_AnimationToTween.ContainsKey(src)) StopPreview(_AnimationToTween[src].tween);
  74. }
  75. EditorGUI.EndDisabledGroup();
  76. EditorGUI.BeginDisabledGroup(!isPreviewing);
  77. if (GUILayout.Button("■ Stop All <i>on GameObject</i>", Styles.btPreview)) {
  78. StopPreview(src.gameObject);
  79. }
  80. if (GUILayout.Button("■ Stop All <i>in Scene</i>", Styles.btPreview)) {
  81. StopAllPreviews();
  82. }
  83. EditorGUI.EndDisabledGroup();
  84. GUILayout.EndHorizontal();
  85. if (isPreviewing) {
  86. int playingTweens = 0;
  87. int completedTweens = 0;
  88. int pausedTweens = 0;
  89. foreach (KeyValuePair<DOTweenAnimation, TweenInfo> kvp in _AnimationToTween) {
  90. Tween t = kvp.Value.tween;
  91. if (t.IsPlaying()) playingTweens++;
  92. else if (t.IsComplete()) completedTweens++;
  93. else pausedTweens++;
  94. }
  95. GUILayout.Label("Playing Tweens: " + playingTweens, Styles.previewStatusLabel);
  96. GUILayout.Label("Completed Tweens: " + completedTweens, Styles.previewStatusLabel);
  97. // GUILayout.Label("Paused Tweens: " + playingTweens);
  98. }
  99. GUILayout.EndVertical();
  100. return isPreviewing;
  101. }
  102. #if !(UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5)
  103. public static void StopAllPreviews(PlayModeStateChange state)
  104. {
  105. StopAllPreviews();
  106. }
  107. #endif
  108. public static void StopAllPreviews()
  109. {
  110. _TmpKeys.Clear();
  111. foreach (KeyValuePair<DOTweenAnimation,TweenInfo> kvp in _AnimationToTween) {
  112. _TmpKeys.Add(kvp.Key);
  113. }
  114. StopPreview(_TmpKeys);
  115. _TmpKeys.Clear();
  116. _AnimationToTween.Clear();
  117. DOTweenEditorPreview.Stop();
  118. #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5
  119. UnityEditor.EditorApplication.playmodeStateChanged -= StopAllPreviews;
  120. #else
  121. UnityEditor.EditorApplication.playModeStateChanged -= StopAllPreviews;
  122. #endif
  123. // EditorApplication.playmodeStateChanged -= StopAllPreviews;
  124. InternalEditorUtility.RepaintAllViews();
  125. }
  126. #endregion
  127. #region Methods
  128. static void StartupGlobalPreview()
  129. {
  130. DOTweenEditorPreview.Start();
  131. #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5
  132. UnityEditor.EditorApplication.playmodeStateChanged += StopAllPreviews;
  133. #else
  134. UnityEditor.EditorApplication.playModeStateChanged += StopAllPreviews;
  135. #endif
  136. // EditorApplication.playmodeStateChanged += StopAllPreviews;
  137. }
  138. static void AddAnimationToGlobalPreview(DOTweenAnimation src)
  139. {
  140. if (!src.isActive) return; // Ignore sources whose tweens have been set to inactive
  141. if (_previewOnlyIfSetToAutoPlay && !src.autoPlay) return;
  142. Tween t = src.CreateEditorPreview();
  143. _AnimationToTween.Add(src, new TweenInfo(src, t, src.isFrom));
  144. // Tween setup
  145. DOTweenEditorPreview.PrepareTweenForPreview(t);
  146. }
  147. static void StopPreview(GameObject go)
  148. {
  149. _TmpKeys.Clear();
  150. foreach (KeyValuePair<DOTweenAnimation,TweenInfo> kvp in _AnimationToTween) {
  151. if (kvp.Key.gameObject != go) continue;
  152. _TmpKeys.Add(kvp.Key);
  153. }
  154. StopPreview(_TmpKeys);
  155. _TmpKeys.Clear();
  156. if (_AnimationToTween.Count == 0) StopAllPreviews();
  157. else InternalEditorUtility.RepaintAllViews();
  158. }
  159. static void StopPreview(Tween t)
  160. {
  161. TweenInfo tInfo = null;
  162. foreach (KeyValuePair<DOTweenAnimation,TweenInfo> kvp in _AnimationToTween) {
  163. if (kvp.Value.tween != t) continue;
  164. tInfo = kvp.Value;
  165. _AnimationToTween.Remove(kvp.Key);
  166. break;
  167. }
  168. if (tInfo == null) {
  169. Debug.LogWarning("DOTween Preview ► Couldn't find tween to stop");
  170. return;
  171. }
  172. if (tInfo.isFrom) {
  173. int totLoops = tInfo.tween.Loops();
  174. if (totLoops < 0 || totLoops > 1) {
  175. tInfo.tween.Goto(tInfo.tween.Duration(false));
  176. } else tInfo.tween.Complete();
  177. } else tInfo.tween.Rewind();
  178. tInfo.tween.Kill();
  179. EditorUtility.SetDirty(tInfo.animation); // Refresh views
  180. if (_AnimationToTween.Count == 0) StopAllPreviews();
  181. else InternalEditorUtility.RepaintAllViews();
  182. }
  183. // Stops while iterating inversely, which deals better with tweens that overwrite each other
  184. static void StopPreview(List<DOTweenAnimation> keys)
  185. {
  186. for (int i = keys.Count - 1; i > -1; --i) {
  187. DOTweenAnimation anim = keys[i];
  188. TweenInfo tInfo = _AnimationToTween[anim];
  189. if (tInfo.isFrom) {
  190. int totLoops = tInfo.tween.Loops();
  191. if (totLoops < 0 || totLoops > 1) {
  192. tInfo.tween.Goto(tInfo.tween.Duration(false));
  193. } else tInfo.tween.Complete();
  194. } else tInfo.tween.Rewind();
  195. tInfo.tween.Kill();
  196. EditorUtility.SetDirty(anim); // Refresh views
  197. _AnimationToTween.Remove(anim);
  198. }
  199. }
  200. #endregion
  201. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  202. // ███ INTERNAL CLASSES ████████████████████████████████████████████████████████████████████████████████████████████████
  203. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  204. class TweenInfo
  205. {
  206. public DOTweenAnimation animation;
  207. public Tween tween;
  208. public bool isFrom;
  209. public TweenInfo(DOTweenAnimation animation, Tween tween, bool isFrom)
  210. {
  211. this.animation = animation;
  212. this.tween = tween;
  213. this.isFrom = isFrom;
  214. }
  215. }
  216. static class Styles
  217. {
  218. static bool _initialized;
  219. public static GUIStyle previewBox, previewLabel, btOption, btPreview, previewStatusLabel;
  220. public static void Init()
  221. {
  222. if (_initialized) return;
  223. _initialized = true;
  224. previewBox = new GUIStyle(GUI.skin.box).Clone().Padding(1, 1, 0, 3)
  225. .Background(DeStylePalette.squareBorderCurved_darkBorders).Border(7, 7, 7, 7);
  226. previewLabel = new GUIStyle(GUI.skin.label).Clone(10, FontStyle.Bold).Padding(1, 0, 3, 0).Margin(3, 6, 0, 0).StretchWidth(false);
  227. btOption = DeGUI.styles.button.bBlankBorderCompact.MarginBottom(2).MarginRight(4);
  228. btPreview = EditorStyles.miniButton.Clone(Format.RichText);
  229. previewStatusLabel = EditorStyles.miniLabel.Clone().Padding(4, 0, 0, 0).Margin(0);
  230. }
  231. }
  232. }
  233. }