using TMPro; using UnityEngine; using UnityEngine.Events; public class Timer : MonoBehaviour { [SerializeField] private float restTime; private bool processing = false; public TextMeshProUGUI TimeLabel; private bool recentlyPlayed = false; private float lastSignalTime; public UnityEvent OnTimerEnded; private void Start() => lastSignalTime = Time.time; void Update() { if (processing) { UiDraw(); restTime -= Time.deltaTime; if (restTime <= 0) { restTime = 0; StopTimer(); OnTimerEnded?.Invoke(); } int rest = (int)restTime; if (!recentlyPlayed && (rest % 60 == 0 || rest == 30 || rest < 11)) { SoundManager.instance.playTimer(); recentlyPlayed = true; lastSignalTime = Time.time; } if (recentlyPlayed) { if (Time.time - lastSignalTime >= 1f) { recentlyPlayed = false; } } } } public void SetupTimer(float seconds) { restTime = seconds; UiDraw(); } public void StartTimer() { processing = true; } public void StopTimer() { processing = false; } void UiDraw() { string minutes = ((int)(restTime / 60)).ToString(); string seconds = ((int)(restTime % 60)).ToString(); if (minutes.Length < 2) minutes = "0" + minutes; if (seconds.Length < 2) seconds = "0" + seconds; string result = minutes + ":" + seconds; TimeLabel.text = result; } }