1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.Events;
5
6 public class ZSTimerManager: SingletonGeneric<ZSTimerManager>
7 {
8 private List<ZSTimerTask> timers = new List<ZSTimerTask>();
9 public static ZSTimerManagerStub stub = new GameObject("ZSTimerManagerStub").AddComponent<ZSTimerManagerStub>();
10
11 public ZSTimerTask Create(int total, int interval, int ID)
12 {
13 var t = timers.Find(pt => pt.ID == ID);
14 if (t != null)
15 {
16 Debug.LogErrorFormat("the timer what ID = {0} already exist.", ID);
17 return null;
18 }
19 ZSTimerTask task = new ZSTimerTask(ID);
20 timers.Add(task);
21
22 return task;
23 }
24
25 public ZSTimerTask GetTimer(int ID)
26 {
27 return timers.Find(task => task.ID == ID);
28 }
29
30 public void RemoveTimer(int ID)
31 {
32 var timer = GetTimer(ID);
33 if (timer == null) Debug.LogErrorFormat("ID = {0} are not found.", ID);
34 timers.Remove(GetTimer(ID));
35 }
36 }
37
38 public class ZSTimerTask
39 {
40 /// <summary>
41 /// 总时间
42 /// </summary>
43 private int _total;
44 /// <summary>
45 /// 间隔时间
46 /// </summary>
47 private int _interval;
48 /// <summary>
49 /// ID
50 /// </summary>
51 public int ID { get; set; }
52
53 private Coroutine driver;
54 /// <summary>
55 /// 剩余时间
56 /// </summary>
57 public int Therest { get; private set; }
58
59 public UnityEvent OnStart = new UnityEvent();
60 public UnityEvent OnComplete = new UnityEvent();
61 public UnityEvent OnInterval = new UnityEvent();
62
63 public ZSTimerTask(int id)
64 {
65 this.ID = id;
66 }
67
68 public virtual void Start(int total, int interval)
69 {
70 _total = total;
71 Therest = _total;
72 _interval = interval;
73 OnStart.Invoke();
74 driver = ZSTimerManager.stub.StartCoroutine(TickRoutine());
75 }
76
77 public virtual void Stop()
78 {
79 if (driver != null) ZSTimerManager.stub.StopCoroutine(driver);
80 }
81
82 public virtual void Reset()
83 {
84 Therest = _total;
85 if (driver != null) ZSTimerManager.stub.StopCoroutine(TickRoutine());
86 driver = ZSTimerManager.stub.StartCoroutine(TickRoutine());
87 }
88
89 /// <summary>
90 /// DRIVER
91 /// </summary>
92 protected virtual IEnumerator TickRoutine()
93 {
94 do
95 {
96 OnInterval.Invoke();
97 yield return new WaitForSeconds(_interval);
98 Therest -= _interval;
99 } while (Therest >= 0);
100 OnComplete.Invoke();
101 }
102
103 public virtual void Close()
104 {
105 OnStart.RemoveAllListeners();
106 OnComplete.RemoveAllListeners();
107 OnInterval.RemoveAllListeners();
108 if (driver != null) ZSTimerManager.stub.StopCoroutine(TickRoutine());
109 }
110 }
111
112 public sealed class ZSTimerManagerStub : MonoBehaviour { }
Smart Timers Manager
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)