File tree Expand file tree Collapse file tree 1 file changed +24
-9
lines changed
Expand file tree Collapse file tree 1 file changed +24
-9
lines changed Original file line number Diff line number Diff 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 ( )
You can’t perform that action at this time.
0 commit comments