From 9164e14bf5874837184fdfdba66a5b977036ca97 Mon Sep 17 00:00:00 2001 From: Victor Guilherme Date: Fri, 29 Nov 2024 09:16:45 -0300 Subject: [PATCH] feat: add option to not automatically plays tweener effect Added a new option on the tweener effect called "StartMode" to set it to not automatically play on start. eg. you have an image with a UIEffect and UIEffecTweener and do not want the effect to play instantly when the object starts but still want to use Normal/Unscaled update mode to not have to tween values manually in code. --- Packages/src/Editor/UIEffectTweenerEditor.cs | 3 ++ Packages/src/Runtime/UIEffectTweener.cs | 48 +++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/Packages/src/Editor/UIEffectTweenerEditor.cs b/Packages/src/Editor/UIEffectTweenerEditor.cs index a44ad969..5adf8d07 100644 --- a/Packages/src/Editor/UIEffectTweenerEditor.cs +++ b/Packages/src/Editor/UIEffectTweenerEditor.cs @@ -17,6 +17,7 @@ internal class UIMaterialPropertyTweenerEditor : Editor private SerializedProperty _restartOnEnable; private SerializedProperty _updateMode; private SerializedProperty _wrapMode; + private SerializedProperty _startMode; private void OnEnable() { @@ -29,6 +30,7 @@ private void OnEnable() _interval = serializedObject.FindProperty("m_Interval"); _wrapMode = serializedObject.FindProperty("m_WrapMode"); _updateMode = serializedObject.FindProperty("m_UpdateMode"); + _startMode = serializedObject.FindProperty("m_StartMode"); } public override void OnInspectorGUI() @@ -44,6 +46,7 @@ public override void OnInspectorGUI() EditorGUILayout.PropertyField(_restartOnEnable); EditorGUILayout.PropertyField(_wrapMode); EditorGUILayout.PropertyField(_updateMode); + EditorGUILayout.PropertyField(_startMode); serializedObject.ApplyModifiedProperties(); DrawPlayer(target as UIEffectTweener); Profiler.EndSample(); diff --git a/Packages/src/Runtime/UIEffectTweener.cs b/Packages/src/Runtime/UIEffectTweener.cs index da876688..e790dd1c 100644 --- a/Packages/src/Runtime/UIEffectTweener.cs +++ b/Packages/src/Runtime/UIEffectTweener.cs @@ -22,6 +22,12 @@ public enum UpdateMode Manual } + public enum StartMode + { + Automatic, + Manual + } + public enum WrapMode { Once, @@ -83,6 +89,13 @@ public enum Direction [SerializeField] private UpdateMode m_UpdateMode = UpdateMode.Normal; + [Tooltip("Specifies how the effect tweener will start.\n" + + " Automatic: Plays the tween automatically when it starts.\n" + + " Manual: Waits for the first `Play()` call to start.")] + [SerializeField] + private StartMode m_StartMode = StartMode.Automatic; + + public bool _isAwaitingStart; private float _rate; private float _time; private UIEffectBase _target; @@ -208,23 +221,32 @@ public UpdateMode updateMode set => m_UpdateMode = value; } + public StartMode startMode + { + get => m_StartMode; + set => m_StartMode = value; + } + public AnimationCurve curve { get => m_Curve; set => m_Curve = value; } + private void Awake() + { + _isAwaitingStart = m_StartMode == StartMode.Manual; + } + private void Update() { - switch (m_UpdateMode) + if (m_StartMode == StartMode.Manual && _isAwaitingStart) { - case UpdateMode.Normal: - UpdateTime(Time.deltaTime); - break; - case UpdateMode.Unscaled: - UpdateTime(Time.unscaledDeltaTime); - break; + return; } + + float deltaTime = m_UpdateMode == UpdateMode.Unscaled ? Time.unscaledDeltaTime : Time.deltaTime; + UpdateTime(deltaTime); } private void OnEnable() @@ -235,6 +257,18 @@ private void OnEnable() } } + public void Play() + { + _isAwaitingStart = false; + Restart(); + } + + public void Stop() + { + _isAwaitingStart = true; + Restart(); + } + public void Restart() { SetTime(0);