From 3bd21c73384906267a2a4c48acdb96df77bd1f67 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 30 May 2021 11:05:38 +0800 Subject: *tween --- .../Animation/Tween/Editor/PlaybackTimer.cs | 70 ++++++++++++ .../Animation/Tween/Editor/PlaybackTimer.cs.meta | 11 ++ .../Tween/Editor/TweenAnimationInspector.cs | 125 ++++++++++++++++----- .../Tween/Editor/TweenAnimationInspector_Alpha.cs | 2 +- .../Tween/Editor/TweenAnimationInspector_Color.cs | 2 - .../Tween/Editor/TweenAnimationInspector_Drag.cs | 58 ++++++++++ .../Editor/TweenAnimationInspector_Drag.cs.meta | 11 ++ .../Tween/Editor/TweenAnimationInspector_Play.cs | 63 ++++++++++- .../Editor/TweenAnimationInspector_RectSize.cs | 2 - 9 files changed, 306 insertions(+), 38 deletions(-) create mode 100644 Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs create mode 100644 Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs.meta create mode 100644 Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Drag.cs create mode 100644 Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Drag.cs.meta (limited to 'Assets/UI_Extension/Scripts/Animation/Tween/Editor') diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs new file mode 100644 index 0000000..4890f2b --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs @@ -0,0 +1,70 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; +namespace TweenAnimation +{ + public class PlaybackTimer + { + // 考虑了Pause的playback时间 + public float time + { + get + { + if (m_PauseRealTime != -1) + return m_PauseRealTime - m_StartRealTime; + return realTime; + } + } + + // 从调用Resume开始后的时间 + private float realTime + { + get + { + return (float)EditorApplication.timeSinceStartup - m_StartRealTime; + } + } + + private float m_StartRealTime; + + private float m_PauseRealTime; + + public PlaybackTimer() + { + Reset(); + } + + //Start & Resume + public void Resume() + { + if (m_PauseRealTime != -1) + m_StartRealTime = (float)EditorApplication.timeSinceStartup - time; + else + m_StartRealTime = (float)EditorApplication.timeSinceStartup; + m_PauseRealTime = -1; + } + + public void Pause() + { + m_PauseRealTime = (float)EditorApplication.timeSinceStartup; + } + + public void Stop() + { + Reset(); + } + + public void Reset() + { + m_StartRealTime = -1; + m_PauseRealTime = -1; + } + + public void SetPauseTimeOffset(float dt) + { + m_PauseRealTime += dt; + } + + } +} \ No newline at end of file diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs.meta b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs.meta new file mode 100644 index 0000000..367e685 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 609e015dff09c2c47ae8469c7574e88e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector.cs index cc57055..bd98aa8 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector.cs @@ -11,6 +11,13 @@ namespace TweenAnimation [CustomEditor(typeof(TweenAnimation), false)] public partial class TweenAnimationInspector : Editor { + static Dictionary s_TweenModuleClasses; + static string[] s_TweenModuleClassNames; + static Dictionary s_TweenModuleGUI; + + static int s_ModuleTabHash = "TweenModuleTab".GetHashCode(); + static int s_ModuleEnabledHash = "TweenModuleEnabled".GetHashCode(); + TweenModuleGUIStyles styles { get @@ -27,17 +34,8 @@ namespace TweenAnimation } } - float value = 20; - TweenAnimation animation; - bool m_ShowAnimationTab = true; - - static Dictionary s_TweenModuleClasses; - static string[] s_TweenModuleClassNames; - static Dictionary s_TweenModuleGUI; - - static int s_ModuleTabHash = "TweenModuleTab".GetHashCode(); - static int s_ModuleEnabledHash = "TweenModuleEnabled".GetHashCode(); + bool m_ShowAnimationTab = false; // 是否在时间轴上显示 bool m_ShowEvents; @@ -65,8 +63,24 @@ namespace TweenAnimation m_EventTimeSet.Clear(); m_SelectedEventTime = -1; + + m_Play = false; + EditorStop(); + + m_DragState = DragState.Release; + + if (m_PlaybackTimer == null) + m_PlaybackTimer = new PlaybackTimer(); } - + + public void OnDisable() + { + m_Play = false; + if (m_Play) + EditorStop(); + m_PlaybackTimer.Reset(); + } + public override void OnInspectorGUI() { animation = target as TweenAnimation; @@ -124,6 +138,9 @@ namespace TweenAnimation // 事件触发方向 animation.eventTriggeredDirection = (TweenAnimation.EventTriggeredDirection)ui.GUIEnumMask("Event Direction", animation.eventTriggeredDirection); + // 是否触发事件 + animation.triggerEvents = ui.GUIToggle("Trigger Events", animation.triggerEvents); + // 是否只触发一次 animation.triggerOnce = ui.GUIToggle("Trigger Once", animation.triggerOnce); @@ -134,7 +151,7 @@ namespace TweenAnimation // 时间轴 DrawRuler(1f, true); - + EventList(); } @@ -211,6 +228,8 @@ namespace TweenAnimation { if (module == null) return; + Event e = Event.current; + string name = module.name; string description = module.description; bool enabled = module.enabled; @@ -228,20 +247,32 @@ namespace TweenAnimation deleteRect.x = rect.x + rect.width - 18; deleteRect.width = 20; deleteRect.height = 20; - if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseUp || Event.current.type == EventType.Layout) - { - module.enabled = GUI.Toggle(checkRect, module.enabled, "", styles.checkmark); - if (GUI.Button(deleteRect, "-", styles.textBoldBig)) - animation.RemoveModule(module); - if (GUI.Button(headerRect, "", styles.headerBg)) - module.unfold = !module.unfold; - } - else if (Event.current.type == EventType.Repaint /*|| Event.current.type == EventType.Layout*/) - { - GUI.Button(headerRect, "", styles.headerBg); - GUI.Toggle(checkRect, enabled, "", styles.checkmark); - GUI.Button(deleteRect, "-", styles.textBoldBig); - } + //if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseUp || Event.current.type == EventType.Layout) + //{ + // module.enabled = GUI.Toggle(checkRect, module.enabled, "", styles.checkmark); + // if (GUI.Button(deleteRect, "-", styles.textBoldBig)) + // animation.RemoveModule(module); + // if (GUI.Button(headerRect, "", styles.headerBg)) + // module.unfold = !module.unfold; + //} + //else if (Event.current.type == EventType.Repaint /*|| Event.current.type == EventType.Layout*/) + //{ + // GUI.Button(headerRect, "", styles.headerBg); + // GUI.Toggle(checkRect, enabled, "", styles.checkmark); + // GUI.Button(deleteRect, "-", styles.textBoldBig); + //} + + //https://answers.unity.com/questions/1562998/how-to-draw-two-button-overlap-in-editor-window-an.html + Rect headerRegion = headerRect; + headerRegion.x += 20; + headerRegion.width -= 50; + GUI.enabled = !(e.isMouse && !headerRegion.Contains(e.mousePosition)); + if (GUI.Button(headerRect, "", styles.headerBg)) + module.unfold = !module.unfold; + GUI.enabled = true; + module.enabled = GUI.Toggle(checkRect, module.enabled, "", styles.checkmark); + if (GUI.Button(deleteRect, "-", styles.textBoldBig)) + animation.RemoveModule(module); Vector2 size = styles.text.CalcSize(new GUIContent(name)); GUI.Label(new Rect(rect.x + 15, rect.y + 2, size.x, 20), name, styles.text); @@ -398,7 +429,7 @@ namespace TweenAnimation for(int i = 0; i < animation.modules.Count; ++i) { TweenModule module = animation.modules[i]; - if(module.unfold) + if(/*module.unfold*/module.enabled) { float left = module.timeOffset / duration * rulerRect.width; float right = (module.timeOffset + module.duration) / duration * rulerRect.width; @@ -409,6 +440,14 @@ namespace TweenAnimation } } + // 绘制playback竖线 + if(bRepaint && (m_Play || m_Pause)) + { + float time = animation.HandleTime(m_PlaybackTimer.time); + float x = (time / animation.duration) * rulerRect.width + 5; + ui.DrawVerticalLineFast(x, yOffBase, yOffBase - (rulerRect.height), Color.red); + } + GL.PopMatrix(); GL.End(); @@ -420,8 +459,9 @@ namespace TweenAnimation if (i % stepSlice == 0 || stepSlice == 10 && i % (stepSlice / 2) == 0) { float time = i * timePerStep; // sec - Vector2 size = styles.text.CalcSize(new GUIContent(time.ToString("f2"))); - GUI.Label(new Rect(rulerRect.x + i * stepPixelWidth - size.x / 2, yOffBase + 2, 50, 15), time.ToString("f2"), styles.text ); + string timeStr = time == 0 ? "0" : time.ToString("f1"); + Vector2 size = styles.text.CalcSize(new GUIContent(timeStr)); + GUI.Label(new Rect(rulerRect.x + i * stepPixelWidth - size.x / 2, yOffBase + 2, 50, 15), timeStr, styles.text ); } } } @@ -499,6 +539,7 @@ namespace TweenAnimation { m_Pause = m_Stop = false; EditorPlay(); + m_PlaybackTimer.Resume(); } } GUI.color = col; @@ -512,7 +553,13 @@ namespace TweenAnimation { m_Pause = !m_Pause; if (m_Pause) + { + EditorPlay(); + if(!m_Play) + m_PlaybackTimer.Resume(); + m_PlaybackTimer.Pause(); m_Play = m_Stop = false; + } } GUI.color = col; @@ -521,10 +568,23 @@ namespace TweenAnimation if (GUI.Button(stopRect, "Stop", styles.miniRight)) { EditorStop(); + m_PlaybackTimer.Stop(); m_Pause = m_Play = false; } } + // playback time + { + if (m_Play || m_Pause) + { + Rect playbackTimeRect = new Rect(100, yOffBase + 23, 90, 20); ; + GUI.Label(playbackTimeRect, "Playback Time: " + m_PlaybackTimer.time.ToString("f1") + "s", styles.text); + } + } + + // 拖拽进度 + DragPlay(rulerRect); + GUI.EndGroup(); } @@ -594,6 +654,7 @@ namespace TweenAnimation unfoldRect.x = rect.x + rect.width - 19; unfoldRect.width = 13; unfoldRect.height = 13; + unfoldRect.y += 3; e.unfold = GUI.Button(unfoldRect, (e.unfold ? "▲" : "▼"), styles.textSmall) ? !e.unfold : e.unfold; if(e.unfold) { @@ -626,5 +687,11 @@ namespace TweenAnimation } } + static EditorWindow s_SceneView; + + private void OnSceneGUI() + { + } + } } \ No newline at end of file diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Alpha.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Alpha.cs index 1f7c0cd..c13e312 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Alpha.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Alpha.cs @@ -21,7 +21,7 @@ namespace TweenAnimation alpha = Mathf.Clamp(alpha, 0, 1); tween.to = alpha; - tween.target = ui.GUIObject("Target", tween.target, typeof(Graphic)) as Graphic; + tween.graphic = ui.GUIObject("Graphic", tween.graphic, typeof(Graphic)) as Graphic; } } } \ No newline at end of file diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Color.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Color.cs index 2aee642..b2155d3 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Color.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Color.cs @@ -11,8 +11,6 @@ namespace TweenAnimation { OnInspector_Base(module); - value = ui.GUIFloat("Float value", value, "f2"); - } } diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Drag.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Drag.cs new file mode 100644 index 0000000..b6d9e89 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Drag.cs @@ -0,0 +1,58 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using UnityEditor; +namespace TweenAnimation +{ + + public partial class TweenAnimationInspector : Editor + { + bool m_MouseDown; + + DragState m_DragState; + + enum DragState + { + Release, + Drag, + + } + + void DragPlay(Rect rulerRect) + { + if (!m_Pause) + return; + + Event e = Event.current; + + Vector2 mousePos = e.mousePosition; + + bool bIn = rulerRect.Contains(mousePos); + + switch (m_DragState) + { + case DragState.Release: + if (e.type == EventType.MouseDown && bIn) + m_DragState = DragState.Drag; + break; + case DragState.Drag: + if (e.type == EventType.MouseUp) + m_DragState = DragState.Release; + break; + } + if (m_DragState == DragState.Drag && bIn) + { + float x = mousePos.x - rulerRect.x; + float t = x / rulerRect.width * animation.duration; + float t0 = animation.GetIdentifiedTime(m_PlaybackTimer.time); + float dt = 0; + if (t0 >= 0) + dt = t - t0; + else // pingpong + dt = -t0 - t; + m_PlaybackTimer.SetPauseTimeOffset(dt); + } + } + } +} \ No newline at end of file diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Drag.cs.meta b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Drag.cs.meta new file mode 100644 index 0000000..6535e03 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Drag.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 18a4e19a94101554eb76b88fbe6c4cc6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Play.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Play.cs index a4f4c3b..0aedd02 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Play.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Play.cs @@ -10,12 +10,11 @@ namespace TweenAnimation { public partial class TweenAnimationInspector : Editor { - float m_Time; + PlaybackTimer m_PlaybackTimer; // 编辑器下播放动画 void EditorPlay() { - m_Time = 0; EditorApplication.update -= Update; EditorApplication.update += Update; } @@ -25,10 +24,66 @@ namespace TweenAnimation EditorApplication.update -= Update; } + //https://answers.unity.com/questions/1187622/how-to-manually-refresh-inspector-from-editor-code.html + void RepaintInspector(System.Type t) + { + Editor[] ed = (Editor[])Resources.FindObjectsOfTypeAll(); + for (int i = 0; i < ed.Length; i++) + { + if (ed[i].GetType() == t) + { + ed[i].Repaint(); + return; + } + } + } + + //https://gamedev.stackexchange.com/questions/125698/how-to-edit-and-persist-serializable-assets-in-the-editor-window + //https://support.unity.com/hc/en-us/articles/115002294603-How-do-I-make-a-scene-dirty-when-modifying-a-property-via-script- + //https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html + //void MarkTargetsDirty() + //{ + // if (animation == null) + // return; + // for (int i = 0; i < animation.modules.Count; ++i) + // { + // var module = animation.modules[i]; + // if (module.target) + // EditorUtility.SetDirty(module.target); + // if (module.targets != null) + // { + // for (int j = 0; j < module.targets.Length; ++j) + // { + // EditorUtility.SetDirty(module.targets[j]); + // } + // } + // } + //} + + //https://forum.unity.com/threads/editor-script-mark-scene-dirty-changed.100340/ + void MarkSceneDirty() + { + if(animation) + EditorUtility.SetDirty(animation); + } + void Update() { - m_Time += Time.deltaTime; - animation.UpdateEditor(m_Time); + animation.UpdateEditor(m_PlaybackTimer.time); + + // repaint + MarkSceneDirty(); + RepaintInspector(typeof(TweenAnimationInspector)); + + //MarkSceneDirty(); + //MarkTargetsDirty(); + //UnityEngine.SceneManagement.Scene scene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene(); + //UnityEngine.SceneManagement.Scene scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene(); + //UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene); + //EditorWindow.GetWindow().Repaint(); + //SceneView.RepaintAll(); + //UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty(); + //SceneView.lastActiveSceneView.Repaint(); } } diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_RectSize.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_RectSize.cs index be6bf44..b963e85 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_RectSize.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_RectSize.cs @@ -11,8 +11,6 @@ namespace TweenAnimation { OnInspector_Base(module); - value = ui.GUIFloat("Float value", value, "f2"); - } } -- cgit v1.1-26-g67d0