SpriteAnimation.cs 533 B

12345678910111213141516171819202122232425262728293031
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class SpriteAnimation : MonoBehaviour
  6. {
  7. public float speed = 0.5f;
  8. public List<Sprite> sprites;
  9. public Image img;
  10. private int index = 0;
  11. private float timer = 0;
  12. void Update()
  13. {
  14. index %= sprites.Count;
  15. img.sprite = sprites[index];
  16. timer += Time.deltaTime * 0.1f;
  17. if(timer >= speed)
  18. {
  19. index++;
  20. timer = 0;
  21. }
  22. }
  23. }