├── README.md
└── Scripts
├── Class
└── Timer.cs
└── Manager
└── TimerManager.cs
/README.md:
--------------------------------------------------------------------------------
1 | Unity3d-Timer
2 | =============
3 |
4 | Just a time manager , so easy .
5 |
6 | Too simple, so I do not need to explain, just see the code.
7 |
8 | Help yourself.
9 |
10 | How to use see the TimeManager.cs ~
11 |
12 | Yooooooo~~~
13 |
--------------------------------------------------------------------------------
/Scripts/Class/Timer.cs:
--------------------------------------------------------------------------------
1 | //Coded by ZhipingXu xuzhiping7@qq.com
2 | //Too simple, so I do not need to explain, just see the code. Help yourself.
3 |
4 | public class Timer{
5 |
6 | //If the Timer is running
7 | private bool b_Tricking;
8 |
9 | //Current time
10 | private float f_CurTime;
11 |
12 | //Time to reach
13 | private float f_TriggerTime;
14 |
15 | //Use delegate to hold the methods
16 | public delegate void EventHandler();
17 |
18 | //The trigger event list
19 | public event EventHandler tick;
20 |
21 | ///
22 | /// Init
23 | ///
24 | /// Trigger Time
25 | public Timer(float second)
26 | {
27 | f_CurTime = 0.0f;
28 | f_TriggerTime = second;
29 | }
30 |
31 | ///
32 | /// Start Timer
33 | ///
34 | public void Start()
35 | {
36 | b_Tricking = true;
37 | }
38 |
39 | ///
40 | /// Update Time
41 | ///
42 | public void Update(float deltaTime)
43 | {
44 | if (b_Tricking)
45 | {
46 | f_CurTime += deltaTime;
47 |
48 | if (f_CurTime > f_TriggerTime)
49 | {
50 | //b_Tricking must set false before tick() , cause if u want to restart in the tick() , b_Tricking would be reset to fasle .
51 | b_Tricking = false;
52 | tick();
53 | }
54 | }
55 | }
56 |
57 |
58 |
59 | ///
60 | /// Stop the Timer
61 | ///
62 | public void Stop()
63 | {
64 | b_Tricking = false;
65 | }
66 |
67 | ///
68 | /// Continue the Timer
69 | ///
70 | public void Continue()
71 | {
72 | b_Tricking = true;
73 | }
74 |
75 | ///
76 | /// Restart the this Timer
77 | ///
78 | public void Restart()
79 | {
80 | b_Tricking = true;
81 | f_CurTime = 0.0f;
82 | }
83 |
84 | ///
85 | /// Change the trigger time in runtime
86 | ///
87 | /// Trigger Time
88 | public void ResetTriggerTime(float second)
89 | {
90 | f_TriggerTime = second;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/Scripts/Manager/TimerManager.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 | using System.Collections;
3 |
4 | public class TimerManager : MonoBehaviour
5 | {
6 | Timer test;
7 |
8 | // Use this for initialization
9 | void Start () {
10 | test = new Timer(3.0f);
11 | test.tick += Test;
12 | test.tick += Test2;
13 | test.Start();
14 | }
15 |
16 | // Update is called once per frame
17 | void Update () {
18 |
19 | //If u have many timer
20 | //u also can serval frame call one time to save some performance, but the deltaTime u should calculate youself
21 | //like :(u should define lastTime youself-- float)
22 |
23 | /*
24 | if(Time.frameCount%5 == 0)
25 | {
26 | delta = Time.time - lastTime;
27 | test.Update(Time.deltaTime);
28 | lastTime = Time.time;
29 | }
30 | */
31 |
32 | test.Update(Time.deltaTime);
33 | }
34 |
35 | //Some time u may need this to avoid conflict when re-init something , just a tip .
36 | void OnDestory(){
37 | test.tick -= Test;
38 | test.tick -= Test2;
39 | }
40 |
41 | void Test()
42 | {
43 | Debug.Log("1");
44 | }
45 |
46 |
47 | void Test2()
48 | {
49 | Debug.Log("2");
50 | }
51 | }
52 |
--------------------------------------------------------------------------------