Skip to content

Commit e82ca49

Browse files
committed
Fix sequences completing too soon and not delaying
1 parent 3b7efa5 commit e82ca49

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

Runtime/Sequence.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,23 @@ private void Next()
8181
{
8282
this.currentIndex++;
8383

84-
if (this.currentIndex >= this.tweens.Count) {
84+
if (CanComplete()) {
8585
Complete();
8686
} else {
8787
this.tweens[this.currentIndex].Play();
8888
}
8989
}
9090

91+
protected override void Animate()
92+
{
93+
// Do nothing. The individual tweens are animated on their own.
94+
}
95+
96+
protected override bool CanComplete()
97+
{
98+
return this.currentIndex >= this.tweens.Count;
99+
}
100+
91101
protected override void OnStart()
92102
{
93103
this.currentIndex = 0;
@@ -146,11 +156,6 @@ protected override void OnReset()
146156
this.currentIndex = -1;
147157
}
148158

149-
protected override void Animate()
150-
{
151-
// Do nothing. The individual tweens are animated on their own.
152-
}
153-
154159
}
155160

156161
}

Runtime/Tween.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,7 @@ internal void Update(float deltaTime)
163163
{
164164
if (this.IsPlaying)
165165
{
166-
if (this.elapsed >= this.duration)
167-
{
168-
Complete();
169-
}
170-
else if (!this.IsDelayed)
166+
if (!this.IsDelayed)
171167
{
172168
this.elapsed += deltaTime;
173169

@@ -177,10 +173,21 @@ internal void Update(float deltaTime)
177173
if (this.onUpdate != null) {
178174
this.onUpdate.Invoke();
179175
}
176+
177+
if (CanComplete()) {
178+
Complete();
179+
}
180180
}
181181
else
182182
{
183183
this.delayElapsed += deltaTime;
184+
185+
// Start the tween once the delay is complete and only if
186+
// the elapsed time is zero which indicates it has never
187+
// been updated yet
188+
if (this.elapsed == 0 && this.delayElapsed >= this.delay) {
189+
Start();
190+
}
184191
}
185192
}
186193
else if (this.state == TweenState.Ready && this.autoStart)
@@ -213,15 +220,26 @@ public void Play()
213220
this.elapsed = 0.0f;
214221
this.delayElapsed = 0.0f;
215222

216-
OnStart();
217-
Animate();
218-
219-
if (this.onStart != null) {
220-
this.onStart.Invoke();
223+
// Start right away if there is no delay
224+
if (!this.IsDelayed) {
225+
Start();
221226
}
222227
}
223228
}
224229

230+
/// <summary>
231+
/// Starts the tween for the first time.
232+
/// </summary>
233+
private void Start()
234+
{
235+
OnStart();
236+
Animate();
237+
238+
if (this.onStart != null) {
239+
this.onStart.Invoke();
240+
}
241+
}
242+
225243
/// <summary>
226244
/// Stops the tween if currently being played.
227245
/// </summary>
@@ -327,6 +345,7 @@ internal void Reset()
327345
OnReset();
328346
}
329347

348+
protected virtual bool CanComplete() => this.elapsed >= this.duration;
330349
protected virtual void OnUpdate() {}
331350
protected virtual void OnStart() {}
332351
protected virtual void OnStop() {}

Runtime/Tweening.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ public static class Tweening
5252
/// <summary>
5353
/// The number of tweens currently alive (not necessarily active).
5454
/// </summary>
55-
public static int Count => TweenManager.Instance.tweens.Count;
55+
public static int Count
56+
{
57+
get
58+
{
59+
if (TweenManager.HasInstance) {
60+
return TweenManager.Instance.tweens.Count;
61+
} else {
62+
return 0;
63+
}
64+
}
65+
}
5666

5767
/// <summary>
5868
/// The number of tweens that are currently alive and active.
@@ -61,6 +71,10 @@ public static int ActiveCount
6171
{
6272
get
6373
{
74+
if (!TweenManager.HasInstance) {
75+
return 0;
76+
}
77+
6478
int count = 0;
6579
List<Tween> tweens = TweenManager.Instance.tweens;
6680

0 commit comments

Comments
 (0)