3
3
4
4
namespace Coffee . UIEffects
5
5
{
6
+ [ ExecuteAlways ]
6
7
[ RequireComponent ( typeof ( UIEffectBase ) ) ]
7
8
public class UIEffectTweener : MonoBehaviour
8
9
{
@@ -44,8 +45,7 @@ public enum Direction
44
45
45
46
[ Tooltip ( "The culling mask of the tween." ) ]
46
47
[ SerializeField ]
47
- private CullingMask m_CullingMask =
48
- CullingMask . Tone | CullingMask . Color | CullingMask . Sampling | CullingMask . Transition ;
48
+ private CullingMask m_CullingMask = ( CullingMask ) ( - 1 ) ;
49
49
50
50
[ Tooltip ( "The direction of the tween." ) ]
51
51
[ SerializeField ]
@@ -96,7 +96,8 @@ public enum Direction
96
96
private StartMode m_StartMode = StartMode . Automatic ;
97
97
98
98
public bool _isAwaitingStart ;
99
- private float _rate ;
99
+ private bool _isPaused ;
100
+ private float _rate = - 1 ;
100
101
private float _time ;
101
102
private UIEffectBase _target ;
102
103
@@ -171,19 +172,12 @@ public float time
171
172
{
172
173
get
173
174
{
174
- if ( _time < delay ) return _time ;
175
- var t = _time - delay ;
176
- switch ( wrapMode )
175
+ if ( wrapMode == WrapMode . Once || wrapMode == WrapMode . PingPongOnce )
177
176
{
178
- case WrapMode . Once :
179
- case WrapMode . PingPongOnce :
180
- return Mathf . Clamp ( t , 0 , totalTime - delay ) + delay ;
181
- case WrapMode . Loop :
182
- case WrapMode . PingPongLoop :
183
- return Mathf . Repeat ( t , totalTime - delay ) + delay ;
184
- default :
185
- throw new ArgumentOutOfRangeException ( ) ;
177
+ return Mathf . Clamp ( _time , 0 , totalTime ) ;
186
178
}
179
+
180
+ return Mathf . Repeat ( _time , totalTime ) ;
187
181
}
188
182
}
189
183
@@ -233,47 +227,101 @@ public AnimationCurve curve
233
227
set => m_Curve = value ;
234
228
}
235
229
236
- private void Awake ( )
230
+ public bool isTweening
237
231
{
238
- _isAwaitingStart = m_StartMode == StartMode . Manual ;
232
+ get
233
+ {
234
+ if ( _isPaused ) return false ;
235
+ if ( wrapMode == WrapMode . Loop || wrapMode == WrapMode . PingPongLoop ) return true ;
236
+
237
+ return direction == Direction . Forward
238
+ ? _time < totalTime
239
+ : 0 < _time ;
240
+ }
239
241
}
240
242
241
- private void Update ( )
243
+ public bool isPaused => _isPaused ;
244
+
245
+ public bool isDelaying => _time < delay ;
246
+
247
+ private void OnEnable ( )
242
248
{
243
- if ( m_StartMode == StartMode . Manual && _isAwaitingStart )
249
+ _isPaused = true ;
250
+ if ( playOnEnable )
244
251
{
245
- return ;
252
+ Play ( ) ;
246
253
}
254
+ }
247
255
248
- float deltaTime = m_UpdateMode == UpdateMode . Unscaled ? Time . unscaledDeltaTime : Time . deltaTime ;
249
- UpdateTime ( deltaTime ) ;
256
+ private void OnDisable ( )
257
+ {
258
+ _isPaused = true ;
250
259
}
251
260
252
- private void OnEnable ( )
261
+ private void Update ( )
253
262
{
254
- if ( m_RestartOnEnable )
263
+ #if UNITY_EDITOR
264
+ if ( ! Application . isPlaying ) return ;
265
+ #endif
266
+ if ( ! isTweening ) return ;
267
+
268
+ var deltaTime = m_UpdateMode == UpdateMode . Unscaled
269
+ ? Time . unscaledDeltaTime
270
+ : Time . deltaTime ;
271
+ UpdateTime ( direction == Direction . Forward ? deltaTime : - deltaTime ) ;
272
+ }
273
+
274
+ public void Play ( bool resetTime )
275
+ {
276
+ if ( resetTime )
255
277
{
256
- Restart ( ) ;
278
+ ResetTime ( ) ;
257
279
}
280
+
281
+ _isPaused = false ;
258
282
}
259
283
260
284
public void Play ( )
261
285
{
262
- _isAwaitingStart = false ;
263
- Restart ( ) ;
286
+ ResetTime ( ) ;
287
+ _isPaused = false ;
288
+ }
289
+
290
+ public void PlayForward ( )
291
+ {
292
+ direction = Direction . Forward ;
293
+ _isPaused = false ;
294
+ }
295
+
296
+ public void PlayReverse ( )
297
+ {
298
+ direction = Direction . Reverse ;
299
+ _isPaused = false ;
264
300
}
265
301
266
302
public void Stop ( )
267
303
{
268
- _isAwaitingStart = true ;
269
- Restart ( ) ;
304
+ _isPaused = true ;
305
+ ResetTime ( ) ;
270
306
}
271
307
272
- public void Restart ( )
308
+ public void SetPause ( bool pause )
309
+ {
310
+ _isPaused = pause ;
311
+ }
312
+
313
+ public void ResetTime ( )
273
314
{
274
315
SetTime ( 0 ) ;
275
316
}
276
317
318
+ [ Obsolete (
319
+ "UIEffectTweener.Restart has been deprecated. Use UIEffectTweener.ResetTime instead (UnityUpgradable) -> ResetTime" ) ]
320
+ public void Restart ( )
321
+ {
322
+ ResetTime ( ) ;
323
+ }
324
+
277
325
public void SetTime ( float sec )
278
326
{
279
327
_time = 0 ;
@@ -282,33 +330,60 @@ public void SetTime(float sec)
282
330
283
331
public void UpdateTime ( float deltaSec )
284
332
{
285
- rate = UpdateTime_Internal ( Mathf . Max ( 0 , deltaSec ) ) / duration ;
286
- }
287
-
288
- private float UpdateTime_Internal ( float delta )
289
- {
290
- _time += direction == Direction . Forward ? delta : - delta ;
291
- if ( _time < delay ) return 0 ;
333
+ var isLoop = wrapMode == WrapMode . Loop || wrapMode == WrapMode . PingPongLoop ;
334
+ _time += deltaSec ;
335
+ if ( isLoop )
336
+ {
337
+ if ( _time < 0 )
338
+ {
339
+ _time = Mathf . Repeat ( _time , totalTime ) ;
340
+ }
341
+ else if ( delay < _time )
342
+ {
343
+ _time = Mathf . Repeat ( _time - delay , totalTime - delay ) + delay ;
344
+ }
345
+ else if ( deltaSec < 0 && delay <= _time - deltaSec )
346
+ {
347
+ _time = Mathf . Repeat ( _time - delay , totalTime - delay ) + delay ;
348
+ }
349
+ }
350
+ else
351
+ {
352
+ _time = Mathf . Clamp ( _time , 0 , totalTime ) ;
353
+ }
292
354
293
355
var t = _time - delay ;
356
+ if ( t <= 0 && 0 <= _time )
357
+ {
358
+ rate = 0 ;
359
+ return ;
360
+ }
361
+
294
362
switch ( wrapMode )
295
363
{
364
+ case WrapMode . Once :
365
+ t = Mathf . Clamp ( t , 0 , duration ) ;
366
+ _time = t + delay ;
367
+ break ;
296
368
case WrapMode . Loop :
297
369
t = Mathf . Repeat ( t , duration + interval ) ;
370
+ _time = t + delay ;
298
371
break ;
299
372
case WrapMode . PingPongOnce :
300
373
t = Mathf . Clamp ( t , 0 , duration * 2 + interval ) ;
374
+ _time = t + delay ;
301
375
t = Mathf . PingPong ( t , duration + interval * 0.5f ) ;
302
376
break ;
303
377
case WrapMode . PingPongLoop :
304
378
t = Mathf . Repeat ( t , ( duration + interval ) * 2 ) ;
379
+ _time = t + delay ;
305
380
t = t < duration * 2 + interval
306
381
? Mathf . PingPong ( t , duration + interval * 0.5f )
307
382
: 0 ;
308
383
break ;
309
384
}
310
385
311
- return Mathf . Clamp ( t , 0 , duration ) ;
386
+ rate = Mathf . Clamp ( t , 0 , duration ) / duration ;
312
387
}
313
388
}
314
389
}
0 commit comments