239 lines
5.4 KiB
C#
239 lines
5.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using QFramework;
|
|
using System;
|
|
using System.Threading;
|
|
using TMPro;
|
|
|
|
namespace QFramework.Example
|
|
{
|
|
//自定义倒计时脚本
|
|
public class UITimeOutData : UIPanelData
|
|
{
|
|
public string totalTimeStr;
|
|
public string varName;
|
|
public string varValue;
|
|
|
|
}
|
|
|
|
public partial class UITimeOut : UIPanel
|
|
{
|
|
|
|
//[Tooltip("倒计时总时长(秒)")]
|
|
private float totalTime ; // 默认3分钟
|
|
|
|
//[Tooltip("是否自动开始倒计时")]
|
|
private bool autoStart = true;
|
|
|
|
// [Tooltip("是否循环倒计时")]
|
|
private bool loop = false;
|
|
|
|
|
|
//[Tooltip("倒计时格式 (mm:ss)")]
|
|
private string timeFormat = "mm:ss";
|
|
|
|
|
|
//[Tooltip("正常状态文本颜色")]
|
|
public Color normalColor = Color.white;
|
|
|
|
//[Tooltip("警告状态文本颜色(剩余10秒内)")]
|
|
public Color warningColor = Color.red;
|
|
|
|
//[Tooltip("警告阈值(秒)")]
|
|
public float warningThreshold = 10f;
|
|
|
|
// 内部状态
|
|
private float currentTime;
|
|
private bool isCountingDown = false;
|
|
|
|
public string varName;
|
|
public float varValue;
|
|
|
|
protected override void OnInit(IUIData uiData = null)
|
|
{
|
|
mData = uiData as UITimeOutData ?? new UITimeOutData();
|
|
|
|
|
|
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject);
|
|
|
|
// 初始化文本颜色
|
|
TimeOutText.color = normalColor;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
protected override void OnOpen(IUIData uiData = null)
|
|
{
|
|
|
|
if (mData.totalTimeStr != null)
|
|
{
|
|
totalTime = mData.totalTimeStr.ToFloat();
|
|
|
|
}
|
|
|
|
if (mData.varName != null)
|
|
{
|
|
varName = mData.varName;
|
|
|
|
}
|
|
if (mData.varValue != null)
|
|
{
|
|
|
|
Debug.Log("!!!!!!!!!!!"+mData.varValue.ToFloat()+"!!!!!!!!!!");
|
|
varValue = mData.varValue.ToFloat();
|
|
|
|
}
|
|
|
|
Debug.Log("打开时间???");
|
|
ResetTimer();
|
|
|
|
if (autoStart)
|
|
{
|
|
StartCountdown();
|
|
}
|
|
}
|
|
|
|
protected override void OnShow()
|
|
{
|
|
|
|
}
|
|
|
|
// 重置计时器
|
|
public void ResetTimer()
|
|
{
|
|
currentTime = totalTime;
|
|
UpdateTimeDisplay();
|
|
|
|
// 重置文本颜色
|
|
TimeOutText.color = normalColor;
|
|
}
|
|
|
|
// 开始倒计时
|
|
public void StartCountdown()
|
|
{
|
|
if (isCountingDown) return;
|
|
|
|
isCountingDown = true;
|
|
UpdateTimeDisplay(); // 立即更新一次显示
|
|
}
|
|
|
|
// 暂停倒计时
|
|
public void PauseCountdown()
|
|
{
|
|
isCountingDown = false;
|
|
}
|
|
|
|
// 恢复倒计时
|
|
public void ResumeCountdown()
|
|
{
|
|
if (currentTime <= 0) return; // 已完成则不恢复
|
|
|
|
isCountingDown = true;
|
|
}
|
|
|
|
// 停止倒计时并重置
|
|
public void StopCountdown()
|
|
{
|
|
isCountingDown = false;
|
|
ResetTimer();
|
|
}
|
|
// 更新倒计时
|
|
void LateUpdate()
|
|
{
|
|
if (!isCountingDown || currentTime <= 0)
|
|
return;
|
|
|
|
currentTime -= Time.deltaTime;
|
|
|
|
// 确保时间不会变为负数
|
|
if (currentTime < 0)
|
|
currentTime = 0;
|
|
|
|
UpdateTimeDisplay();
|
|
|
|
// 检查是否需要更改文本颜色
|
|
if (currentTime <= warningThreshold)
|
|
{
|
|
TimeOutText.color = warningColor;
|
|
}
|
|
else if (TimeOutText.color != normalColor)
|
|
{
|
|
TimeOutText.color = normalColor;
|
|
}
|
|
|
|
// 通知倒计时更新
|
|
|
|
|
|
// 检查倒计时是否完成
|
|
if (currentTime <= 0)
|
|
{
|
|
OnCountdownComplete();
|
|
}
|
|
}
|
|
|
|
// 更新时间显示
|
|
void UpdateTimeDisplay()
|
|
{
|
|
TimeSpan timeSpan = TimeSpan.FromSeconds(currentTime);
|
|
|
|
switch (timeFormat.ToLower())
|
|
{
|
|
case "hh:mm:ss":
|
|
TimeOutText.text = timeSpan.ToString(@"hh\:mm\:ss");
|
|
break;
|
|
case "mm:ss":
|
|
TimeOutText.text = timeSpan.ToString(@"mm\:ss");
|
|
break;
|
|
case "ss":
|
|
TimeOutText.text = Mathf.CeilToInt(currentTime).ToString();
|
|
break;
|
|
default:
|
|
TimeOutText.text = timeSpan.ToString(@"mm\:ss");
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 倒计时完成处理
|
|
void OnCountdownComplete()
|
|
{
|
|
isCountingDown = false;
|
|
|
|
// 触发完成事件
|
|
VarController.Instance.Set(varName, varValue);
|
|
|
|
// 检查是否循环
|
|
if (loop)
|
|
{
|
|
ResetTimer();
|
|
StartCountdown();
|
|
}
|
|
}
|
|
|
|
// 获取当前剩余时间
|
|
public float GetRemainingTime()
|
|
{
|
|
return currentTime;
|
|
}
|
|
|
|
// 设置新的倒计时时间
|
|
public void SetTotalTime(float newTime)
|
|
{
|
|
totalTime = Mathf.Max(0, newTime);
|
|
ResetTimer();
|
|
}
|
|
|
|
protected override void OnHide()
|
|
{
|
|
|
|
|
|
StopCountdown();
|
|
}
|
|
|
|
protected override void OnClose()
|
|
{
|
|
}
|
|
}
|
|
}
|