using AibisDream.Framework; using AibisDream.UI; using UnityEngine; using TMPro; namespace AibisDream { public class DemoTimer : MonoBehaviour { public float totalTimeInSeconds = 1200f; private TMP_Text _timerText; private float _currentTime; private bool _isRunning; private void Awake() { _timerText = GetComponent(); // 注册事件 EnumEventSystem.Global.Register(GameLoopEnum.GameStart, TimerStart); EnumEventSystem.Global.Register(GameLoopEnum.GameQuit, TimerStop); } private void TimerStart() { _currentTime = totalTimeInSeconds; _isRunning = true; _timerText.enabled = true; } private void TimerStop() { _currentTime = 0; _isRunning = false; _timerText.enabled = false; } private void TimerPause() { _isRunning = false; } private void Update() { if (_isRunning) { _currentTime -= Time.deltaTime; if (_currentTime <= 0) { TimerPause(); ForceReturnToMainMenu(); } else { UpdateTimerDisplay(); } } } private void UpdateTimerDisplay() { int hours = Mathf.FloorToInt(_currentTime / 3600); int minutes = Mathf.FloorToInt(_currentTime % 3600 / 60); int seconds = Mathf.FloorToInt(_currentTime % 60); _timerText.text = $"{hours:D2}:{minutes:D2}:{seconds:D2}"; } private void ForceReturnToMainMenu() { // 打开EndPanel UIManager.Instance.ShowPanel(); } } }