Advertisement
GoodNoodle

SpriteAnimator.cs

Aug 16th, 2022
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SpriteAnimator
  6. {
  7.     SpriteRenderer spriteRenderer;
  8.     List<Sprite> frames;
  9.     float frameRate;
  10.  
  11.     int currentFrame;
  12.     float timer;
  13.  
  14.     public SpriteAnimator(List<Sprite> frames, SpriteRenderer spriteRenderer, float frameRate=0.16f)
  15.     {
  16.         this.frames = frames;
  17.         this.spriteRenderer = spriteRenderer;
  18.         this.frameRate = frameRate;
  19.     }
  20.  
  21.     public void Start()
  22.     {
  23.         currentFrame = 0;
  24.         timer = 0f;
  25.         spriteRenderer.sprite = frames[0];
  26.     }
  27.  
  28.     public void HandleUpdate()
  29.     {
  30.         timer += Time.deltaTime;
  31.         if (timer > frameRate)
  32.         {
  33.             currentFrame = (currentFrame + 1) % frames.Count;
  34.             spriteRenderer.sprite = frames[currentFrame];
  35.             timer -= frameRate;
  36.         }
  37.     }
  38.  
  39.     public List<Sprite> Frames {
  40.         get { return frames; }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement