Skip to content

Commit

Permalink
feat: add OnComplete event for UIEffectTweener
Browse files Browse the repository at this point in the history
close #289
  • Loading branch information
mob-sakai committed Dec 29, 2024
1 parent 74244b5 commit 5a3dce2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Packages/src/Editor/UIEffectTweenerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class UIEffectTweenerEditor : Editor
private SerializedProperty _playOnEnable;
private SerializedProperty _updateMode;
private SerializedProperty _wrapMode;
private SerializedProperty _onComplete;
private bool _isPlaying = false;
private double _lastTime;

Expand All @@ -32,6 +33,7 @@ private void OnEnable()
_interval = serializedObject.FindProperty("m_Interval");
_wrapMode = serializedObject.FindProperty("m_WrapMode");
_updateMode = serializedObject.FindProperty("m_UpdateMode");
_onComplete = serializedObject.FindProperty("m_OnComplete");

EditorApplication.update += UpdateTweeners;
}
Expand All @@ -54,6 +56,7 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_playOnEnable);
EditorGUILayout.PropertyField(_wrapMode);
EditorGUILayout.PropertyField(_updateMode);
EditorGUILayout.PropertyField(_onComplete);
serializedObject.ApplyModifiedProperties();
DrawPlayer(target as UIEffectTweener);
Profiler.EndSample();
Expand Down
42 changes: 42 additions & 0 deletions Packages/src/Runtime/UIEffectTweener.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;

namespace Coffee.UIEffects
Expand Down Expand Up @@ -94,6 +95,10 @@ public enum PlayOnEnable
[SerializeField]
private UpdateMode m_UpdateMode = UpdateMode.Normal;

[Tooltip("Event to invoke when the tween has completed.")]
[SerializeField]
private UnityEvent m_OnComplete = new UnityEvent();

private bool _isPaused;
private float _rate = -1;
private float _time;
Expand Down Expand Up @@ -255,6 +260,11 @@ public AnimationCurve curve
set => m_Curve = value;
}

/// <summary>
/// Event to invoke when the tween has completed.
/// </summary>
public UnityEvent onComplete => m_OnComplete;

/// <summary>
/// Is the tween playing?
/// </summary>
Expand Down Expand Up @@ -329,24 +339,44 @@ public void Play(bool resetTime)
}

_isPaused = false;

if (!isTweening)
{
m_OnComplete.Invoke();
}
}

public void Play()
{
ResetTime();
_isPaused = false;

if (!isTweening)
{
m_OnComplete.Invoke();
}
}

public void PlayForward()
{
direction = Direction.Forward;
_isPaused = false;

if (!isTweening)
{
m_OnComplete.Invoke();
}
}

public void PlayReverse()
{
direction = Direction.Reverse;
_isPaused = false;

if (!isTweening)
{
m_OnComplete.Invoke();
}
}

public void Stop()
Expand Down Expand Up @@ -380,6 +410,7 @@ public void SetTime(float sec)

public void UpdateTime(float deltaSec)
{
var prevTweening = isTweening;
var isLoop = wrapMode == WrapMode.Loop || wrapMode == WrapMode.PingPongLoop;
_time += deltaSec;
if (isLoop)
Expand All @@ -406,6 +437,12 @@ public void UpdateTime(float deltaSec)
if (t <= 0 && 0 <= _time)
{
rate = 0;

if (prevTweening && !isTweening)
{
m_OnComplete.Invoke();
}

return;
}

Expand Down Expand Up @@ -434,6 +471,11 @@ public void UpdateTime(float deltaSec)
}

rate = Mathf.Clamp(t, 0, duration) / duration;

if (prevTweening && !isTweening)
{
m_OnComplete.Invoke();
}
}
}
}

0 comments on commit 5a3dce2

Please sign in to comment.