Skip to content

Commit 9d96e6d

Browse files
committed
Kill tweens on scene unload
1 parent e57e077 commit 9d96e6d

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

Runtime/TweenManager.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using UnityEngine;
3+
using UnityEngine.SceneManagement;
34

45
namespace Zigurous.Tweening
56
{
@@ -39,8 +40,7 @@ internal static TweenManager Instance
3940
GameObject singleton = new GameObject();
4041
singleton.name = typeof(TweenManager).Name;
4142
singleton.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
42-
43-
_instance = singleton.AddComponent<TweenManager>();
43+
singleton.AddComponent<TweenManager>();
4444
DontDestroyOnLoad(singleton);
4545
}
4646
}
@@ -54,9 +54,14 @@ private void Awake()
5454
{
5555
_isUnloading = false;
5656

57-
if (_instance == null) {
57+
if (_instance == null)
58+
{
5859
_instance = this;
59-
} else {
60+
61+
SceneManager.sceneUnloaded += SceneUnloaded;
62+
}
63+
else
64+
{
6065
Destroy(this);
6166
}
6267
}
@@ -65,8 +70,11 @@ private void OnDestroy()
6570
{
6671
_isUnloading = true;
6772

68-
if (_instance == this) {
73+
if (_instance == this)
74+
{
6975
_instance = null;
76+
77+
SceneManager.sceneUnloaded -= SceneUnloaded;
7078
}
7179

7280
foreach (Tween tween in this.tweens) {
@@ -176,6 +184,16 @@ internal void Track(Tween tween)
176184
}
177185
}
178186

187+
/// <summary>
188+
/// Kills all tweens when the active scene is unloaded.
189+
/// </summary>
190+
private void SceneUnloaded(Scene scene)
191+
{
192+
if (Tweening.killTweensOnSceneUnload) {
193+
Tweening.KillAll();
194+
}
195+
}
196+
179197
}
180198

181199
}

Runtime/Tweening.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public static class Tweening
4343
/// </summary>
4444
public static float overshoot = 1.70158f;
4545

46+
/// <summary>
47+
/// Kills all tweens when the active scene is unloaded.
48+
/// </summary>
49+
public static bool killTweensOnSceneUnload = true;
50+
4651
/// <summary>
4752
/// The initial amount of tweens that memory is allocated for when the
4853
/// system starts. Additional memory will be allocated as needed.

0 commit comments

Comments
 (0)