1
1
using System ;
2
2
using UnityEngine ;
3
+ using UnityEngine . Events ;
3
4
using UnityEngine . Serialization ;
4
5
5
6
namespace Coffee . UIEffects
@@ -94,6 +95,10 @@ public enum PlayOnEnable
94
95
[ SerializeField ]
95
96
private UpdateMode m_UpdateMode = UpdateMode . Normal ;
96
97
98
+ [ Tooltip ( "Event to invoke when the tween has completed." ) ]
99
+ [ SerializeField ]
100
+ private UnityEvent m_OnComplete = new UnityEvent ( ) ;
101
+
97
102
private bool _isPaused ;
98
103
private float _rate = - 1 ;
99
104
private float _time ;
@@ -255,6 +260,11 @@ public AnimationCurve curve
255
260
set => m_Curve = value ;
256
261
}
257
262
263
+ /// <summary>
264
+ /// Event to invoke when the tween has completed.
265
+ /// </summary>
266
+ public UnityEvent onComplete => m_OnComplete ;
267
+
258
268
/// <summary>
259
269
/// Is the tween playing?
260
270
/// </summary>
@@ -329,24 +339,44 @@ public void Play(bool resetTime)
329
339
}
330
340
331
341
_isPaused = false ;
342
+
343
+ if ( ! isTweening )
344
+ {
345
+ m_OnComplete . Invoke ( ) ;
346
+ }
332
347
}
333
348
334
349
public void Play ( )
335
350
{
336
351
ResetTime ( ) ;
337
352
_isPaused = false ;
353
+
354
+ if ( ! isTweening )
355
+ {
356
+ m_OnComplete . Invoke ( ) ;
357
+ }
338
358
}
339
359
340
360
public void PlayForward ( )
341
361
{
342
362
direction = Direction . Forward ;
343
363
_isPaused = false ;
364
+
365
+ if ( ! isTweening )
366
+ {
367
+ m_OnComplete . Invoke ( ) ;
368
+ }
344
369
}
345
370
346
371
public void PlayReverse ( )
347
372
{
348
373
direction = Direction . Reverse ;
349
374
_isPaused = false ;
375
+
376
+ if ( ! isTweening )
377
+ {
378
+ m_OnComplete . Invoke ( ) ;
379
+ }
350
380
}
351
381
352
382
public void Stop ( )
@@ -380,6 +410,7 @@ public void SetTime(float sec)
380
410
381
411
public void UpdateTime ( float deltaSec )
382
412
{
413
+ var prevTweening = isTweening ;
383
414
var isLoop = wrapMode == WrapMode . Loop || wrapMode == WrapMode . PingPongLoop ;
384
415
_time += deltaSec ;
385
416
if ( isLoop )
@@ -406,6 +437,12 @@ public void UpdateTime(float deltaSec)
406
437
if ( t <= 0 && 0 <= _time )
407
438
{
408
439
rate = 0 ;
440
+
441
+ if ( prevTweening && ! isTweening )
442
+ {
443
+ m_OnComplete . Invoke ( ) ;
444
+ }
445
+
409
446
return ;
410
447
}
411
448
@@ -434,6 +471,11 @@ public void UpdateTime(float deltaSec)
434
471
}
435
472
436
473
rate = Mathf . Clamp ( t , 0 , duration ) / duration ;
474
+
475
+ if ( prevTweening && ! isTweening )
476
+ {
477
+ m_OnComplete . Invoke ( ) ;
478
+ }
437
479
}
438
480
}
439
481
}
0 commit comments