Skip to content

Commit aeb78ed

Browse files
committed
feat: add OnComplete event for UIEffectTweener
close #289, close #188
1 parent 7fc8b15 commit aeb78ed

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Packages/src/Editor/UIEffectTweenerEditor.cs

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal class UIEffectTweenerEditor : Editor
1818
private SerializedProperty _playOnEnable;
1919
private SerializedProperty _updateMode;
2020
private SerializedProperty _wrapMode;
21+
private SerializedProperty _onComplete;
2122
private bool _isPlaying = false;
2223
private double _lastTime;
2324

@@ -32,6 +33,7 @@ private void OnEnable()
3233
_interval = serializedObject.FindProperty("m_Interval");
3334
_wrapMode = serializedObject.FindProperty("m_WrapMode");
3435
_updateMode = serializedObject.FindProperty("m_UpdateMode");
36+
_onComplete = serializedObject.FindProperty("m_OnComplete");
3537

3638
EditorApplication.update += UpdateTweeners;
3739
}
@@ -54,6 +56,7 @@ public override void OnInspectorGUI()
5456
EditorGUILayout.PropertyField(_playOnEnable);
5557
EditorGUILayout.PropertyField(_wrapMode);
5658
EditorGUILayout.PropertyField(_updateMode);
59+
EditorGUILayout.PropertyField(_onComplete);
5760
serializedObject.ApplyModifiedProperties();
5861
DrawPlayer(target as UIEffectTweener);
5962
Profiler.EndSample();

Packages/src/Runtime/UIEffectTweener.cs

+42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using UnityEngine;
3+
using UnityEngine.Events;
34
using UnityEngine.Serialization;
45

56
namespace Coffee.UIEffects
@@ -94,6 +95,10 @@ public enum PlayOnEnable
9495
[SerializeField]
9596
private UpdateMode m_UpdateMode = UpdateMode.Normal;
9697

98+
[Tooltip("Event to invoke when the tween has completed.")]
99+
[SerializeField]
100+
private UnityEvent m_OnComplete = new UnityEvent();
101+
97102
private bool _isPaused;
98103
private float _rate = -1;
99104
private float _time;
@@ -255,6 +260,11 @@ public AnimationCurve curve
255260
set => m_Curve = value;
256261
}
257262

263+
/// <summary>
264+
/// Event to invoke when the tween has completed.
265+
/// </summary>
266+
public UnityEvent onComplete => m_OnComplete;
267+
258268
/// <summary>
259269
/// Is the tween playing?
260270
/// </summary>
@@ -329,24 +339,44 @@ public void Play(bool resetTime)
329339
}
330340

331341
_isPaused = false;
342+
343+
if (!isTweening)
344+
{
345+
m_OnComplete.Invoke();
346+
}
332347
}
333348

334349
public void Play()
335350
{
336351
ResetTime();
337352
_isPaused = false;
353+
354+
if (!isTweening)
355+
{
356+
m_OnComplete.Invoke();
357+
}
338358
}
339359

340360
public void PlayForward()
341361
{
342362
direction = Direction.Forward;
343363
_isPaused = false;
364+
365+
if (!isTweening)
366+
{
367+
m_OnComplete.Invoke();
368+
}
344369
}
345370

346371
public void PlayReverse()
347372
{
348373
direction = Direction.Reverse;
349374
_isPaused = false;
375+
376+
if (!isTweening)
377+
{
378+
m_OnComplete.Invoke();
379+
}
350380
}
351381

352382
public void Stop()
@@ -380,6 +410,7 @@ public void SetTime(float sec)
380410

381411
public void UpdateTime(float deltaSec)
382412
{
413+
var prevTweening = isTweening;
383414
var isLoop = wrapMode == WrapMode.Loop || wrapMode == WrapMode.PingPongLoop;
384415
_time += deltaSec;
385416
if (isLoop)
@@ -406,6 +437,12 @@ public void UpdateTime(float deltaSec)
406437
if (t <= 0 && 0 <= _time)
407438
{
408439
rate = 0;
440+
441+
if (prevTweening && !isTweening)
442+
{
443+
m_OnComplete.Invoke();
444+
}
445+
409446
return;
410447
}
411448

@@ -434,6 +471,11 @@ public void UpdateTime(float deltaSec)
434471
}
435472

436473
rate = Mathf.Clamp(t, 0, duration) / duration;
474+
475+
if (prevTweening && !isTweening)
476+
{
477+
m_OnComplete.Invoke();
478+
}
437479
}
438480
}
439481
}

0 commit comments

Comments
 (0)