Skip to content

Commit 073e3e9

Browse files
committed
Tween removes itself from layer .
1 parent 24ecf89 commit 073e3e9

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

Runtime/Tween.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ private Tween()
259259
_layer.AddToLayer(this);
260260
}
261261

262+
~Tween()
263+
{
264+
_layer.RemoveFromLayer(this);
265+
_layer = null!;
266+
}
267+
262268
#endregion
263269

264270
#region Properties

Runtime/TweenLayer.cs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using JetBrains.Annotations;
@@ -9,7 +9,7 @@ namespace DavidFDev.Tweening
99
/// <summary>
1010
/// Control all tweens in a layer at once.
1111
/// </summary>
12-
public sealed class TweenLayer : IReadOnlyList<Tween>
12+
public sealed class TweenLayer
1313
{
1414
#region Static fields
1515

@@ -21,7 +21,7 @@ public sealed class TweenLayer : IReadOnlyList<Tween>
2121
#region Fields
2222

2323
[NotNull]
24-
private readonly HashSet<Tween> _tweens = new HashSet<Tween>();
24+
private readonly List<WeakReference<Tween>> _tweens = new List<WeakReference<Tween>>();
2525

2626
private float _speed = 1f;
2727

@@ -44,12 +44,8 @@ public float Speed
4444
get => _speed;
4545
set => _speed = Mathf.Max(0f, value);
4646
}
47-
48-
[PublicAPI]
49-
public int Count => _tweens.Count;
50-
51-
[PublicAPI, NotNull]
52-
public Tween this[int index] => _tweens.ElementAt(index);
47+
48+
internal IEnumerable<WeakReference<Tween>> Tweens => _tweens;
5349

5450
#endregion
5551

@@ -62,8 +58,9 @@ public float Speed
6258
[PublicAPI]
6359
public void StartAll(float? duration = null)
6460
{
65-
foreach (var tween in _tweens)
61+
foreach (var tweenRef in _tweens)
6662
{
63+
if (!tweenRef.TryGetTarget(out var tween)) continue;
6764
tween.Start(duration);
6865
}
6966
}
@@ -74,30 +71,26 @@ public void StartAll(float? duration = null)
7471
[PublicAPI]
7572
public void StopAll()
7673
{
77-
foreach (var tween in _tweens)
74+
foreach (var tweenRef in _tweens)
7875
{
76+
if (!tweenRef.TryGetTarget(out var tween)) continue;
7977
tween.Stop();
8078
}
8179
}
8280

83-
public IEnumerator<Tween> GetEnumerator()
84-
{
85-
return _tweens.GetEnumerator();
86-
}
87-
8881
internal void AddToLayer([NotNull] Tween tween)
8982
{
90-
_tweens.Add(tween);
83+
_tweens.Add(new WeakReference<Tween>(tween));
9184
}
9285

9386
internal void RemoveFromLayer([NotNull] Tween tween)
9487
{
95-
_tweens.Remove(tween);
96-
}
97-
98-
IEnumerator IEnumerable.GetEnumerator()
99-
{
100-
return GetEnumerator();
88+
for (var i = 0; i < _tweens.Count; i += 1)
89+
{
90+
if (!_tweens[i].TryGetTarget(out var target) || target != tween) continue;
91+
_tweens.RemoveAt(i);
92+
return;
93+
}
10194
}
10295

10396
#endregion
@@ -129,7 +122,7 @@ public WaitForTweenLayer([NotNull] TweenLayer layer)
129122

130123
#region Properties
131124

132-
public override bool keepWaiting => Layer.Any(x => x.IsActive);
125+
public override bool keepWaiting => Layer.Tweens.Any(x => x.TryGetTarget(out var t) && t.IsActive);
133126

134127
#endregion
135128
}

0 commit comments

Comments
 (0)