Advertisement
infinite_ammo

SpriteAnimation.cs

Feb 9th, 2013
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [System.Serializable]
  5. public class Animation
  6. {
  7.     public string name;
  8.     public float delay;
  9.     public Texture2D[] textures;
  10.     public bool loop;
  11.    
  12.     [HideInInspector]
  13.     public int frame;
  14.    
  15.     public void NextFrame()
  16.     {
  17.         frame++;
  18.        
  19.         if (frame >= textures.Length)
  20.         {
  21.             if (loop)
  22.             {
  23.                 frame = 0;
  24.             }
  25.             else
  26.             {
  27.                 frame = textures.Length - 1;
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. public class SpriteAnimation : MonoBehaviour
  34. {
  35.     public Animation[] animations;
  36.    
  37.     public float speed = 1f;
  38.    
  39.     public bool isPlaying { get; private set; }
  40.     public float timer { get; private set; }
  41.    
  42.     public Animation currentAnimation { get; private set; }
  43.    
  44.     public void Play(string name)
  45.     {
  46.         PlayFromFrame(name, 0);
  47.     }
  48.    
  49.     public void PlayFromFrame(string name, int frame)
  50.     {
  51.         Animation animation = GetAnimation(name);
  52.         if (animation != null)
  53.         {
  54.             currentAnimation = animation;
  55.             currentAnimation.frame = frame;
  56.             isPlaying = true;
  57.             timer = 0f;
  58.             SetCurrentFrame();
  59.         }
  60.     }
  61.    
  62.     public Animation GetAnimation(string name)
  63.     {
  64.         foreach (Animation animation in animations)
  65.         {
  66.             if (animation.name == name)
  67.                 return animation;
  68.         }
  69.         return null;
  70.     }
  71.    
  72.     void SetCurrentFrame()
  73.     {
  74.         if (currentAnimation != null)
  75.         {
  76.             renderer.material.mainTexture = currentAnimation.textures[currentAnimation.frame];
  77.         }
  78.     }
  79.    
  80.     void Update()
  81.     {
  82.         if (isPlaying)
  83.         {
  84.             if (currentAnimation != null)
  85.             {
  86.                 if (currentAnimation.delay > 0f)
  87.                 {
  88.                     timer += Time.deltaTime * speed;
  89.                     while (timer > currentAnimation.delay)
  90.                     {
  91.                         currentAnimation.NextFrame();
  92.                         timer -= currentAnimation.delay;
  93.                     }
  94.                 }
  95.                
  96.                 SetCurrentFrame();
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement