75 lines
1.9 KiB
C#
75 lines
1.9 KiB
C#
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<TMP_Text>();
|
|
// 注册事件
|
|
EnumEventSystem.Global.Register(GameLifecycleEvent.SessionStarted, TimerStart);
|
|
EnumEventSystem.Global.Register(GameLifecycleEvent.SessionEnded, 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<EndPanel>();
|
|
}
|
|
}
|
|
}
|