109 lines
2.9 KiB
C#
109 lines
2.9 KiB
C#
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
using System.Collections;
|
|||
|
using GCGame.Table;
|
|||
|
|
|||
|
public class TimeDownText : MonoBehaviour {
|
|||
|
|
|||
|
public delegate void TimeOverEvent(Hashtable table);
|
|||
|
|
|||
|
public enum TimeFormat
|
|||
|
{
|
|||
|
china,
|
|||
|
english,
|
|||
|
number,
|
|||
|
}
|
|||
|
int sendEventDown = 0; //发送结束事件延时时间
|
|||
|
float m_endTime; //倒计时结束时间点 相对于Time.realtimeSinceStartup
|
|||
|
string m_textStr = string.Empty; //传过来的格式化字符串
|
|||
|
Text m_text;
|
|||
|
TimeFormat m_timeFormat = TimeFormat.china; //显示的时间的格式
|
|||
|
TimeOverEvent m_timeOverFun;
|
|||
|
|
|||
|
public void Stop()
|
|||
|
{
|
|||
|
m_endTime = 0;
|
|||
|
}
|
|||
|
|
|||
|
public void SetTipText(string tip)
|
|||
|
{
|
|||
|
m_textStr = tip;
|
|||
|
}
|
|||
|
|
|||
|
public void Init(string text,float endTime = 0, float timeTotle = 0,int StrDictionaryID = -1, TimeOverEvent OverEvent = null, TimeFormat timeFormat = TimeFormat.china,int EventDown = 4)
|
|||
|
{
|
|||
|
if (gameObject.activeSelf == false)
|
|||
|
return;
|
|||
|
m_text = GetComponent<Text>();
|
|||
|
if (m_text == null)
|
|||
|
return;
|
|||
|
m_text.text = text;
|
|||
|
if (endTime <= 0) {
|
|||
|
Stop ();
|
|||
|
return;
|
|||
|
}
|
|||
|
LastUpdateTime = 0;
|
|||
|
m_endTime = endTime;
|
|||
|
m_timeFormat = timeFormat;
|
|||
|
Tab_StrDictionary pDictionary = TableManager.GetStrDictionaryByID(StrDictionaryID, 0);
|
|||
|
if(pDictionary!=null)
|
|||
|
{
|
|||
|
m_textStr = pDictionary.StrDictionary;
|
|||
|
}
|
|||
|
m_timeOverFun = OverEvent;
|
|||
|
sendEventDown = EventDown;
|
|||
|
}
|
|||
|
|
|||
|
string GetTipStr()
|
|||
|
{
|
|||
|
int leaveTimes = (int)(m_endTime - Time.realtimeSinceStartup);
|
|||
|
if (leaveTimes <= 0)
|
|||
|
leaveTimes = 0;
|
|||
|
switch(m_timeFormat)
|
|||
|
{
|
|||
|
case TimeFormat.china:
|
|||
|
return GCGame.Utils.GetTimeChinaStr(leaveTimes);
|
|||
|
case TimeFormat.english:
|
|||
|
return GCGame.Utils.GetTimeDiffFormatString(leaveTimes);
|
|||
|
case TimeFormat.number:
|
|||
|
return leaveTimes.ToString();
|
|||
|
}
|
|||
|
return "";
|
|||
|
}
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
UpdateTimeDown();
|
|||
|
}
|
|||
|
|
|||
|
float LastUpdateTime = 0;
|
|||
|
void UpdateTimeDown()
|
|||
|
{
|
|||
|
if (m_text == null || m_endTime<=0 || Time.realtimeSinceStartup - LastUpdateTime < 1)
|
|||
|
return;
|
|||
|
LastUpdateTime = Time.realtimeSinceStartup;
|
|||
|
if (m_endTime < Time.realtimeSinceStartup)
|
|||
|
{
|
|||
|
if (sendEventDown <= 0)
|
|||
|
{
|
|||
|
if (m_timeOverFun != null)
|
|||
|
m_timeOverFun.Invoke(null);
|
|||
|
return;
|
|||
|
}
|
|||
|
sendEventDown--;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
string timeStr = GetTipStr();
|
|||
|
if (string.IsNullOrEmpty(m_textStr))
|
|||
|
{
|
|||
|
m_text.text = timeStr;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
m_text.text = string.Format(m_textStr, timeStr);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|