103 lines
2.5 KiB
C#
103 lines
2.5 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using UnityEngine.UI;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine.Networking;
|
|
using Utility;
|
|
using System;
|
|
using UnityEngine.Events;
|
|
|
|
public class Timers : MonoBehaviour
|
|
{
|
|
public bool isRun = false;
|
|
public float limitTime = 0;
|
|
//public float countTime = 0;
|
|
public Text guiTimer; // 用于显示计时器的 Text 组件
|
|
public UnityEvent<string> timeOver;
|
|
|
|
// Start 方法在脚本激活时运行,用于初始化计时器
|
|
public void SetTime(int _time)
|
|
{
|
|
//StartCoroutine(LoadJSON(Application.streamingAssetsPath + "/Timer.config"));
|
|
limitTime = _time * 60;
|
|
guiTimer.text = "剩余时间:" + StringHelper.GetCountDownStr((int)limitTime);
|
|
}
|
|
|
|
public string GetCurrentTime()
|
|
{
|
|
EndTimer();
|
|
return limitTime.ToString("0");
|
|
}
|
|
|
|
|
|
// 运行计时器
|
|
public void RunTimer()
|
|
{
|
|
isRun = true;
|
|
}
|
|
|
|
// 暂停或恢复计时器
|
|
public void PauseTimer()
|
|
{
|
|
isRun = false;
|
|
}
|
|
|
|
// 结束计时器
|
|
public void EndTimer()
|
|
{
|
|
isRun = false;
|
|
}
|
|
|
|
// Update 方法在每一帧运行,用于更新计时器的状态和显示
|
|
private void Update()
|
|
{
|
|
if (isRun)
|
|
Timer();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计时
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private void Timer()
|
|
{
|
|
guiTimer.text = StringHelper.GetCountDownStr((int)limitTime);
|
|
if (limitTime >= 0)
|
|
{
|
|
limitTime -= Time.deltaTime;
|
|
guiTimer.text = "剩余时间:" + StringHelper.GetCountDownStr((int)limitTime);
|
|
}
|
|
else
|
|
{
|
|
//Debug.Log("计时结束");
|
|
limitTime = 0;
|
|
guiTimer.text = "剩余时间:" + StringHelper.GetCountDownStr((int)limitTime);
|
|
timeOver?.Invoke(limitTime.ToString());
|
|
isRun = false;
|
|
}
|
|
|
|
}
|
|
|
|
//IEnumerator LoadJSON(string fileName)
|
|
//{
|
|
// string sPath = fileName;
|
|
// using (UnityWebRequest oReq = UnityWebRequest.Get(sPath))
|
|
// {
|
|
// yield return oReq.SendWebRequest();
|
|
// if (!string.IsNullOrEmpty(oReq.error))
|
|
// {
|
|
// Debug.Log("获取配置信息失败!");
|
|
// yield break;
|
|
// }
|
|
// string json = oReq.downloadHandler.text;
|
|
// TimerSer settings = JsonConvert.DeserializeObject<TimerSer>(json);
|
|
// limitTime = settings.Timer;
|
|
// //Debug.Log(limitTime);
|
|
// }
|
|
//}
|
|
}
|
|
public class TimerSer
|
|
{
|
|
public float Timer;
|
|
}
|