Skip to content

Commit 6199d45

Browse files
committed
Added tween layers.
1 parent a9b55cf commit 6199d45

File tree

4 files changed

+166
-4
lines changed

4 files changed

+166
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
- Added tween layers for controlling multiple tweens at a time.
89
- Added option to tween using unscaled delta time.
9-
- Added WaitForTween custom yield instruction.
10+
- Added WaitForTween and WaitForTweenLayer as custom yield instructions.
1011
- Added JetBrain annotations.
1112
- Replaced exceptions with Debug.Warning.
1213

Runtime/Tween.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ public static Tween Create<T>([NotNull] object target, [NotNull] string property
241241

242242
private Action _onComplete;
243243

244+
[NotNull]
245+
private TweenLayer _layer;
246+
244247
#endregion
245248

246249
#region Constructors
@@ -252,6 +255,8 @@ private Tween()
252255
LerpFunction = null!;
253256
EasingFunction = null!;
254257
UnderlyingType = null!;
258+
_layer = TweenLayer.Default;
259+
_layer.AddToLayer(this);
255260
}
256261

257262
#endregion
@@ -269,13 +274,40 @@ private Tween()
269274
/// </summary>
270275
[PublicAPI]
271276
public bool IsPaused { get; set; }
277+
278+
/// <summary>
279+
/// Whether the tween animation is able to update. Affected by pausing and time scale.
280+
/// </summary>
281+
[PublicAPI]
282+
public bool IsAbleToUpdate => !IsPaused && !Layer.IsPaused && (IsUnscaled || Time.timeScale > 0f);
272283

273284
/// <summary>
274285
/// Whether the tween animation should use Time.unscaledDeltaTime.
275286
/// </summary>
276287
[PublicAPI]
277288
public bool IsUnscaled { get; set; }
278289

290+
/// <summary>
291+
/// Controlling layer that the tween is a part of.
292+
/// </summary>
293+
[PublicAPI, NotNull]
294+
public TweenLayer Layer
295+
{
296+
get => _layer;
297+
set
298+
{
299+
if (value == _layer)
300+
{
301+
Debug.LogError("Failed to set tween layer: already on the specified layer.");
302+
return;
303+
}
304+
305+
_layer.RemoveFromLayer(this);
306+
_layer = value;
307+
_layer.AddToLayer(this);
308+
}
309+
}
310+
279311
/// <summary>
280312
/// Starting value of the tween (0%).
281313
/// </summary>
@@ -403,7 +435,7 @@ public object GetTweenedValueAt(float progress)
403435
[PublicAPI, Pure]
404436
public override string ToString()
405437
{
406-
return $"{StartValue} to {EndValue} over {TotalDuration} seconds{(IsActive ? $" ({GetProgress()*100:0.0}%: {CurrentValue ?? "-"}){(IsPaused ? " [Paused]" : "")}" : "")}";
438+
return $"{StartValue} to {EndValue} over {TotalDuration} seconds{(IsActive ? $" ({GetProgress()*100:0.0}%: {CurrentValue ?? "-"}){(!IsAbleToUpdate ? " [Paused]" : "")}" : "")}";
407439
}
408440

409441
/// <summary>
@@ -426,8 +458,8 @@ private IEnumerator Update()
426458
// Begin loop
427459
while (ElapsedTime <= TotalDuration)
428460
{
429-
// Pause if paused (duh!)
430-
while (IsPaused)
461+
// Pause if unable to update
462+
while (!IsAbleToUpdate)
431463
{
432464
yield return null;
433465
}

Runtime/TweenLayer.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using JetBrains.Annotations;
5+
using UnityEngine;
6+
7+
namespace DavidFDev.Tweening
8+
{
9+
/// <summary>
10+
/// Control all tweens in a layer at once.
11+
/// </summary>
12+
public sealed class TweenLayer : IReadOnlyList<Tween>
13+
{
14+
#region Static fields
15+
16+
[PublicAPI, NotNull]
17+
public static readonly TweenLayer Default = new TweenLayer();
18+
19+
#endregion
20+
21+
#region Fields
22+
23+
[NotNull]
24+
private readonly HashSet<Tween> _tweens = new HashSet<Tween>();
25+
26+
#endregion
27+
28+
#region Properties
29+
30+
/// <summary>
31+
/// Whether all tween animations are paused on this layer.
32+
/// </summary>
33+
[PublicAPI]
34+
public bool IsPaused { get; set; }
35+
36+
[PublicAPI]
37+
public int Count => _tweens.Count;
38+
39+
[PublicAPI, NotNull]
40+
public Tween this[int index] => _tweens.ElementAt(index);
41+
42+
#endregion
43+
44+
#region Methods
45+
46+
/// <summary>
47+
/// Start all tweens on this layer.
48+
/// </summary>
49+
/// <param name="duration">Optionally change the tween's duration to a new value or, if null, remain the same.</param>
50+
[PublicAPI]
51+
public void StartAll(float? duration = null)
52+
{
53+
foreach (var tween in _tweens)
54+
{
55+
tween.Start(duration);
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Stop all tweens on this layer.
61+
/// </summary>
62+
[PublicAPI]
63+
public void StopAll()
64+
{
65+
foreach (var tween in _tweens)
66+
{
67+
tween.Stop();
68+
}
69+
}
70+
71+
public IEnumerator<Tween> GetEnumerator()
72+
{
73+
return _tweens.GetEnumerator();
74+
}
75+
76+
internal void AddToLayer([NotNull] Tween tween)
77+
{
78+
_tweens.Add(tween);
79+
}
80+
81+
internal void RemoveFromLayer([NotNull] Tween tween)
82+
{
83+
_tweens.Remove(tween);
84+
}
85+
86+
IEnumerator IEnumerable.GetEnumerator()
87+
{
88+
return GetEnumerator();
89+
}
90+
91+
#endregion
92+
}
93+
94+
#region Other types
95+
96+
/// <summary>
97+
/// Yield instruction that waits for all tweens in a layer to finish.
98+
/// Usage: yield return new WaitForTweenLayer(...)
99+
/// </summary>
100+
public sealed class WaitForTweenLayer : CustomYieldInstruction
101+
{
102+
#region Fields
103+
104+
[PublicAPI, NotNull]
105+
public readonly TweenLayer Layer;
106+
107+
#endregion
108+
109+
#region Constructors
110+
111+
public WaitForTweenLayer([NotNull] TweenLayer layer)
112+
{
113+
Layer = layer;
114+
}
115+
116+
#endregion
117+
118+
#region Properties
119+
120+
public override bool keepWaiting => Layer.Any(x => x.IsActive);
121+
122+
#endregion
123+
}
124+
125+
#endregion
126+
}

Runtime/TweenLayer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)