From 2683c04adcaff44f9504248d2c983cd86bd4b3ad Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 31 May 2021 09:43:09 +0800 Subject: *tween --- .../Scripts/Animation/Tween/Demo/TweenDemo.unity | 159 ++------------- .../Animation/Tween/Demo/TweenEventHandlerTest.cs | 18 ++ .../Tween/Demo/TweenEventHandlerTest.cs.meta | 11 + .../Animation/Tween/Editor/PlaybackTimer.cs | 17 +- .../Tween/Editor/TweenAnimationInspector.cs | 78 ++++++- .../Tween/Editor/TweenAnimationInspector_Play.cs | 3 +- .../Tween/Editor/TweenControllerInspector.cs | 123 +++++++++++ .../Tween/Editor/TweenControllerInspector.cs.meta | 11 + .../Scripts/Animation/Tween/Panel 5.prefab | 227 +++++++++++++++++---- .../Scripts/Animation/Tween/TweenAnimation.cs | 75 ++++++- .../Scripts/Animation/Tween/TweenController.cs | 51 +++-- 11 files changed, 558 insertions(+), 215 deletions(-) create mode 100644 Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenEventHandlerTest.cs create mode 100644 Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenEventHandlerTest.cs.meta create mode 100644 Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs create mode 100644 Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs.meta diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenDemo.unity b/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenDemo.unity index bf6c3a1..7d8478d 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenDemo.unity +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenDemo.unity @@ -4940,77 +4940,6 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!1 &419127640 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 419127641} - - component: {fileID: 419127643} - - component: {fileID: 419127642} - m_Layer: 5 - m_Name: Image - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!224 &419127641 -RectTransform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 419127640} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 224828143557382810} - m_RootOrder: 1 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0.5, y: 0.5} - m_AnchorMax: {x: 0.5, y: 0.5} - m_AnchoredPosition: {x: 7.4, y: 106.3} - m_SizeDelta: {x: 100, y: 100} - m_Pivot: {x: 0.5, y: 0.5} ---- !u!114 &419127642 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 419127640} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} - m_Name: - m_EditorClassIdentifier: - m_Material: {fileID: 0} - m_Color: {r: 1, g: 1, b: 1, a: 0} - m_RaycastTarget: 1 - m_OnCullStateChanged: - m_PersistentCalls: - m_Calls: [] - m_Sprite: {fileID: 0} - m_Type: 0 - m_PreserveAspect: 0 - m_FillCenter: 1 - m_FillMethod: 4 - m_FillAmount: 1 - m_FillClockwise: 1 - m_FillOrigin: 0 ---- !u!222 &419127643 -CanvasRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 419127640} - m_CullTransparentMesh: 0 --- !u!1 &638242413 GameObject: m_ObjectHideFlags: 0 @@ -5061,7 +4990,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Material: {fileID: 0} - m_Color: {r: 1, g: 1, b: 1, a: 0.9256807} + m_Color: {r: 1, g: 1, b: 1, a: 0.875} m_RaycastTarget: 1 m_OnCullStateChanged: m_PersistentCalls: @@ -5132,7 +5061,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Material: {fileID: 0} - m_Color: {r: 1, g: 1, b: 1, a: 0} + m_Color: {r: 1, g: 1, b: 1, a: 0.125} m_RaycastTarget: 1 m_OnCullStateChanged: m_PersistentCalls: @@ -5561,82 +5490,20 @@ PrefabInstance: propertyPath: m_Name value: Panel objectReference: {fileID: 0} - - target: {fileID: 2269033645537954357, guid: 1821abac54ca6014d8fff06cff6aff5d, + - target: {fileID: 2226187378230708801, guid: 1821abac54ca6014d8fff06cff6aff5d, type: 3} - propertyPath: animations.Array.size - value: 1 + propertyPath: triggerOnce + value: 0 objectReference: {fileID: 0} - - target: {fileID: 2269033645537954357, guid: 1821abac54ca6014d8fff06cff6aff5d, + - target: {fileID: 2226187378230708801, guid: 1821abac54ca6014d8fff06cff6aff5d, type: 3} - propertyPath: animations.Array.data[0].name - value: Normal - objectReference: {fileID: 0} - - target: {fileID: 2269033645537954357, guid: 1821abac54ca6014d8fff06cff6aff5d, + propertyPath: modules.Array.data[0].graphic + value: + objectReference: {fileID: 1853917340} + - target: {fileID: 2226187378230708801, guid: 1821abac54ca6014d8fff06cff6aff5d, type: 3} - propertyPath: animations.Array.data[0].animation + propertyPath: modules.Array.data[1].graphic value: - objectReference: {fileID: 2226187378230708801} - m_RemovedComponents: - - {fileID: 2226187378230708801, guid: 1821abac54ca6014d8fff06cff6aff5d, type: 3} + objectReference: {fileID: 638242415} + m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 1821abac54ca6014d8fff06cff6aff5d, type: 3} ---- !u!1 &2226187378230708800 stripped -GameObject: - m_CorrespondingSourceObject: {fileID: 2225253819299306041, guid: 1821abac54ca6014d8fff06cff6aff5d, - type: 3} - m_PrefabInstance: {fileID: 2226187378230708799} - m_PrefabAsset: {fileID: 0} ---- !u!114 &2226187378230708801 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2226187378230708800} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 6f0c0bc999ebb1a4ea14378d250ac5b5, type: 3} - m_Name: - m_EditorClassIdentifier: - description: No Description - modules: - - id: 0 - - id: 1 - duration: 2 - playbackStyle: 2 - eventTriggeredDirection: -1 - playbackLimit: 0 - triggerOnce: 0 - scriptHandler: 0 - eventList: - - name: New Event - time: 0 - eventHandler: - m_PersistentCalls: - m_Calls: [] - - name: New Event - time: 1 - eventHandler: - m_PersistentCalls: - m_Calls: [] - references: - version: 1 - 00000000: - type: {class: TweenAlpha, ns: TweenAnimation, asm: Assembly-CSharp} - data: - enabled: 1 - timeOffset: 0 - duration: 1 - description: - from: 1 - to: 0 - graphic: {fileID: 1853917340} - 00000001: - type: {class: TweenAlpha, ns: TweenAnimation, asm: Assembly-CSharp} - data: - enabled: 1 - timeOffset: 1.07 - duration: 0.69 - description: - from: 0 - to: 1 - graphic: {fileID: 638242415} diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenEventHandlerTest.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenEventHandlerTest.cs new file mode 100644 index 0000000..ff7874a --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenEventHandlerTest.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using TweenAnimation; + +public class TweenEventHandlerTest : MonoBehaviour , ITweenEventHandler +{ + public void OnTweenEvent(string eventName) + { + } + + [ExecuteInEditMode] + public void RecvEvent(string content) + { + Debug.Log(content); + } + +} diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenEventHandlerTest.cs.meta b/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenEventHandlerTest.cs.meta new file mode 100644 index 0000000..3ad86f5 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Demo/TweenEventHandlerTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0bc2088dbeaab534fa8605006045fe44 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs index 69b91f7..533fbc8 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs @@ -2,18 +2,24 @@ using System.Collections.Generic; using UnityEngine; using UnityEditor; + namespace TweenAnimation { public class PlaybackTimer { - // 考虑了Pause的playback时间,没考虑playback limit + // 动画 + private TweenAnimation m_Animation; + + // 考虑了Pause的playback逻辑时间,考虑了playbackLimit public float time { get { + float t = realTime; if (m_PauseRealTime != -1) - return m_PauseRealTime - m_StartRealTime; - return realTime; + t = m_PauseRealTime - m_StartRealTime; + t = m_Animation.ApplyLimit(t); + return t; } } @@ -30,8 +36,9 @@ namespace TweenAnimation private float m_PauseRealTime; - public PlaybackTimer() + public PlaybackTimer(TweenAnimation animation) { + m_Animation = animation; Reset(); } @@ -48,6 +55,8 @@ namespace TweenAnimation public void Pause() { m_PauseRealTime = (float)EditorApplication.timeSinceStartup; + float t = m_Animation.ApplyLimit(m_PauseRealTime - m_StartRealTime); + m_PauseRealTime = m_StartRealTime + t; } public void Stop() diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector.cs index 9780e8c..741458e 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector.cs @@ -48,12 +48,16 @@ namespace TweenAnimation HashSet m_EventTimeSet; float m_SelectedEventTime; + Dictionary m_CallbackUnfold; + public void OnEnable() { - if(s_TweenModuleGUI == null) + animation = target as TweenAnimation; + + if (s_TweenModuleGUI == null) s_TweenModuleGUI = new Dictionary(); - m_ShowAnimationTab = true; + m_ShowAnimationTab = false; m_ShowEvents = true; m_ShowModules = true; @@ -70,7 +74,9 @@ namespace TweenAnimation m_DragState = DragState.Release; if (m_PlaybackTimer == null) - m_PlaybackTimer = new PlaybackTimer(); + m_PlaybackTimer = new PlaybackTimer(animation); + + m_CallbackUnfold = new Dictionary(); } public void OnDisable() @@ -113,12 +119,12 @@ namespace TweenAnimation } GUI.Label(new Rect(rect.x + 10, rect.y + 3, 100, 20), "Tween Animation", styles.headerTitle); - Vector2 size = styles.text.CalcSize(new GUIContent(animation.description)); - GUI.Label(new Rect(rect.x + rect.width - size.x - 10, rect.y + 3, 100, 20), animation.description, styles.text); + Vector2 size = styles.text.CalcSize(new GUIContent(animation.name)); + GUI.Label(new Rect(rect.x + rect.width - size.x - 10, rect.y + 3, 100, 20), animation.name, styles.text); if (m_ShowAnimationTab) { - animation.description = ui.GUIText("Description", animation.description); + animation.name = ui.GUIText("Name", animation.name); // 播放风格 animation.playbackStyle = (TweenAnimation.PlaybackStyle) ui.GUIEnum("Playback Style", animation.playbackStyle); @@ -136,7 +142,10 @@ namespace TweenAnimation animation.duration = Mathf.Clamp(duration, 0, float.MaxValue); // 事件触发方向 - animation.eventTriggeredDirection = (TweenAnimation.EventTriggeredDirection)ui.GUIEnumMask("Event Direction", animation.eventTriggeredDirection); + if(animation.playbackStyle == TweenAnimation.PlaybackStyle.PingPong) + { + animation.eventTriggeredDirection = (TweenAnimation.EventTriggeredDirection)ui.GUIEnumMask("Event Direction", animation.eventTriggeredDirection); + } // 是否触发事件 animation.triggerEvents = ui.GUIToggle("Trigger Events", animation.triggerEvents); @@ -151,7 +160,11 @@ namespace TweenAnimation // 时间轴 DrawRuler(1f, true); - + + // default event callbacks + DefaultEventCallbacks(); + + // event list EventList(); } @@ -554,9 +567,11 @@ namespace TweenAnimation m_Pause = !m_Pause; if (m_Pause) { - EditorPlay(); if(!m_Play) + { + EditorPlay(); m_PlaybackTimer.Resume(); + } m_PlaybackTimer.Pause(); m_Play = m_Stop = false; } @@ -588,6 +603,49 @@ namespace TweenAnimation GUI.EndGroup(); } + void DefaultEventCallbacks() + { + Rect evetRect = ui.GetControlRect(15); + + Rect labelRect = evetRect; + labelRect.x += 2; + GUI.Label(labelRect, "Callbacks", styles.textBold); + + EventCallback("onStart"); + EventCallback("onEnd"); + EventCallback("onTurnStart"); + EventCallback("onTurnEnd"); + } + + void EventCallback(string callbackName) + { + Rect rect = ui.GetControlRect(15); + + Rect labelRect = rect; + labelRect.x += 20; + GUI.Label(labelRect, callbackName, styles.text); + + Rect unfoldRect = rect; + unfoldRect.x = rect.x + rect.width - 19; + unfoldRect.width = 13; + unfoldRect.height = 13; + unfoldRect.y += 3; + bool unfold; + if(!m_CallbackUnfold.TryGetValue(callbackName, out unfold)) + { + m_CallbackUnfold.Add(callbackName, false); + } + m_CallbackUnfold[callbackName]= GUI.Button(unfoldRect, (m_CallbackUnfold[callbackName] ? "▲" : "▼"), styles.textSmall) ? !m_CallbackUnfold[callbackName] : m_CallbackUnfold[callbackName]; + if(m_CallbackUnfold[callbackName]) + { + var prop = serializedObject.FindProperty(callbackName); + if(prop != null) + { + EditorGUILayout.PropertyField(prop); + } + } + } + void EventList() { Rect evetRect = ui.GetControlRect(15); @@ -629,7 +687,7 @@ namespace TweenAnimation // event time Rect timeLabelRect = rect; - timeLabelRect.x += 25; + timeLabelRect.x += 20; timeLabelRect.width = 30; timeLabelRect.height = 13; GUI.Label(timeLabelRect, "time:", e.time == m_SelectedEventTime ? styles.textBold : styles.text); 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 647c828..e74aa74 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Play.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenAnimationInspector_Play.cs @@ -17,14 +17,13 @@ namespace TweenAnimation get { return m_PlaybackTimer.time; - float t = animation.ApplyLimit(m_PlaybackTimer.time); - return t; } } // 编辑器下播放动画 void EditorPlay() { + animation.BeforePlay(); EditorApplication.update -= Update; EditorApplication.update += Update; } diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs new file mode 100644 index 0000000..e83e87e --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs @@ -0,0 +1,123 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; + +namespace TweenAnimation +{ + [CustomEditor(typeof(TweenController), false)] + public class TweenControllerInspector : Editor + { + private TweenController controller; + + public override void OnInspectorGUI() + { + controller = target as TweenController; + if (controller == null) + return; + + GUI.changed = false; + + // automatically play on enable + Rect autoRect = GUILayoutUtility.GetRect(0f, 20f); + autoRect.width = 150; + GUI.Label(autoRect, "Auto Play When Enabled"); + autoRect.x = 190; + controller.autoPlayOnEnable = GUI.Toggle(autoRect, controller.autoPlayOnEnable, ""); + + // default animation + if(controller.autoPlayOnEnable) + { + Rect defaultRect = GUILayoutUtility.GetRect(0f, 20f); + float width = defaultRect.width; + defaultRect.width = 150; + GUI.Label(defaultRect, "Default Animation"); + defaultRect.x = 190; + defaultRect.width = width - 172; + int index = -1; + int count = 0; + for(int i = 0; i < controller.animations.Count;i++) + { + if (controller.animations[i] != null) + ++count; + } + string[] displayName = new string[count]; + for(int i = 0, j = 0; i < controller.animations.Count; ++i) + { + if (controller.animations[i] == null) + continue; + if (controller.animations[i].name == controller.defaultAnimation) + index = j; + displayName[j++] = controller.animations[i].name; + } + index = EditorGUI.Popup(defaultRect, index, displayName); + if(index >= 0 && index < displayName.Length) + controller.defaultAnimation = displayName[index]; + } + + Rect hintRect = GUILayoutUtility.GetRect(0f, 20f); + GUI.Label(hintRect, "Animations"); + + // animation list + if(controller.animations.Count > 0) + { + List deleted = new List(); + for (int i = 0; i < controller.animations.Count; ++i) + { + var animation = controller.animations[i]; + Rect rect = GUILayoutUtility.GetRect(0f, 20f); + Rect buttonRect = rect; + buttonRect.width = 20; + if (GUI.Button(buttonRect, "-")) + { + deleted.Add(i); + break; + } + Rect nameRect = rect; + nameRect.x = 60; + nameRect.width = 100; + if (animation) + GUI.Label(nameRect, animation.name); + else + GUI.Label(nameRect, ""); + Rect animationRect = rect; + animationRect.width = rect.width - 178; + animationRect.x = 190; + var newAnimation = EditorGUI.ObjectField(animationRect, animation, typeof(TweenAnimation), true) as TweenAnimation; + if (newAnimation != animation && controller.animations.Contains(newAnimation)) + { + Debug.LogError("Tween Animation'" + newAnimation + "' already added."); + } + else + controller.animations[i] = newAnimation; + } + + if (deleted.Count > 0) + { + List temp = controller.animations; + controller.animations = new List(); + for (int i = 0; i < temp.Count; ++i) + { + if (!deleted.Contains(i)) + controller.animations.Add(temp[i]); + } + } + + GUILayout.Space(3); + } + // add new + Rect addRect = GUILayoutUtility.GetRect(0f, 20f); + if (GUI.Button(addRect, "+New Animation")) + { + controller.animations.Add(null); + } + + if(GUI.changed) + { + EditorUtility.SetDirty(controller); + } + + } + + } +} diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs.meta b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs.meta new file mode 100644 index 0000000..cbcef84 --- /dev/null +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7d62716461ba0fc4c9295d14af916587 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/Panel 5.prefab b/Assets/UI_Extension/Scripts/Animation/Tween/Panel 5.prefab index 4543479..7402974 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/Panel 5.prefab +++ b/Assets/UI_Extension/Scripts/Animation/Tween/Panel 5.prefab @@ -1,5 +1,76 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1 &419127640 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 419127641} + - component: {fileID: 419127643} + - component: {fileID: 419127642} + m_Layer: 5 + m_Name: Image + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &419127641 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 419127640} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 2160533344743970981} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 7.4, y: 106.3} + m_SizeDelta: {x: 100, y: 100} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!222 &419127643 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 419127640} + m_CullTransparentMesh: 0 +--- !u!114 &419127642 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 419127640} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 0} + m_RaycastTarget: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 0} + m_Type: 0 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 --- !u!1 &2225253819299306041 GameObject: m_ObjectHideFlags: 0 @@ -11,6 +82,8 @@ GameObject: - component: {fileID: 2160533344743970981} - component: {fileID: 2269033645537954357} - component: {fileID: 2226187378230708801} + - component: {fileID: 6121367869316875577} + - component: {fileID: 2226187378230708802} m_Layer: 5 m_Name: Panel 5 m_TagString: Untagged @@ -30,6 +103,7 @@ RectTransform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 2160518537753456285} + - {fileID: 419127641} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -50,9 +124,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 1a60e4bfc89118145bda9abd42b07e9e, type: 3} m_Name: m_EditorClassIdentifier: + autoPlayOnEnable: 1 + defaultAnimation: Normal animations: - - name: Normal - animation: {fileID: 0} + - {fileID: 2226187378230708801} + - {fileID: 6121367869316875577} --- !u!114 &2226187378230708801 MonoBehaviour: m_ObjectHideFlags: 0 @@ -65,19 +141,62 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 6f0c0bc999ebb1a4ea14378d250ac5b5, type: 3} m_Name: m_EditorClassIdentifier: - description: No Description + name: Normal modules: - id: 0 - id: 1 - - id: 2 - - id: 3 duration: 1 - playbackStyle: 1 - eventTriggeredDirection: 0 + playbackStyle: 2 + eventTriggeredDirection: -1 playbackLimit: 0 - triggerOnce: 0 - scriptHandler: 0 - eventList: [] + triggerEvents: 1 + triggerOnce: 1 + scriptHandler: 1 + eventList: + - name: First + time: 0.1 + eventHandler: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 2226187378230708802} + m_MethodName: RecvEvent + m_Mode: 5 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: Recv First + m_BoolArgument: 0 + m_CallState: 1 + - name: Second + time: 0.5 + eventHandler: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 2226187378230708802} + m_MethodName: RecvEvent + m_Mode: 5 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: Recv Second + m_BoolArgument: 0 + m_CallState: 1 + onStart: + m_PersistentCalls: + m_Calls: [] + onTurnStart: + m_PersistentCalls: + m_Calls: [] + onTurnEnd: + m_PersistentCalls: + m_Calls: [] + onEnd: + m_PersistentCalls: + m_Calls: [] references: version: 1 00000000: @@ -85,31 +204,69 @@ MonoBehaviour: data: enabled: 1 timeOffset: 0 - duration: 0 + duration: 1 description: - alpha: 0 + from: 1 + to: 0 + graphic: {fileID: 0} 00000001: - type: {class: TweenRectSize, ns: TweenAnimation, asm: Assembly-CSharp} - data: - enabled: 1 - timeOffset: 0 - duration: 0 - description: - 00000002: - type: {class: TweenColor, ns: TweenAnimation, asm: Assembly-CSharp} - data: - enabled: 1 - timeOffset: 0 - duration: 0 - description: - color: {r: 0, g: 0, b: 0, a: 0} - 00000003: - type: {class: TweenRectSize, ns: TweenAnimation, asm: Assembly-CSharp} + type: {class: TweenAlpha, ns: TweenAnimation, asm: Assembly-CSharp} data: enabled: 1 timeOffset: 0 - duration: 0 + duration: 1 description: + from: 0 + to: 1 + graphic: {fileID: 0} +--- !u!114 &6121367869316875577 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2225253819299306041} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6f0c0bc999ebb1a4ea14378d250ac5b5, type: 3} + m_Name: + m_EditorClassIdentifier: + name: Pressed + modules: [] + duration: 1 + playbackStyle: 1 + eventTriggeredDirection: 0 + playbackLimit: 0 + triggerEvents: 0 + triggerOnce: 0 + scriptHandler: 0 + eventList: [] + onStart: + m_PersistentCalls: + m_Calls: [] + onTurnStart: + m_PersistentCalls: + m_Calls: [] + onTurnEnd: + m_PersistentCalls: + m_Calls: [] + onEnd: + m_PersistentCalls: + m_Calls: [] + references: + version: 1 +--- !u!114 &2226187378230708802 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2225253819299306041} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0bc2088dbeaab534fa8605006045fe44, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &2225254824961927991 GameObject: m_ObjectHideFlags: 0 @@ -175,8 +332,8 @@ MonoBehaviour: m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] - m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0} - m_Type: 1 + m_Sprite: {fileID: 0} + m_Type: 0 m_PreserveAspect: 0 m_FillCenter: 1 m_FillMethod: 4 @@ -281,22 +438,22 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Material: {fileID: 0} - m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1} + m_Color: {r: 1, g: 1, b: 1, a: 1} m_RaycastTarget: 1 m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] m_FontData: - m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_Font: {fileID: 0} m_FontSize: 14 m_FontStyle: 0 m_BestFit: 0 m_MinSize: 10 m_MaxSize: 40 - m_Alignment: 4 + m_Alignment: 0 m_AlignByGeometry: 0 m_RichText: 1 m_HorizontalOverflow: 0 m_VerticalOverflow: 0 m_LineSpacing: 1 - m_Text: Button + m_Text: diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs b/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs index 6441552..79f31c6 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs @@ -18,6 +18,8 @@ namespace TweenAnimation [NonSerialized] public bool unfold = false; #endif + [NonSerialized] + public bool trigged = false; public TweenEvent() { name = "New Event"; @@ -42,7 +44,7 @@ namespace TweenAnimation PingPong, } - public string description = "No Description"; + public new string name = "New Animation"; [SerializeReference] public List modules; @@ -67,6 +69,15 @@ namespace TweenAnimation public List eventList; + // 默认的事件回调函数 + public UnityEvent onStart; + public UnityEvent onTurnStart; + public UnityEvent onTurnEnd; + public UnityEvent onEnd; + + // 上次更新时的时间(一个turn内的) + private float m_LastPlaybackTurnTime; + public TweenAnimation() { this.playbackStyle = PlaybackStyle.Loop; @@ -74,6 +85,7 @@ namespace TweenAnimation this.duration = 1; this.modules = new List(); this.triggerOnce = false; + m_LastPlaybackTurnTime = -0.001f; } public void AddModule(TweenModule module) @@ -105,8 +117,25 @@ namespace TweenAnimation eventList.Remove(e); } + public void BeforePlay() + { + m_LastPlaybackTurnTime = -0.001f; + ResetEventTriggerFlag(); + } + + private void ResetEventTriggerFlag() + { + for(int i = 0; i < eventList.Count; ++i) + { + var e = eventList[i]; + if (e == null) + continue; + e.trigged = false; + } + } + /// - /// 根据时间执行tween + /// 根据时间执行tween, time是从播放开始的真实时间 /// /// real time public void Process(float time) @@ -114,9 +143,44 @@ namespace TweenAnimation if (modules == null) return; - time = ApplyLimit(time); - float logicTime = HandleTime(time); + float limitTime = ApplyLimit(time); // 转换为playback limit后的逻辑时间 + float logicTime = HandleTime(limitTime); // 转换为1个turn内的逻辑时间 + // 处理事件 + if(triggerEvents) + { + if (limitTime >= 0 && m_LastPlaybackTurnTime < 0) + onStart.Invoke(); + bool isPingpong = playbackStyle == PlaybackStyle.PingPong; + for (int i = 0; i < eventList.Count; ++i) + { + var e = eventList[i]; + if (e == null) + continue; + if (triggerOnce && e.trigged) + continue; + float t = e.time; + bool bTrigger = false; + bool isOdd = (((int)(limitTime / duration)) & 1) == 1; + if (isPingpong && isOdd && (eventTriggeredDirection & EventTriggeredDirection.Backward) == 0) + continue; + if (isPingpong && !isOdd && (eventTriggeredDirection & EventTriggeredDirection.Forward) == 0) + continue; + if (isPingpong && isOdd) + bTrigger = logicTime <= t && m_LastPlaybackTurnTime > t; + else + bTrigger = logicTime >= t && m_LastPlaybackTurnTime < t; + if(bTrigger && e.eventHandler != null) + { + e.eventHandler.Invoke(); + if (triggerOnce) + e.trigged = true; + } + } + m_LastPlaybackTurnTime = logicTime; + } + + // 更新动画 for (int i = 0; i < modules.Count; i ++) { TweenModule module = modules[i]; @@ -166,12 +230,13 @@ namespace TweenAnimation return logicTime; } + // 返回真实时间对应的逻辑时间 public float ApplyLimit(float time) { if (playbackLimit <= 0) return time; if (playbackStyle == PlaybackStyle.Loop) - time = Mathf.Clamp(time, 0, playbackLimit * duration); + time = Mathf.Clamp(time, 0, playbackLimit * duration - 0.001f); // 0.001是为了防止HandleTime的mod等于0 else if(playbackStyle == PlaybackStyle.PingPong) time = Mathf.Clamp(time, 0, playbackLimit * duration); return time; diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/TweenController.cs b/Assets/UI_Extension/Scripts/Animation/Tween/TweenController.cs index 561eff6..e2c4495 100644 --- a/Assets/UI_Extension/Scripts/Animation/Tween/TweenController.cs +++ b/Assets/UI_Extension/Scripts/Animation/Tween/TweenController.cs @@ -7,29 +7,54 @@ using UnityEngine.Events; namespace TweenAnimation { - [Serializable] - public class TweenState - { - public string name; - public TweenAnimation animation; - } - public class TweenController : MonoBehaviour { - public List animations; + public bool autoPlayOnEnable; + public string defaultAnimation; + public List animations; - void Start () { - - } + private string m_Current; + private TweenAnimation m_CurrentAnimation; - public void PlayAnimation(string name) + private float m_StartTime; + + public void OnEnable() { + if(autoPlayOnEnable) + { + PlayAnimation(defaultAnimation); + } + } + public void PlayAnimation(string name) + { + m_Current = name; + m_CurrentAnimation = null; + for (int i = 0; i < animations.Count; ++i) + { + if(animations[i].name == name) + { + m_CurrentAnimation = animations[i]; + break; + } + } + if(m_CurrentAnimation == null) + { + Debug.LogError("No such animation'" + name + "'"); + } + else + { + m_CurrentAnimation.BeforePlay(); + } + m_StartTime = Time.time; } private void Update() { - + if (m_CurrentAnimation == null) + return; + float t = Time.time; + m_CurrentAnimation.Process(t - m_StartTime); } } -- cgit v1.1-26-g67d0