yyl
2025-06-13 eb1efcaa9be0e2340fc49b38dab8df18e36526c9
Main/Component/UI/Common/TimerBehaviour.cs
@@ -7,90 +7,87 @@
using UnityEngine.UI;
using System;
namespace vnxbqy.UI
public class TimerBehaviour : MonoBehaviour
{
    [SerializeField] protected Text m_TimeShow;
    public Text timeShow { get { return m_TimeShow; } }
    public class TimerBehaviour : MonoBehaviour
    [SerializeField] Pattern m_Pattern = Pattern.English;
    protected DateTime endTime;
    Action onTime;
    float secondTimer = 0f;
    public void Begin(int seconds, Action _callBack = null)
    {
        [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)
        {
            endTime = TimeUtility.ServerNow + new TimeSpan(seconds * TimeSpan.TicksPerSecond);
            onTime = _callBack;
            if (endTime > TimeUtility.ServerNow)
            {
                this.SetActive(true);
                UpdateTimeShow();
            }
            else
            {
                this.SetActive(false);
            }
            this.SetActive(true);
            UpdateTimeShow();
        }
        public void Stop()
        else
        {
            endTime = DateTime.MinValue;
            onTime = null;
            this.SetActive(false);
        }
    }
        private void LateUpdate()
    public void Stop()
    {
        endTime = DateTime.MinValue;
        onTime = null;
        this.SetActive(false);
    }
    private void LateUpdate()
    {
        if (TimeUtility.ServerNow > endTime)
        {
            if (TimeUtility.ServerNow > endTime)
            if (onTime != null)
            {
                if (onTime != null)
                {
                    onTime();
                    onTime = null;
                }
                this.SetActive(false);
                onTime();
                onTime = null;
            }
            else
            this.SetActive(false);
        }
        else
        {
            secondTimer += Time.deltaTime;
            if (secondTimer > 1f)
            {
                secondTimer += Time.deltaTime;
                if (secondTimer > 1f)
                {
                    secondTimer = 0f;
                    UpdateTimeShow();
                }
                secondTimer = 0f;
                UpdateTimeShow();
            }
        }
    }
        protected virtual void UpdateTimeShow()
    protected virtual void UpdateTimeShow()
    {
        var lastSecond = (float)(endTime - TimeUtility.ServerNow).TotalSeconds;
        switch (m_Pattern)
        {
            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;
            }
            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,
        }
    public enum Pattern
    {
        English = 0,
        Chinese = 1,
    }
}