Skip to content

Commit 140f53c

Browse files
committed
Use scene index to destroy tweens on scene unload
1 parent 74b2815 commit 140f53c

File tree

8 files changed

+45
-27
lines changed

8 files changed

+45
-27
lines changed

Runtime/Extensions/Misc/RectOffsetTweens.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ public static class RectOffsetTweens
77
public static Tween TweenLeft(this RectOffset offset, int to, float duration) =>
88
Tweening.To(getter: () => offset.left,
99
setter: left => offset.left = left,
10-
to, duration).SetTarget(offset);
10+
to, duration);
1111

1212
public static Tween TweenRight(this RectOffset offset, int to, float duration) =>
1313
Tweening.To(getter: () => offset.right,
1414
setter: right => offset.right = right,
15-
to, duration).SetTarget(offset);
15+
to, duration);
1616

1717
public static Tween TweenTop(this RectOffset offset, int to, float duration) =>
1818
Tweening.To(getter: () => offset.top,
1919
setter: top => offset.top = top,
20-
to, duration).SetTarget(offset);
20+
to, duration);
2121

2222
public static Tween TweenBottom(this RectOffset offset, int to, float duration) =>
2323
Tweening.To(getter: () => offset.bottom,
2424
setter: bottom => offset.bottom = bottom,
25-
to, duration).SetTarget(offset);
25+
to, duration);
2626

2727
}
2828

Runtime/Extensions/Physics/PhysicMaterialTweens.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ public static class PhysicMaterialTweens
77
public static Tween TweenBounciness(this PhysicMaterial material, float to, float duration) =>
88
Tweening.To(getter: () => material.bounciness,
99
setter: bounciness => material.bounciness = bounciness,
10-
to, duration).SetTarget(material);
10+
to, duration);
1111

1212
public static Tween TweenDynamicFriction(this PhysicMaterial material, float to, float duration) =>
1313
Tweening.To(getter: () => material.dynamicFriction,
1414
setter: friction => material.dynamicFriction = friction,
15-
to, duration).SetTarget(material);
15+
to, duration);
1616

1717
public static Tween TweenStaticFriction(this PhysicMaterial material, float to, float duration) =>
1818
Tweening.To(getter: () => material.staticFriction,
1919
setter: friction => material.staticFriction = friction,
20-
to, duration).SetTarget(material);
20+
to, duration);
2121

2222
}
2323

Runtime/Extensions/Physics2D/PhysicsMaterial2DTweens.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public static class PhysicsMaterial2DTweens
77
public static Tween TweenBounciness(this PhysicsMaterial2D material, float to, float duration) =>
88
Tweening.To(getter: () => material.bounciness,
99
setter: bounciness => material.bounciness = bounciness,
10-
to, duration).SetTarget(material);
10+
to, duration);
1111

1212
public static Tween TweenFriction(this PhysicsMaterial2D material, float to, float duration) =>
1313
Tweening.To(getter: () => material.friction,
1414
setter: friction => material.friction = friction,
15-
to, duration).SetTarget(material);
15+
to, duration);
1616

1717
}
1818

Runtime/Extensions/Rendering/MaterialTweens.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ public static class MaterialTweens
77
public static Tween TweenColor(this Material material, Color to, float duration) =>
88
Tweening.To(getter: () => material.color,
99
setter: color => material.color = color,
10-
to, duration).SetTarget(material);
10+
to, duration);
1111

1212
public static Tween TweenAlpha(this Material material, float to, float duration) =>
1313
Tweening.To(getter: () => material.color.a,
1414
setter: alpha => material.color = new Color(material.color.r, material.color.g, material.color.b, alpha),
15-
to, duration).SetTarget(material);
15+
to, duration);
1616

1717
public static Tween TweenMainTextureScale(this Material material, Vector2 to, float duration) =>
1818
Tweening.To(getter: () => material.mainTextureScale,
1919
setter: mainTextureScale => material.mainTextureScale = mainTextureScale,
20-
to, duration).SetTarget(material);
20+
to, duration);
2121

2222
public static Tween TweenMainTextureOffset(this Material material, Vector2 to, float duration) =>
2323
Tweening.To(getter: () => material.mainTextureOffset,
2424
setter: mainTextureOffset => material.mainTextureOffset = mainTextureOffset,
25-
to, duration).SetTarget(material);
25+
to, duration);
2626

2727
}
2828

Runtime/PropertyChaining.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using UnityEngine;
2+
13
namespace Zigurous.Tweening
24
{
35
/// <summary>
@@ -7,13 +9,23 @@ namespace Zigurous.Tweening
79
/// </summary>
810
public static class PropertyChaining
911
{
10-
/// <summary>
11-
/// Sets the id of the tween such that it can be identified by the given
12-
/// target object.
13-
/// </summary>
14-
public static T SetTarget<T, U>(this T tween, U target) where T: Tween
12+
public static T SetTarget<T>(this T tween, Component target) where T: Tween
13+
{
14+
if (target != null)
15+
{
16+
tween.id = target.GetHashCode();
17+
tween.sceneIndex = target.gameObject.scene.buildIndex;
18+
}
19+
return tween;
20+
}
21+
22+
public static T SetTarget<T>(this T tween, GameObject target) where T: Tween
1523
{
16-
tween.id = target.GetHashCode();
24+
if (target != null)
25+
{
26+
tween.id = target.GetHashCode();
27+
tween.sceneIndex = target.scene.buildIndex;
28+
}
1729
return tween;
1830
}
1931

Runtime/Tween.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public abstract class Tween
1414
/// </summary>
1515
public int id;
1616

17+
/// <summary>
18+
/// The index of the scene that contains the object being animated by
19+
/// the tween. The is used to kill the tween when the scene is unloaded.
20+
/// </summary>
21+
internal int sceneIndex = -1;
22+
1723
/// <summary>
1824
/// The template type used by the tween, if relevant.
1925
/// </summary>
@@ -398,6 +404,8 @@ public void Restart()
398404
internal void Reset()
399405
{
400406
this.id = -1;
407+
this.sceneIndex = -1;
408+
401409
this.state = TweenState.Ready;
402410
this.internalState = InternalTweenState.Queued;
403411

Runtime/TweenManager.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,15 @@ internal void Track(Tween tween)
185185
}
186186

187187
/// <summary>
188-
/// Kills all tweens when the active scene is unloaded.
188+
/// Kills all tweens that are animating objects on the unloaded scene.
189189
/// </summary>
190190
private void SceneUnloaded(Scene scene)
191191
{
192-
if (Tweening.killTweensOnSceneUnload) {
193-
Tweening.KillAll();
192+
foreach (Tween tween in this.tweens)
193+
{
194+
if (tween.sceneIndex == -1 || tween.sceneIndex == scene.buildIndex) {
195+
tween.Kill();
196+
}
194197
}
195198
}
196199

Runtime/Tweening.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ 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-
5146
/// <summary>
5247
/// The initial amount of tweens that memory is allocated for when the
5348
/// system starts. Additional memory will be allocated as needed.

0 commit comments

Comments
 (0)