Advertisement
infinite_ammo

SpriteAnimator.cs

Nov 16th, 2013
7,984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SpriteAnimator : MonoBehaviour
  5. {
  6.     [System.Serializable]
  7.     public class AnimationTrigger
  8.     {
  9.         public int frame;
  10.         public string name;
  11.     }
  12.  
  13.     [System.Serializable]
  14.     public class Animation
  15.     {
  16.         public string name;
  17.         public int fps;
  18.         public Sprite[] frames;
  19.  
  20.         public AnimationTrigger[] triggers;
  21.     }
  22.  
  23.     public SpriteRenderer spriteRenderer;
  24.     public Animation[] animations;
  25.  
  26.     public bool playing { get; private set; }
  27.     public Animation currentAnimation { get; private set; }
  28.     public int currentFrame { get; private set; }
  29.     public bool loop { get; private set; }
  30.  
  31.     public string playAnimationOnStart;
  32.  
  33.     void Awake()
  34.     {
  35.         if (!spriteRenderer)
  36.             spriteRenderer = GetComponent<SpriteRenderer>();
  37.     }
  38.  
  39.     void OnEnable()
  40.     {
  41.         if (playAnimationOnStart != "")
  42.             Play(playAnimationOnStart);
  43.     }
  44.  
  45.     void OnDisable()
  46.     {
  47.         playing = false;
  48.         currentAnimation = null;
  49.     }
  50.  
  51.     public void Play(string name, bool loop = true, int startFrame = 0)
  52.     {
  53.         Animation animation = GetAnimation(name);
  54.         if (animation != null )
  55.         {
  56.             if (animation != currentAnimation)
  57.             {
  58.                 ForcePlay(name, loop, startFrame);
  59.             }
  60.         }
  61.         else
  62.         {
  63.             Debug.LogWarning("could not find animation: " + name);
  64.         }
  65.     }
  66.  
  67.     public void ForcePlay(string name, bool loop = true, int startFrame = 0)
  68.     {
  69.         Animation animation = GetAnimation(name);
  70.         if (animation != null)
  71.         {
  72.             this.loop = loop;
  73.             currentAnimation = animation;
  74.             playing = true;
  75.             currentFrame = startFrame;
  76.             spriteRenderer.sprite = animation.frames[currentFrame];
  77.             StopAllCoroutines();
  78.             StartCoroutine(PlayAnimation(currentAnimation));
  79.         }
  80.     }
  81.  
  82.     public void SlipPlay(string name, int wantFrame, params string[] otherNames)
  83.     {
  84.         for (int i = 0; i < otherNames.Length; i++)
  85.         {
  86.             if (currentAnimation != null && currentAnimation.name == otherNames[i])
  87.             {
  88.                 Play(name, true, currentFrame);
  89.                 break;
  90.             }
  91.         }
  92.         Play(name, true, wantFrame);
  93.     }
  94.  
  95.     public bool IsPlaying(string name)
  96.     {
  97.         return (currentAnimation != null && currentAnimation.name == name);
  98.     }
  99.  
  100.     public Animation GetAnimation(string name)
  101.     {
  102.         foreach (Animation animation in animations)
  103.         {
  104.             if (animation.name == name)
  105.             {
  106.                 return animation;
  107.             }
  108.         }
  109.         return null;
  110.     }
  111.  
  112.     IEnumerator PlayAnimation(Animation animation)
  113.     {
  114.         float timer = 0f;
  115.         float delay = 1f / (float)animation.fps;
  116.         while (loop || currentFrame < animation.frames.Length-1)
  117.         {
  118.  
  119.             while (timer < delay)
  120.             {
  121.                 timer += Time.deltaTime;
  122.                 yield return 0f;
  123.             }
  124.             while (timer > delay)
  125.             {
  126.                 timer -= delay;
  127.                 NextFrame(animation);
  128.             }
  129.  
  130.             spriteRenderer.sprite = animation.frames[currentFrame];
  131.         }
  132.  
  133.         currentAnimation = null;
  134.     }
  135.  
  136.     void NextFrame(Animation animation)
  137.     {
  138.         currentFrame++;
  139.         foreach (AnimationTrigger animationTrigger in currentAnimation.triggers)
  140.         {
  141.             if (animationTrigger.frame == currentFrame)
  142.             {
  143.                 gameObject.SendMessageUpwards(animationTrigger.name);
  144.             }
  145.         }
  146.  
  147.         if (currentFrame >= animation.frames.Length)
  148.         {
  149.             if (loop)
  150.                 currentFrame = 0;
  151.             else
  152.                 currentFrame = animation.frames.Length - 1;
  153.         }
  154.     }
  155.  
  156.     public int GetFacing()
  157.     {
  158.         return (int)Mathf.Sign(spriteRenderer.transform.localScale.x);
  159.     }
  160.  
  161.     public void FlipTo(float dir)
  162.     {
  163.         if (dir < 0f)
  164.             spriteRenderer.transform.localScale = new Vector3(-1f, 1f, 1f);
  165.         else
  166.             spriteRenderer.transform.localScale = new Vector3(1f, 1f, 1f);
  167.     }
  168.  
  169.     public void FlipTo(Vector3 position)
  170.     {
  171.         float diff = position.x - transform.position.x;
  172.         if (diff < 0f)
  173.             spriteRenderer.transform.localScale = new Vector3(-1f, 1f, 1f);
  174.         else
  175.             spriteRenderer.transform.localScale = new Vector3(1f, 1f, 1f);
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement