UIEffectTweenerEditor.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.Profiling;
  5. namespace Coffee.UIEffects.Editors
  6. {
  7. [CanEditMultipleObjects]
  8. [CustomEditor(typeof(UIEffectTweener))]
  9. internal class UIEffectTweenerEditor : Editor
  10. {
  11. private SerializedProperty _cullingMask;
  12. private SerializedProperty _direction;
  13. private SerializedProperty _curve;
  14. private SerializedProperty _delay;
  15. private SerializedProperty _duration;
  16. private SerializedProperty _interval;
  17. private SerializedProperty _playOnEnable;
  18. private SerializedProperty _resetTimeOnEnable;
  19. private SerializedProperty _updateMode;
  20. private SerializedProperty _wrapMode;
  21. private SerializedProperty _onComplete;
  22. private bool _isPlaying = false;
  23. private double _lastTime;
  24. private void OnEnable()
  25. {
  26. _cullingMask = serializedObject.FindProperty("m_CullingMask");
  27. _direction = serializedObject.FindProperty("m_Direction");
  28. _curve = serializedObject.FindProperty("m_Curve");
  29. _playOnEnable = serializedObject.FindProperty("m_PlayOnEnable");
  30. _resetTimeOnEnable = serializedObject.FindProperty("m_ResetTimeOnEnable");
  31. _delay = serializedObject.FindProperty("m_Delay");
  32. _duration = serializedObject.FindProperty("m_Duration");
  33. _interval = serializedObject.FindProperty("m_Interval");
  34. _wrapMode = serializedObject.FindProperty("m_WrapMode");
  35. _updateMode = serializedObject.FindProperty("m_UpdateMode");
  36. _onComplete = serializedObject.FindProperty("m_OnComplete");
  37. EditorApplication.update += UpdateTweeners;
  38. }
  39. private void OnDisable()
  40. {
  41. EditorApplication.update -= UpdateTweeners;
  42. }
  43. public override void OnInspectorGUI()
  44. {
  45. Profiler.BeginSample("(UIE)[UIEffectTweener] OnInspectorGUI");
  46. serializedObject.UpdateIfRequiredOrScript();
  47. EditorGUILayout.PropertyField(_cullingMask);
  48. EditorGUILayout.PropertyField(_direction);
  49. EditorGUILayout.PropertyField(_curve);
  50. EditorGUILayout.PropertyField(_delay);
  51. EditorGUILayout.PropertyField(_duration);
  52. EditorGUILayout.PropertyField(_interval);
  53. EditorGUILayout.PropertyField(_playOnEnable);
  54. EditorGUILayout.PropertyField(_resetTimeOnEnable);
  55. EditorGUILayout.PropertyField(_wrapMode);
  56. EditorGUILayout.PropertyField(_updateMode);
  57. EditorGUILayout.PropertyField(_onComplete);
  58. serializedObject.ApplyModifiedProperties();
  59. DrawPlayer(target as UIEffectTweener);
  60. Profiler.EndSample();
  61. }
  62. private void DrawPlayer(UIEffectTweener tweener)
  63. {
  64. if (!tweener) return;
  65. EditorGUILayout.Space(4);
  66. GUILayout.Label(GUIContent.none, "sv_iconselector_sep", GUILayout.ExpandWidth(true));
  67. EditorGUILayout.BeginHorizontal();
  68. var r = EditorGUILayout.GetControlRect(false);
  69. var rResetTimeButton = new Rect(r.x, r.y, 20, r.height);
  70. if (GUI.Button(rResetTimeButton, EditorGUIUtility.IconContent("animation.firstkey"), "IconButton"))
  71. {
  72. ResetTime();
  73. }
  74. var rPlayButton = new Rect(r.x + 20, r.y, 20, r.height);
  75. if (GUI.Button(rPlayButton, EditorGUIUtility.IconContent("playbutton"), "IconButton"))
  76. {
  77. SetPlaying(true);
  78. }
  79. var rPauseButton = new Rect(r.x + 40, r.y, 20, r.height);
  80. if (GUI.Button(rPauseButton, EditorGUIUtility.IconContent("pausebutton"), "IconButton"))
  81. {
  82. SetPlaying(false);
  83. }
  84. var totalTime = tweener.totalTime;
  85. var time = tweener.time;
  86. var label = EditorGUIUtility.TrTempContent($"{time:N2}/{totalTime:N2}");
  87. var rLabel = new Rect(r.x + r.width - 80, r.y, 80, r.height);
  88. GUI.Label(rLabel, label, "RightLabel");
  89. EditorGUILayout.EndHorizontal();
  90. EditorGUI.BeginChangeCheck();
  91. var rSlider = new Rect(r.x + 60, r.y, r.width - 140, r.height);
  92. var r0 = new Rect(rSlider.x, rSlider.y + 4, rSlider.width, rSlider.height - 8);
  93. r0.x += DrawBackground(r0, rSlider.width * tweener.delay / totalTime, Color.blue);
  94. r0.x += DrawBackground(r0, rSlider.width * tweener.duration / totalTime, Color.green);
  95. if (UIEffectTweener.WrapMode.Loop <= tweener.wrapMode)
  96. {
  97. r0.x += DrawBackground(r0, rSlider.width * tweener.interval / totalTime, Color.red);
  98. }
  99. if (UIEffectTweener.WrapMode.PingPongOnce <= tweener.wrapMode)
  100. {
  101. r0.x += DrawBackground(r0, rSlider.width * tweener.duration / totalTime, Color.green);
  102. }
  103. if (UIEffectTweener.WrapMode.PingPongLoop <= tweener.wrapMode)
  104. {
  105. r0.x += DrawBackground(r0, rSlider.width * tweener.interval / totalTime, Color.red);
  106. }
  107. GUI.color = Color.white;
  108. time = GUI.HorizontalSlider(rSlider, time, 0, totalTime);
  109. if (EditorGUI.EndChangeCheck())
  110. {
  111. SetTime(time);
  112. }
  113. if (Application.isPlaying && tweener.isActiveAndEnabled)
  114. {
  115. Repaint();
  116. }
  117. }
  118. private static float DrawBackground(Rect r, float width, Color color)
  119. {
  120. r.width = width;
  121. GUI.color = color;
  122. GUI.Label(r, GUIContent.none, "TE DefaultTime");
  123. return width;
  124. }
  125. private void SetTime(float time)
  126. {
  127. foreach (var tweener in targets.OfType<UIEffectTweener>())
  128. {
  129. tweener.SetTime(time);
  130. }
  131. }
  132. private void ResetTime()
  133. {
  134. foreach (var tweener in targets.OfType<UIEffectTweener>())
  135. {
  136. tweener.ResetTime(tweener.direction);
  137. }
  138. }
  139. private void SetPlaying(bool enable)
  140. {
  141. if (!EditorApplication.isPlaying)
  142. {
  143. _isPlaying = enable;
  144. _lastTime = EditorApplication.timeSinceStartup;
  145. return;
  146. }
  147. foreach (var tweener in targets.OfType<UIEffectTweener>())
  148. {
  149. if (enable)
  150. {
  151. tweener.Play(false);
  152. }
  153. else
  154. {
  155. tweener.SetPause(true);
  156. }
  157. }
  158. }
  159. private void UpdateTweeners()
  160. {
  161. if (!_isPlaying || EditorApplication.isPlayingOrWillChangePlaymode) return;
  162. var delta = (float)(EditorApplication.timeSinceStartup - _lastTime);
  163. _lastTime = EditorApplication.timeSinceStartup;
  164. foreach (var tweener in targets.OfType<UIEffectTweener>())
  165. {
  166. tweener.UpdateTime(tweener.direction == UIEffectTweener.Direction.Forward ? delta : -delta);
  167. }
  168. EditorApplication.QueuePlayerLoopUpdate();
  169. }
  170. }
  171. }