Advertisement
infinite_ammo

SpriteAnimator.cs

Jan 17th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.41 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 string name;
  10.         public int frame;
  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 string sequenceCode;
  21.         public string cue;
  22.  
  23.         public AnimationTrigger[] triggers;
  24.     }
  25.  
  26.     // sequence code example:
  27.     // 0-1:3 2-3:3 4-5:4 6-7:4 8:3 9:3
  28.  
  29.     public string setAnimationName;
  30.     public string setAnimationPath;
  31.     public string setAnimationSpriteName;
  32.     public int setAnimationStartFrame;
  33.  
  34.     public SpriteRenderer spriteRenderer;
  35.     public Animation[] animations;
  36.  
  37.     public bool playing { get; private set; }
  38.     public Animation currentAnimation { get; private set; }
  39.     public int currentFrame { get; private set; }
  40.     [HideInInspector]
  41.     public bool loop;
  42.  
  43.     public string playAnimationOnStart;
  44.  
  45.     bool looped;
  46.  
  47.     /*
  48.     void Start()
  49.     {
  50.         if (playAnimationOnStart != "")
  51.             Play(playAnimationOnStart);
  52.     }
  53.     */
  54.  
  55.     void Start()
  56.     {
  57.         if (!spriteRenderer)
  58.             spriteRenderer = GetComponent<SpriteRenderer>();
  59.  
  60.         if (playAnimationOnStart != "")
  61.             Play(playAnimationOnStart);
  62.     }
  63.    
  64.     void OnDisable()
  65.     {
  66.         playing = false;
  67.         currentAnimation = null;
  68.     }
  69.  
  70.     public void Play(string name, bool loop = true, int startFrame = 0)
  71.     {
  72.         Animation animation = GetAnimation(name);
  73.         if (animation != null )
  74.         {
  75.             if (animation != currentAnimation)
  76.             {
  77.                 ForcePlay(name, loop, startFrame);
  78.             }
  79.         }
  80.         else
  81.         {
  82.             Debug.LogWarning("could not find animation: " + name);
  83.         }
  84.     }
  85.  
  86.     public void ForcePlay(string name, bool loop = true, int startFrame = 0)
  87.     {
  88.         Animation animation = GetAnimation(name);
  89.         if (animation != null )
  90.         {
  91.             this.loop = loop;
  92.             currentAnimation = animation;
  93.             playing = true;
  94.             currentFrame = startFrame;
  95.             spriteRenderer.sprite = animation.frames[currentFrame];
  96.             StopAllCoroutines();
  97.             StartCoroutine(PlayAnimation(currentAnimation));
  98.         }
  99.     }
  100.  
  101.     public void SlipPlay(string name, int wantFrame, params string[] otherNames)
  102.     {
  103.         for (int i = 0; i < otherNames.Length; i++)
  104.         {
  105.             if (currentAnimation != null && currentAnimation.name == otherNames[i])
  106.             {
  107.                 Play(name, true, currentFrame);
  108.                 break;
  109.             }
  110.         }
  111.         Play(name, true, wantFrame);
  112.     }
  113.  
  114.     public bool IsPlaying(string name)
  115.     {
  116.         return (currentAnimation != null && currentAnimation.name == name);
  117.     }
  118.  
  119.     public Animation GetAnimation(string name)
  120.     {
  121.         foreach (Animation animation in animations)
  122.         {
  123.             if (animation.name == name)
  124.             {
  125.                 return animation;
  126.             }
  127.         }
  128.         return null;
  129.     }
  130.  
  131.     IEnumerator CueAnimation(string animationName, float minTime, float maxTime)
  132.     {
  133.         yield return new WaitForSeconds(Random.Range(minTime, maxTime));
  134.         ForcePlay(animationName, false);
  135.     }
  136.  
  137.     IEnumerator PlayAnimation(Animation animation)
  138.     {
  139.         playing = true;
  140.         //Debug.Log("Playing animation: " + animation.name);
  141.  
  142.         float timer = 0f;
  143.         float delay = 1f / (float)animation.fps;
  144.         string cueOnComplete = "";
  145.  
  146.         if (animation.cue != null && animation.cue != "")
  147.         {
  148.             if (animation.cue.IndexOf(':') != -1)
  149.             {
  150.                 string[] dataBits = animation.cue.Trim().Split(':');
  151.  
  152.                 string animationName = dataBits[1];
  153.                 dataBits = dataBits[0].Split('-');
  154.                
  155.                 float minTime = float.Parse(dataBits[0], System.Globalization.CultureInfo.InvariantCulture);
  156.                 float maxTime = minTime;
  157.                
  158.                 if (dataBits.Length > 1)
  159.                     maxTime = float.Parse(dataBits[1], System.Globalization.CultureInfo.InvariantCulture);
  160.                
  161.                 StartCoroutine(CueAnimation(animationName, minTime, maxTime));
  162.  
  163.                 loop = true;
  164.             }
  165.             else
  166.             {
  167.                 cueOnComplete = animation.cue.Trim();
  168.             }
  169.         }
  170.  
  171.         if (animation.sequenceCode != null && animation.sequenceCode != "")
  172.         {
  173.             while (true)
  174.             {
  175.                 string[] split = animation.sequenceCode.Split(',');
  176.                 foreach (string data in split)
  177.                 {
  178.                     string[] dataBits = data.Trim().Split(':');
  179.                     float duration = float.Parse(dataBits[1], System.Globalization.CultureInfo.InvariantCulture);
  180.                     dataBits = dataBits[0].Split('-');
  181.  
  182.                     int startFrame = int.Parse(dataBits[0], System.Globalization.CultureInfo.InvariantCulture);
  183.                     int endFrame = startFrame;
  184.  
  185.                     if (dataBits.Length > 1)
  186.                         endFrame = int.Parse(dataBits[1], System.Globalization.CultureInfo.InvariantCulture);
  187.  
  188.                     currentFrame = startFrame;
  189.  
  190.                     Debug.Log ("startFrame: " + startFrame + " endFrame: " + endFrame + " duration: " + duration);
  191.  
  192.                     while (duration > 0f)
  193.                     {
  194.                         while (timer < delay)
  195.                         {
  196.                             duration -= Time.deltaTime;
  197.                             timer += Time.deltaTime;
  198.                             yield return null;
  199.                         }
  200.                         while (timer >= delay)
  201.                         {
  202.                             timer -= delay;
  203.                             currentFrame++;
  204.                             if (currentFrame > endFrame)
  205.                                 currentFrame = startFrame;
  206.                         }
  207.  
  208.                         spriteRenderer.sprite = animation.frames[currentFrame];
  209.                     }
  210.                 }
  211.                 if (cueOnComplete != "")
  212.                     ForcePlay(cueOnComplete, loop);
  213.             }
  214.         }
  215.         else
  216.         {
  217.             while (loop || currentFrame < animation.frames.Length-1)
  218.             {
  219.                 while (timer < delay)
  220.                 {
  221.                     timer += Time.deltaTime;
  222.                     yield return null;
  223.                 }
  224.  
  225.                 while (timer >= delay)
  226.                 {
  227.                     timer -= delay;
  228.                     NextFrame(animation);
  229.                 }
  230.  
  231.                 spriteRenderer.sprite = animation.frames[currentFrame];
  232.             }
  233.             if (cueOnComplete != "")
  234.                 ForcePlay(cueOnComplete, loop);
  235.         }
  236.  
  237.         currentAnimation = null;
  238.         playing = false;
  239.     }
  240.  
  241.     void NextFrame(Animation animation)
  242.     {
  243.         looped = false;
  244.         currentFrame++;
  245.         foreach (AnimationTrigger animationTrigger in animation.triggers)
  246.         {
  247.             if (animationTrigger.frame == currentFrame)
  248.             {
  249.                 gameObject.SendMessageUpwards(animationTrigger.name);
  250.             }
  251.         }
  252.  
  253.         if (currentFrame >= animation.frames.Length)
  254.         {
  255.             if (loop)
  256.                 currentFrame = 0;
  257.             else
  258.                 currentFrame = animation.frames.Length - 1;
  259.         }
  260.     }
  261.  
  262.     public int GetFacing()
  263.     {
  264.         return (int)Mathf.Sign(spriteRenderer.transform.localScale.x);
  265.     }
  266.  
  267.     public void FlipTo(float dir)
  268.     {
  269.         if (dir < 0f)
  270.             spriteRenderer.transform.localScale = new Vector3(-1f, 1f, 1f);
  271.         else
  272.             spriteRenderer.transform.localScale = new Vector3(1f, 1f, 1f);
  273.     }
  274.  
  275.     public void FlipTo(Vector3 position)
  276.     {
  277.         float diff = position.x - transform.position.x;
  278.         if (diff < 0f)
  279.             spriteRenderer.transform.localScale = new Vector3(-1f, 1f, 1f);
  280.         else
  281.             spriteRenderer.transform.localScale = new Vector3(1f, 1f, 1f);
  282.     }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement