feat: add OnComplete event for UIEffectTweener

close #289, close #188
This commit is contained in:
mob-sakai
2024-12-29 19:47:31 +09:00
parent 7fc8b1599a
commit aeb78ed577
2 changed files with 45 additions and 0 deletions
@@ -18,6 +18,7 @@ namespace Coffee.UIEffects.Editors
private SerializedProperty _playOnEnable; private SerializedProperty _playOnEnable;
private SerializedProperty _updateMode; private SerializedProperty _updateMode;
private SerializedProperty _wrapMode; private SerializedProperty _wrapMode;
private SerializedProperty _onComplete;
private bool _isPlaying = false; private bool _isPlaying = false;
private double _lastTime; private double _lastTime;
@@ -32,6 +33,7 @@ namespace Coffee.UIEffects.Editors
_interval = serializedObject.FindProperty("m_Interval"); _interval = serializedObject.FindProperty("m_Interval");
_wrapMode = serializedObject.FindProperty("m_WrapMode"); _wrapMode = serializedObject.FindProperty("m_WrapMode");
_updateMode = serializedObject.FindProperty("m_UpdateMode"); _updateMode = serializedObject.FindProperty("m_UpdateMode");
_onComplete = serializedObject.FindProperty("m_OnComplete");
EditorApplication.update += UpdateTweeners; EditorApplication.update += UpdateTweeners;
} }
@@ -54,6 +56,7 @@ namespace Coffee.UIEffects.Editors
EditorGUILayout.PropertyField(_playOnEnable); EditorGUILayout.PropertyField(_playOnEnable);
EditorGUILayout.PropertyField(_wrapMode); EditorGUILayout.PropertyField(_wrapMode);
EditorGUILayout.PropertyField(_updateMode); EditorGUILayout.PropertyField(_updateMode);
EditorGUILayout.PropertyField(_onComplete);
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
DrawPlayer(target as UIEffectTweener); DrawPlayer(target as UIEffectTweener);
Profiler.EndSample(); Profiler.EndSample();
+42
View File
@@ -1,5 +1,6 @@
using System; using System;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization; using UnityEngine.Serialization;
namespace Coffee.UIEffects namespace Coffee.UIEffects
@@ -94,6 +95,10 @@ namespace Coffee.UIEffects
[SerializeField] [SerializeField]
private UpdateMode m_UpdateMode = UpdateMode.Normal; 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 bool _isPaused;
private float _rate = -1; private float _rate = -1;
private float _time; private float _time;
@@ -255,6 +260,11 @@ namespace Coffee.UIEffects
set => m_Curve = value; set => m_Curve = value;
} }
/// <summary>
/// Event to invoke when the tween has completed.
/// </summary>
public UnityEvent onComplete => m_OnComplete;
/// <summary> /// <summary>
/// Is the tween playing? /// Is the tween playing?
/// </summary> /// </summary>
@@ -329,24 +339,44 @@ namespace Coffee.UIEffects
} }
_isPaused = false; _isPaused = false;
if (!isTweening)
{
m_OnComplete.Invoke();
}
} }
public void Play() public void Play()
{ {
ResetTime(); ResetTime();
_isPaused = false; _isPaused = false;
if (!isTweening)
{
m_OnComplete.Invoke();
}
} }
public void PlayForward() public void PlayForward()
{ {
direction = Direction.Forward; direction = Direction.Forward;
_isPaused = false; _isPaused = false;
if (!isTweening)
{
m_OnComplete.Invoke();
}
} }
public void PlayReverse() public void PlayReverse()
{ {
direction = Direction.Reverse; direction = Direction.Reverse;
_isPaused = false; _isPaused = false;
if (!isTweening)
{
m_OnComplete.Invoke();
}
} }
public void Stop() public void Stop()
@@ -380,6 +410,7 @@ namespace Coffee.UIEffects
public void UpdateTime(float deltaSec) public void UpdateTime(float deltaSec)
{ {
var prevTweening = isTweening;
var isLoop = wrapMode == WrapMode.Loop || wrapMode == WrapMode.PingPongLoop; var isLoop = wrapMode == WrapMode.Loop || wrapMode == WrapMode.PingPongLoop;
_time += deltaSec; _time += deltaSec;
if (isLoop) if (isLoop)
@@ -406,6 +437,12 @@ namespace Coffee.UIEffects
if (t <= 0 && 0 <= _time) if (t <= 0 && 0 <= _time)
{ {
rate = 0; rate = 0;
if (prevTweening && !isTweening)
{
m_OnComplete.Invoke();
}
return; return;
} }
@@ -434,6 +471,11 @@ namespace Coffee.UIEffects
} }
rate = Mathf.Clamp(t, 0, duration) / duration; rate = Mathf.Clamp(t, 0, duration) / duration;
if (prevTweening && !isTweening)
{
m_OnComplete.Invoke();
}
} }
} }
} }