Skip to content

Commit 54825ee

Browse files
committed
fix: fix reverse direction mode for UIEffectTweener
close #281, close #282, close #283
1 parent bdd8e08 commit 54825ee

File tree

1 file changed

+112
-37
lines changed

1 file changed

+112
-37
lines changed

Packages/src/Runtime/UIEffectTweener.cs

+112-37
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Coffee.UIEffects
55
{
6+
[ExecuteAlways]
67
[RequireComponent(typeof(UIEffectBase))]
78
public class UIEffectTweener : MonoBehaviour
89
{
@@ -44,8 +45,7 @@ public enum Direction
4445

4546
[Tooltip("The culling mask of the tween.")]
4647
[SerializeField]
47-
private CullingMask m_CullingMask =
48-
CullingMask.Tone | CullingMask.Color | CullingMask.Sampling | CullingMask.Transition;
48+
private CullingMask m_CullingMask = (CullingMask)(-1);
4949

5050
[Tooltip("The direction of the tween.")]
5151
[SerializeField]
@@ -96,7 +96,8 @@ public enum Direction
9696
private StartMode m_StartMode = StartMode.Automatic;
9797

9898
public bool _isAwaitingStart;
99-
private float _rate;
99+
private bool _isPaused;
100+
private float _rate = -1;
100101
private float _time;
101102
private UIEffectBase _target;
102103

@@ -171,19 +172,12 @@ public float time
171172
{
172173
get
173174
{
174-
if (_time < delay) return _time;
175-
var t = _time - delay;
176-
switch (wrapMode)
175+
if (wrapMode == WrapMode.Once || wrapMode == WrapMode.PingPongOnce)
177176
{
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);
186178
}
179+
180+
return Mathf.Repeat(_time, totalTime);
187181
}
188182
}
189183

@@ -233,47 +227,101 @@ public AnimationCurve curve
233227
set => m_Curve = value;
234228
}
235229

236-
private void Awake()
230+
public bool isTweening
237231
{
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+
}
239241
}
240242

241-
private void Update()
243+
public bool isPaused => _isPaused;
244+
245+
public bool isDelaying => _time < delay;
246+
247+
private void OnEnable()
242248
{
243-
if (m_StartMode == StartMode.Manual && _isAwaitingStart)
249+
_isPaused = true;
250+
if (playOnEnable)
244251
{
245-
return;
252+
Play();
246253
}
254+
}
247255

248-
float deltaTime = m_UpdateMode == UpdateMode.Unscaled ? Time.unscaledDeltaTime : Time.deltaTime;
249-
UpdateTime(deltaTime);
256+
private void OnDisable()
257+
{
258+
_isPaused = true;
250259
}
251260

252-
private void OnEnable()
261+
private void Update()
253262
{
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)
255277
{
256-
Restart();
278+
ResetTime();
257279
}
280+
281+
_isPaused = false;
258282
}
259283

260284
public void Play()
261285
{
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;
264300
}
265301

266302
public void Stop()
267303
{
268-
_isAwaitingStart = true;
269-
Restart();
304+
_isPaused = true;
305+
ResetTime();
270306
}
271307

272-
public void Restart()
308+
public void SetPause(bool pause)
309+
{
310+
_isPaused = pause;
311+
}
312+
313+
public void ResetTime()
273314
{
274315
SetTime(0);
275316
}
276317

318+
[Obsolete(
319+
"UIEffectTweener.Restart has been deprecated. Use UIEffectTweener.ResetTime instead (UnityUpgradable) -> ResetTime")]
320+
public void Restart()
321+
{
322+
ResetTime();
323+
}
324+
277325
public void SetTime(float sec)
278326
{
279327
_time = 0;
@@ -282,33 +330,60 @@ public void SetTime(float sec)
282330

283331
public void UpdateTime(float deltaSec)
284332
{
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+
}
292354

293355
var t = _time - delay;
356+
if (t <= 0 && 0 <= _time)
357+
{
358+
rate = 0;
359+
return;
360+
}
361+
294362
switch (wrapMode)
295363
{
364+
case WrapMode.Once:
365+
t = Mathf.Clamp(t, 0, duration);
366+
_time = t + delay;
367+
break;
296368
case WrapMode.Loop:
297369
t = Mathf.Repeat(t, duration + interval);
370+
_time = t + delay;
298371
break;
299372
case WrapMode.PingPongOnce:
300373
t = Mathf.Clamp(t, 0, duration * 2 + interval);
374+
_time = t + delay;
301375
t = Mathf.PingPong(t, duration + interval * 0.5f);
302376
break;
303377
case WrapMode.PingPongLoop:
304378
t = Mathf.Repeat(t, (duration + interval) * 2);
379+
_time = t + delay;
305380
t = t < duration * 2 + interval
306381
? Mathf.PingPong(t, duration + interval * 0.5f)
307382
: 0;
308383
break;
309384
}
310385

311-
return Mathf.Clamp(t, 0, duration);
386+
rate = Mathf.Clamp(t, 0, duration) / duration;
312387
}
313388
}
314389
}

0 commit comments

Comments
 (0)