GifImage.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.UI;
  6. public class GifImage : MonoBehaviour
  7. {
  8. [SerializeField] Sprite[] _pics;
  9. int _picsPerSecond = 10;
  10. Image _image;
  11. float _time = 0.0f;
  12. int _currentFrame = 0;
  13. float _frameDelay = 0.0f;
  14. void Start()
  15. {
  16. _image = GetComponent<Image>();
  17. if (_picsPerSecond > 0)
  18. {
  19. _frameDelay = 1f / _picsPerSecond;
  20. }
  21. }
  22. void Update()
  23. {
  24. if (_pics == null)
  25. {
  26. return;
  27. }
  28. if (_pics.Length == 0)
  29. {
  30. return;
  31. }
  32. if (_image == null)
  33. {
  34. return;
  35. }
  36. if (_frameDelay <= 0)
  37. {
  38. return;
  39. }
  40. _time += Time.deltaTime;
  41. if (_time >= _frameDelay)
  42. {
  43. _currentFrame = (_currentFrame + 1) % _pics.Length;
  44. _time = 0.0f;
  45. _image.sprite = _pics[_currentFrame];
  46. }
  47. }
  48. }