From 5a3dce244f6efb3b376f7f8da8cdfafe8d750cca Mon Sep 17 00:00:00 2001
From: mob-sakai <12690315+mob-sakai@users.noreply.github.com>
Date: Sun, 29 Dec 2024 19:47:31 +0900
Subject: [PATCH] feat: add `OnComplete` event for UIEffectTweener
close #289
---
Packages/src/Editor/UIEffectTweenerEditor.cs | 3 ++
Packages/src/Runtime/UIEffectTweener.cs | 42 ++++++++++++++++++++
2 files changed, 45 insertions(+)
diff --git a/Packages/src/Editor/UIEffectTweenerEditor.cs b/Packages/src/Editor/UIEffectTweenerEditor.cs
index beac25be..1ec5c5e1 100644
--- a/Packages/src/Editor/UIEffectTweenerEditor.cs
+++ b/Packages/src/Editor/UIEffectTweenerEditor.cs
@@ -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;
@@ -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;
}
@@ -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();
diff --git a/Packages/src/Runtime/UIEffectTweener.cs b/Packages/src/Runtime/UIEffectTweener.cs
index 2ea457f5..eb8bce75 100644
--- a/Packages/src/Runtime/UIEffectTweener.cs
+++ b/Packages/src/Runtime/UIEffectTweener.cs
@@ -1,5 +1,6 @@
using System;
using UnityEngine;
+using UnityEngine.Events;
using UnityEngine.Serialization;
namespace Coffee.UIEffects
@@ -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;
@@ -255,6 +260,11 @@ public AnimationCurve curve
set => m_Curve = value;
}
+ ///
+ /// Event to invoke when the tween has completed.
+ ///
+ public UnityEvent onComplete => m_OnComplete;
+
///
/// Is the tween playing?
///
@@ -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()
@@ -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)
@@ -406,6 +437,12 @@ public void UpdateTime(float deltaSec)
if (t <= 0 && 0 <= _time)
{
rate = 0;
+
+ if (prevTweening && !isTweening)
+ {
+ m_OnComplete.Invoke();
+ }
+
return;
}
@@ -434,6 +471,11 @@ public void UpdateTime(float deltaSec)
}
rate = Mathf.Clamp(t, 0, duration) / duration;
+
+ if (prevTweening && !isTweening)
+ {
+ m_OnComplete.Invoke();
+ }
}
}
}