Game Development Reference
In-Depth Information
84 //Is playing false
85 IsPlaying = false;
86
87 //Send Sprite Animation stopped event to gameobject
88 gameObject.SendMessage("SpriteAnimationStopped", AnimID,
SendMessageOptions.DontRequireReceiver);
89 }
90 //--------------------------------------------------------------
91 }
Line 23.
The public array member
Sprites
references a collection of
SpriteRenderer
components in the scene, together representing all frames for
an animation. The order of the SpriteRenderers in the array is important; the
order defines the direction and flow of the animation, with the first element
corresponding to the first frame, and the last element to the last frame. Notice
how this animation class works: it accepts a collection of preinstantiated
SpriteRenderers in the scene, and
shows
or
hides
them during animation
playback—showing only the current and active frame, while hiding all other
frames. This animation class will
not
edit or adjust sprite UVs.
Line 17.
The FPS public member defines the animation speed. Specifically,
it defines the number of sprites in the array SpriteRenderer that should be
processed (or shown)
per second
during animation playback.
Line 41.
The
PlaySpriteAnimation
coroutine should be called to start animation
playback. If the member
PlaybackType
is set to
PLAYLOOP
, the coroutine will
repeat endlessly unless
StopSpriteAnimation
is called.
Let's now put this animation class to the test by assembling a new weapon Prefab object in the
scene, starting with the fists/punch weapon. To do this, I'll drag the four punch sprite frames from
the atlas texture into the scene (each a unique frame of animation for the fists/punch weapon),
making sure all sprites are positioned at exactly the same world position, overlapping one another.
The sprites are
Spr_Punch_01
,
Spr_Punch_02
,
Spr_Punch_03
, and
Spr_Punch_04
. Overlapping
their position is important to maintain frame consistency across the animation. It doesn't matter
for appearances that the sprites overlap, since only one sprite will be shown at any one time
(see Figure
6-3
). The sprites will probably appear huge in comparison to the rest of the scene.
Don't worry about this for now; we'll fix it later.