using UnityEngine; using UnityEngine.UI; public class ImageSequence : MonoBehaviour { [Header("Sprites")] public Sprite[] frames = new Sprite[9]; [Header("Přehrávání")] public float fps = 12f; public bool loop = true; [Header("Tlačítko")] public Button playButton; private Image _image; private int _currentFrame = 0; private float _timer = 0f; private bool _playing = false; void Awake() { _image = GetComponent(); } void Start() { if (frames.Length == 0 || _image == null) return; ShowFrame(0); if (playButton != null) playButton.onClick.AddListener(TogglePlay); } void Update() { if (!_playing || frames.Length == 0) return; _timer += Time.deltaTime; if (_timer >= 1f / fps) { _timer = 0f; NextFrame(); } } void NextFrame() { _currentFrame++; if (_currentFrame >= frames.Length) { if (loop) _currentFrame = 0; else { _currentFrame = frames.Length - 1; Stop(); return; } } ShowFrame(_currentFrame); } void ShowFrame(int index) { if (index < 0 || index >= frames.Length) return; if (frames[index] != null) _image.sprite = frames[index]; } public void Play() { _playing = true; } public void Pause() { _playing = false; } public void Stop() { _playing = false; _currentFrame = 0; ShowFrame(0); } public void TogglePlay() { if (_playing) Pause(); else Play(); } public void GoToFrame(int index) { _currentFrame = Mathf.Clamp(index, 0, frames.Length - 1); ShowFrame(_currentFrame); } }