SHARE
TWEET

simple sprite sheet animator

a guest Oct 1st, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode()]
  4. public class BakedAnimator : MonoBehaviour {
  5.    
  6.     public Sprite[] m_Sprites;
  7.     public int FramesPerSecond = 8;
  8.     public float Speed = 1.0f;
  9.  
  10.     SpriteRenderer spriteRenderer;
  11.  
  12.    
  13.     void OnEnable()
  14.     {
  15.         spriteRenderer = GetComponent<SpriteRenderer>();
  16.     }
  17.  
  18.     void Update() {
  19.         if (!spriteRenderer || m_Sprites.Length == 0)
  20.             return;
  21.        
  22.         float t = (float)Time.time * (int)FramesPerSecond * Speed;
  23.         var s = m_Sprites[(int)Mathf.Repeat(t, m_Sprites.Length)];
  24.         spriteRenderer.sprite = s;
  25.     }
  26.  
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top