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
25
25
[ Tooltip ( "The amount of frames per second that are rendered." ) ]
26
26
public float frameRate = 24.0f ;
27
27
28
+ /// <summary>
29
+ /// Animates the sprites in reverse order.
30
+ /// </summary>
31
+ [ Tooltip ( "Animates the sprites in reverse order." ) ]
32
+ public bool reversed ;
33
+
28
34
/// <summary>
29
35
/// Whether the animation should loop back to the start after cycling
30
36
/// through each sprite.
@@ -47,9 +53,10 @@ private void Awake()
47
53
this . spriteRenderer = GetComponent < SpriteRenderer > ( ) ;
48
54
}
49
55
50
- private void OnEnable ( )
56
+ private void Start ( )
51
57
{
52
- SetNextFrameTime ( ) ;
58
+ this . frame = 0 ;
59
+ SetSprite ( ) ;
53
60
}
54
61
55
62
private void Update ( )
@@ -61,22 +68,30 @@ private void Update()
61
68
62
69
private void NextFrame ( )
63
70
{
64
- this . frame ++ ;
71
+ if ( this . reversed ) {
72
+ this . frame -- ;
73
+ } else {
74
+ this . frame ++ ;
75
+ }
65
76
66
- if ( this . frame >= this . sprites . Length )
77
+ if ( this . frame < 0 || this . frame >= this . sprites . Length )
67
78
{
68
79
if ( this . loop ) {
69
- this . frame = 0 ;
80
+ this . frame = this . reversed ? this . sprites . Length - 1 : 0 ;
70
81
} else {
71
- this . frame = Mathf . Max ( this . sprites . Length - 1 , 0 ) ;
82
+ this . frame = Mathf . Clamp ( this . frame , 0 , this . sprites . Length - 1 ) ;
72
83
}
73
84
}
74
85
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 ) {
76
93
this . spriteRenderer . sprite = this . sprites [ this . frame ] ;
77
94
}
78
-
79
- SetNextFrameTime ( ) ;
80
95
}
81
96
82
97
private void SetNextFrameTime ( )
You can’t perform that action at this time.
0 commit comments