Skip to content

Commit

Permalink
feat: add option to not automatically plays tweener effect
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vgArchives authored and mob-sakai committed Nov 29, 2024
1 parent 5d0b03a commit 9164e14
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Packages/src/Editor/UIEffectTweenerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class UIMaterialPropertyTweenerEditor : Editor
private SerializedProperty _restartOnEnable;
private SerializedProperty _updateMode;
private SerializedProperty _wrapMode;
private SerializedProperty _startMode;

private void OnEnable()
{
Expand All @@ -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()
Expand All @@ -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();
Expand Down
48 changes: 41 additions & 7 deletions Packages/src/Runtime/UIEffectTweener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public enum UpdateMode
Manual
}

public enum StartMode
{
Automatic,
Manual
}

public enum WrapMode
{
Once,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -235,6 +257,18 @@ private void OnEnable()
}
}

public void Play()
{
_isAwaitingStart = false;
Restart();
}

public void Stop()
{
_isAwaitingStart = true;
Restart();
}

public void Restart()
{
SetTime(0);
Expand Down

0 comments on commit 9164e14

Please sign in to comment.