Skip to content

Commit

Permalink
Kill tweens on scene unload
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgraham committed Jun 15, 2021
1 parent e57e077 commit 9d96e6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
28 changes: 23 additions & 5 deletions Runtime/TweenManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Zigurous.Tweening
{
Expand Down Expand Up @@ -39,8 +40,7 @@ internal static TweenManager Instance
GameObject singleton = new GameObject();
singleton.name = typeof(TweenManager).Name;
singleton.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;

_instance = singleton.AddComponent<TweenManager>();
singleton.AddComponent<TweenManager>();
DontDestroyOnLoad(singleton);
}
}
Expand All @@ -54,9 +54,14 @@ private void Awake()
{
_isUnloading = false;

if (_instance == null) {
if (_instance == null)
{
_instance = this;
} else {

SceneManager.sceneUnloaded += SceneUnloaded;
}
else
{
Destroy(this);
}
}
Expand All @@ -65,8 +70,11 @@ private void OnDestroy()
{
_isUnloading = true;

if (_instance == this) {
if (_instance == this)
{
_instance = null;

SceneManager.sceneUnloaded -= SceneUnloaded;
}

foreach (Tween tween in this.tweens) {
Expand Down Expand Up @@ -176,6 +184,16 @@ internal void Track(Tween tween)
}
}

/// <summary>
/// Kills all tweens when the active scene is unloaded.
/// </summary>
private void SceneUnloaded(Scene scene)
{
if (Tweening.killTweensOnSceneUnload) {
Tweening.KillAll();
}
}

}

}
5 changes: 5 additions & 0 deletions Runtime/Tweening.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public static class Tweening
/// </summary>
public static float overshoot = 1.70158f;

/// <summary>
/// Kills all tweens when the active scene is unloaded.
/// </summary>
public static bool killTweensOnSceneUnload = true;

/// <summary>
/// The initial amount of tweens that memory is allocated for when the
/// system starts. Additional memory will be allocated as needed.
Expand Down

0 comments on commit 9d96e6d

Please sign in to comment.