//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Saturday, November 11, 2017 //-------------------------------------------------------- using UnityEngine; using System.Collections; using UnityEngine.UI; using System; public class TimerBehaviour : MonoBehaviour { [SerializeField] protected Text m_TimeShow; public Text timeShow { get { return m_TimeShow; } } [SerializeField] Pattern m_Pattern = Pattern.English; protected DateTime endTime; Action onTime; float secondTimer = 0f; public void Begin(int seconds, Action _callBack = null) { endTime = TimeUtility.ServerNow + new TimeSpan(seconds * TimeSpan.TicksPerSecond); onTime = _callBack; if (endTime > TimeUtility.ServerNow) { this.SetActive(true); UpdateTimeShow(); } else { this.SetActive(false); } } public void Stop() { endTime = DateTime.MinValue; onTime = null; this.SetActive(false); } private void LateUpdate() { if (TimeUtility.ServerNow > endTime) { if (onTime != null) { onTime(); onTime = null; } this.SetActive(false); } else { secondTimer += Time.deltaTime; if (secondTimer > 1f) { secondTimer = 0f; UpdateTimeShow(); } } } protected virtual void UpdateTimeShow() { var lastSecond = (float)(endTime - TimeUtility.ServerNow).TotalSeconds; switch (m_Pattern) { case Pattern.English: m_TimeShow.text = TimeUtility.SecondsToHMS(Mathf.RoundToInt(lastSecond)); break; case Pattern.Chinese: m_TimeShow.text = TimeUtility.SecondsToDHMSCHS(Mathf.RoundToInt(lastSecond)); break; } } public enum Pattern { English = 0, Chinese = 1, } }