Skip to content

Commit 73af3ad

Browse files
committed
Add support for reverse animated sprites
1 parent 18804cd commit 73af3ad

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

Runtime/AnimatedSprite.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public sealed class AnimatedSprite : MonoBehaviour
2525
[Tooltip("The amount of frames per second that are rendered.")]
2626
public float frameRate = 24.0f;
2727

28+
/// <summary>
29+
/// Animates the sprites in reverse order.
30+
/// </summary>
31+
[Tooltip("Animates the sprites in reverse order.")]
32+
public bool reversed;
33+
2834
/// <summary>
2935
/// Whether the animation should loop back to the start after cycling
3036
/// through each sprite.
@@ -47,9 +53,10 @@ private void Awake()
4753
this.spriteRenderer = GetComponent<SpriteRenderer>();
4854
}
4955

50-
private void OnEnable()
56+
private void Start()
5157
{
52-
SetNextFrameTime();
58+
this.frame = 0;
59+
SetSprite();
5360
}
5461

5562
private void Update()
@@ -61,22 +68,30 @@ private void Update()
6168

6269
private void NextFrame()
6370
{
64-
this.frame++;
71+
if (this.reversed) {
72+
this.frame--;
73+
} else {
74+
this.frame++;
75+
}
6576

66-
if (this.frame >= this.sprites.Length)
77+
if (this.frame < 0 || this.frame >= this.sprites.Length)
6778
{
6879
if (this.loop) {
69-
this.frame = 0;
80+
this.frame = this.reversed ? this.sprites.Length - 1 : 0;
7081
} else {
71-
this.frame = Mathf.Max(this.sprites.Length - 1, 0);
82+
this.frame = Mathf.Clamp(this.frame, 0, this.sprites.Length - 1);
7283
}
7384
}
7485

75-
if (this.frame < this.sprites.Length) {
86+
SetSprite();
87+
SetNextFrameTime();
88+
}
89+
90+
private void SetSprite()
91+
{
92+
if (this.frame >= 0 && this.frame < this.sprites.Length) {
7693
this.spriteRenderer.sprite = this.sprites[this.frame];
7794
}
78-
79-
SetNextFrameTime();
8095
}
8196

8297
private void SetNextFrameTime()

0 commit comments

Comments
 (0)