Skip to content

Commit 778b427

Browse files
author
Cory Leach
committed
Update TweenExtensions.cs
Fixing some NRE exceptions that could happen when canceling a tween upon application exit
1 parent 735fab3 commit 778b427

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

Runtime/Tween/TweenExtensions.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public static class TweenExtensions
1111
private static CancellationTokenSource _cancellationTokenSource;
1212
private static Dictionary<int, TweenData> _tweenDict;
1313

14+
private static CancellationToken CancellationToken => _cancellationTokenSource.Token;
15+
16+
private static bool CanTween => Application.isPlaying && !CancellationToken.IsCancellationRequested;
17+
1418
[RuntimeInitializeOnLoadMethod]
1519
public static void Initialize()
1620
{
@@ -37,7 +41,7 @@ public static void CancelAllTweens()
3741
_cancellationTokenSource.Cancel();
3842
_cancellationTokenSource.Dispose();
3943
_cancellationTokenSource = new CancellationTokenSource();
40-
_tweenDict.Clear();
44+
_tweenDict?.Clear();
4145
}
4246

4347
public static void DoKillTweens(this GameObject obj)
@@ -57,12 +61,12 @@ public static void DoKillTweens(this UnityEngine.Object obj)
5761

5862
public static async Task DoTweenAsync(int id, float duration, Action<float> action, Easing easeType = Easing.Linear, AnimationCurve customCurve = null)
5963
{
60-
await DoTweenAsync(id, duration, _cancellationTokenSource.Token, action, easeType, customCurve);
64+
await DoTweenAsync(id, duration, CancellationToken, action, easeType, customCurve);
6165
}
6266

6367
public static async Task DoPunchTweenAsync(int id, float duration, Action<float> action, Easing easeType = Easing.Linear, AnimationCurve customCurve = null)
6468
{
65-
await DoPunchTweenAsync(id, duration, _cancellationTokenSource.Token, action, easeType, customCurve);
69+
await DoPunchTweenAsync(id, duration, CancellationToken, action, easeType, customCurve);
6670
}
6771

6872
public static Task DoTweenAsync(int id, float duration, CancellationToken cancellationToken, Action<float> action, Easing easeType = Easing.Linear, AnimationCurve customCurve = null)
@@ -83,7 +87,7 @@ public static async Task DoTweenAsyncWithLerp(Func<float,float,float,float> lerp
8387
var ease = easeType != Easing.CustomCurve ? EaseFunctions.Get(easeType) : customCurve.Evaluate;
8488
action?.Invoke(ease.Invoke(0));
8589

86-
while (t < duration && Application.isPlaying)
90+
while (t < duration && CanTween)
8791
{
8892
await Task.Yield();
8993

@@ -96,8 +100,8 @@ public static async Task DoTweenAsyncWithLerp(Func<float,float,float,float> lerp
96100
action?.Invoke(ease.Invoke(lerpMethod(0, duration, t)));
97101
}
98102

99-
//Just exit immediately if we've stopped playing in editor
100-
if (!Application.isPlaying)
103+
//Just exit immediately if we've stopped
104+
if (!CanTween)
101105
{
102106
return;
103107
}
@@ -132,6 +136,11 @@ private static CancellationToken StartTween(int id)
132136

133137
private static void CompleteTween(int id)
134138
{
139+
if (_tweenDict == null)
140+
{
141+
return;
142+
}
143+
135144
if (!_tweenDict.TryGetValue(id, out var tweenData))
136145
{
137146
return;
@@ -151,7 +160,7 @@ private static void CompleteTween(int id)
151160

152161
public static void CancelTweensForId(int id)
153162
{
154-
if (!_tweenDict.TryGetValue(id, out var tweenData))
163+
if (_tweenDict == null || !_tweenDict.TryGetValue(id, out var tweenData))
155164
{
156165
return;
157166
}

0 commit comments

Comments
 (0)