yyl
56 分钟以前 fa983b84bcdb05324ea15ee50a76f870c4c3931d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//--------------------------------------------------------
//    [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,
    }
 
}