Advertisement
gameDevTeacher

CustomAnimator

Apr 26th, 2025
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | Gaming | 0 0
  1. public class ImageFlipAnimation : MonoBehaviour
  2.     {
  3.         [SerializeField] Sprite[] sprites;
  4.         [SerializeField] Image image;
  5.  
  6.         [SerializeField] float fps = 10;
  7.  
  8.         public void Play()
  9.         {
  10.             Stop();
  11.             StartCoroutine(AnimSequence());
  12.         }
  13.  
  14.         public void Stop()
  15.         {
  16.             StopAllCoroutines();
  17.             ShowFrame(0);
  18.         }
  19.  
  20.         IEnumerator AnimSequence()
  21.         {
  22.             var delay = new WaitForSeconds(1 / fps);
  23.             int index = 0;
  24.             while(true)
  25.             {
  26.                 if (index >= sprites.Length) index = 0;
  27.                 ShowFrame(index);
  28.                 index++;
  29.                 yield return delay;
  30.             }
  31.         }
  32.  
  33.         void ShowFrame(int index)
  34.         {
  35.             image.sprite = sprites[index];
  36.         }
  37.     }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement