Game Development Reference
In-Depth Information
Animations, Frames, and Prefabs
Before moving further, refining the derived gun classes to implement extended and specialized
functionality for each weapon, we'll take a detour into animation. This might initially seem a misplaced
detour. But animation will play an important role for our two weapon classes, because both must
display a fire or attack animation each time the weapon is used. Consider Figures 6-1 and 6-2 ,
which show not only each weapon, but also the frames of animation that should play when fired.
When the gamer presses the Fire button, we'll want the active weapon to cycle through its frames of
animation, returning back to the original, neutral frame when completed. To achieve this, a new class
must be coded. Specifically, this class will accept an array of sprite objects in the scene (each sprite
representing a single frame in an animation sequence) and it will hide and show all related sprites
(frames) in sequence to play back the complete animation, frame by frame. Take a look at the class in
Listing 6-4, called SpriteShowAnimator.cs . Comments follow.
Listing 6-4. SpriteShowAnimator.cs: Class to Display a Sprite Animation
01 //This class maintains a collection of sprite objects as frames of animation
02 //It shows and hides those frames according to a set of playback settings
03 //--------------------------------------------------------------
04 using UnityEngine;
05 using System.Collections;
06 //--------------------------------------------------------------
07 public class SpriteShowAnimator : MonoBehaviour
08 {
09 //--------------------------------------------------------------
10 //Playback types - run once or loop forever
11 public enum ANIMATOR_PLAYBACK_TYPE {PLAYONCE = 0, PLAYLOOP = 1};
12
13 //Playback type for this animation
14 public ANIMATOR_PLAYBACK_TYPE PlaybackType = ANIMATOR_PLAYBACK_TYPE.PLAYONCE;
15
16 //Frames per second to play for this animation
17 public int FPS = 5;
18
19 //Custom ID for animation - used with function PlaySpriteAnimation
20 public int AnimationID = 0;
21
22 //Frames of animation
23 public SpriteRenderer[] Sprites = null;
24
25 //Should auto-play?
26 public bool AutoPlay = false;
27
28 //Should first hide all sprite renderers on playback? or leave at defaults
29 public bool HideSpritesOnStart = true;
30
31 //Boolean indicating whether animation is currently playing
32 bool IsPlaying = false;
 
Search WWH ::




Custom Search