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 _updateMode;
private SerializedProperty _wrapMode;
private SerializedProperty _onComplete;
private bool _isPlaying = false;
private double _lastTime;
@@ -32,6 +33,7 @@ namespace Coffee.UIEffects.Editors
_interval = serializedObject.FindProperty("m_Interval");
_wrapMode = serializedObject.FindProperty("m_WrapMode");
_updateMode = serializedObject.FindProperty("m_UpdateMode");
_onComplete = serializedObject.FindProperty("m_OnComplete");
EditorApplication.update += UpdateTweeners;
}
@@ -54,6 +56,7 @@ namespace Coffee.UIEffects.Editors
EditorGUILayout.PropertyField(_playOnEnable);
EditorGUILayout.PropertyField(_wrapMode);
EditorGUILayout.PropertyField(_updateMode);
EditorGUILayout.PropertyField(_onComplete);
serializedObject.ApplyModifiedProperties();
DrawPlayer(target as UIEffectTweener);
Profiler.EndSample();
+42
View File
@@ -1,5 +1,6 @@
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;
namespace Coffee.UIEffects
@@ -94,6 +95,10 @@ namespace Coffee.UIEffects
[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;
@@ -255,6 +260,11 @@ namespace Coffee.UIEffects
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>
@@ -329,24 +339,44 @@ namespace Coffee.UIEffects
}
_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()
@@ -380,6 +410,7 @@ namespace Coffee.UIEffects
public void UpdateTime(float deltaSec)
{
var prevTweening = isTweening;
var isLoop = wrapMode == WrapMode.Loop || wrapMode == WrapMode.PingPongLoop;
_time += deltaSec;
if (isLoop)
@@ -406,6 +437,12 @@ namespace Coffee.UIEffects
if (t <= 0 && 0 <= _time)
{
rate = 0;
if (prevTweening && !isTweening)
{
m_OnComplete.Invoke();
}
return;
}
@@ -434,6 +471,11 @@ namespace Coffee.UIEffects
}
rate = Mathf.Clamp(t, 0, duration) / duration;
if (prevTweening && !isTweening)
{
m_OnComplete.Invoke();
}
}
}
}