Advertisement
lunoland

Simple Sprite Animator for Unity

Sep 21st, 2019
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. [CreateAssetMenu(menuName = "Sprites/Sprite Animation", fileName = "NewSpriteAnimation")]
  2. public class SpriteAnimation : ScriptableObject {
  3.  
  4.     [System.Serializable]
  5.     public struct Frame {
  6.         public Sprite sprite;
  7.         public int milliseconds;
  8.     }
  9.  
  10.     public bool loop = false;
  11.     public int cancellableAt = 0;
  12.     public float speed = 1f;
  13.     public Frame[] frames;
  14. }
  15.  
  16.  
  17. public class SpriteAnimator : MonoBehaviour {
  18.  
  19.     [System.Serializable]
  20.     public struct Request {
  21.         public SpriteAnimation animation;
  22.         public float speed;
  23.         public bool loop;
  24.         public int start;
  25.         public int end;
  26.     }
  27.        
  28.  
  29.     public SpriteAnimation defaultAnimation;
  30.     public bool playOnAwake;
  31.     public bool ignoreTimescale;
  32.     public bool pause;
  33.  
  34.     [ReadOnly] public int frame;
  35.     [ReadOnly] public Request currentRequest;
  36.     [ReadOnly] public Request nextRequest;
  37.  
  38.  
  39.     SpriteRenderer spriteRenderer;
  40.     float elapsed = 0f;
  41.     float duration;
  42.  
  43.  
  44.     void Awake() {
  45.         spriteRenderer = GetComponent<SpriteRenderer>();
  46.     }
  47.  
  48.     void OnEnable() {
  49.         if (playOnAwake && defaultAnimation != null) { PlayAnimation(defaultAnimation, start: Random.Range(0, defaultAnimation.frames.Length)); }
  50.     }
  51.  
  52.     void Update() {
  53.         if (currentRequest.animation == null || pause) { return; }
  54.  
  55.         if (nextRequest.animation != null && frame >= currentRequest.animation.cancellableAt) {
  56.             currentRequest = nextRequest;
  57.             nextRequest.animation = null;
  58.             ChangeFrame(currentRequest.start);
  59.         }
  60.  
  61.         elapsed += ignoreTimescale ? Time.unscaledDeltaTime : Time.deltaTime;
  62.  
  63.         if (elapsed >= duration) {
  64.             frame++;
  65.  
  66.             if (frame > currentRequest.end) {
  67.                 if (currentRequest.loop || currentRequest.animation.loop) {
  68.                     frame = currentRequest.start;
  69.                 }
  70.                 else {
  71.                     PlayAnimation(defaultAnimation);
  72.                     return;
  73.                 }
  74.             }
  75.  
  76.             ChangeFrame(frame);
  77.         }
  78.     }
  79.  
  80.  
  81.     public void ChangeFrame(int value) {
  82.         frame = value
  83.         elapsed = 0f;
  84.         duration = currentRequest.animation.frames[frame].milliseconds * 0.001f * (1f / currentRequest.animation.speed) * (1f / currentRequest.speed);
  85.         spriteRenderer.sprite = currentRequest.animation.frames[frame].sprite;
  86.     }
  87.  
  88.     public void PlayAnimation(SpriteAnimation animation, float speed = 1f, bool loop = false, int start = -1, int end = -1, bool force = false) {
  89.         if (animation == null) {
  90.             Stop();
  91.             return;
  92.         }
  93.  
  94.         start = start < 0 ? 0 : start;
  95.         end = end < 0 ? animation.frames.Length - 1 : end;
  96.  
  97.         if (currentRequest.animation == null || frame >= currentRequest.animation.cancellableAt || force) {
  98.             nextRequest.animation = null;
  99.             currentRequest.animation = animation;
  100.             currentRequest.speed = speed;
  101.             currentRequest.loop = loop;
  102.             currentRequest.start = start;
  103.             currentRequest.end = end;
  104.             ChangeFrame(start);
  105.         }
  106.         else {
  107.             nextRequest.animation = animation;
  108.             nextRequest.speed = speed;
  109.             nextRequest.loop = loop;
  110.             nextRequest.start = start;
  111.             nextRequest.end = end;
  112.         }
  113.     }
  114.  
  115.     public void Stop() {
  116.         currentRequest.animation = null;
  117.         nextRequest.animation = null;
  118.         spriteRenderer.sprite = null;
  119.         frame = -1;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement