240 lines
5.3 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System;
using System.Threading;
using TMPro;
namespace QFramework.Example
{
//<2F>Զ<EFBFBD><D4B6><EFBFBD><E5B5B9>ʱ<EFBFBD>ű<EFBFBD>
public class UITimeOutData : UIPanelData
{
public string totalTimeStr;
public string varName;
public string varValue;
}
public partial class UITimeOut : UIPanel
{
//[Tooltip("<22><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʱ<EFBFBD><CAB1>(<28><>)")]
private float totalTime ; // Ĭ<><C4AC>3<EFBFBD><33><EFBFBD><EFBFBD>
//[Tooltip("<22>Ƿ<EFBFBD><C7B7>Զ<EFBFBD><D4B6><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ")]
private bool autoStart = true;
// [Tooltip("<22>Ƿ<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ")]
private bool loop = false;
//[Tooltip("<22><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʽ (mm:ss)")]
private string timeFormat = "mm:ss";
//[Tooltip("<22><><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1><EFBFBD>ɫ")]
public Color normalColor = Color.white;
//[Tooltip("<22><><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD><C4B1><EFBFBD>ɫ(ʣ<><CAA3>10<31><30><EFBFBD><EFBFBD>)")]
public Color warningColor = Color.red;
//[Tooltip("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ(<28><>)")]
public float warningThreshold = 10f;
// <20>ڲ<EFBFBD>״̬
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);
// <20><>ʼ<EFBFBD><CABC><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ɫ
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("<22><><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><E4A3BF><EFBFBD><EFBFBD>");
ResetTimer();
if (autoStart)
{
StartCountdown();
}
}
protected override void OnShow()
{
}
// <20><><EFBFBD>ü<EFBFBD>ʱ<EFBFBD><CAB1>
public void ResetTimer()
{
currentTime = totalTime;
UpdateTimeDisplay();
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ɫ
TimeOutText.color = normalColor;
}
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ
public void StartCountdown()
{
if (isCountingDown) return;
isCountingDown = true;
UpdateTimeDisplay(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʾ
}
// <20><>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD>ʱ
public void PauseCountdown()
{
isCountingDown = false;
}
// <20>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ʱ
public void ResumeCountdown()
{
if (currentTime <= 0) return; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򲻻ָ<F2B2BBBB>
isCountingDown = true;
}
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public void StopCountdown()
{
isCountingDown = false;
ResetTimer();
}
// <20><><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ʱ
void Update()
{
if (!isCountingDown || currentTime <= 0)
return;
currentTime -= Time.deltaTime;
// ȷ<><C8B7>ʱ<EFBFBD><EFBFBD><E4B2BB><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
if (currentTime < 0)
currentTime = 0;
UpdateTimeDisplay();
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ɫ
if (currentTime <= warningThreshold)
{
TimeOutText.color = warningColor;
}
else if (TimeOutText.color != normalColor)
{
TimeOutText.color = normalColor;
}
// ֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><E9B5B9>ʱ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if (currentTime <= 0)
{
OnCountdownComplete();
}
}
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʾ
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;
}
}
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ɴ<EFBFBD><C9B4><EFBFBD>
void OnCountdownComplete()
{
isCountingDown = false;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
VarController.Instance.Set(varName, varValue);
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ѭ<EFBFBD><D1AD>
if (loop)
{
ResetTimer();
StartCountdown();
}
}
// <20><>ȡ<EFBFBD><C8A1>ǰʣ<C7B0><CAA3>ʱ<EFBFBD><CAB1>
public float GetRemainingTime()
{
return currentTime;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µĵ<C2B5><C4B5><EFBFBD>ʱʱ<CAB1><CAB1>
public void SetTotalTime(float newTime)
{
totalTime = Mathf.Max(0, newTime);
ResetTimer();
}
protected override void OnHide()
{
StopCountdown();
}
protected override void OnClose()
{
}
}
}