Timer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public enum ETimerType
  6. {
  7. OneShot,
  8. Repeated,
  9. }
  10. public class Timer
  11. {
  12. //当前这个定时器对象的唯一标识符
  13. public string key;
  14. //超时时间
  15. public float time;
  16. //当前数到的时间
  17. public float curTime;
  18. //定时器的类型
  19. public ETimerType type;
  20. //回调函数
  21. public Action action;
  22. //
  23. public bool isDead;
  24. public Timer(string key, float time, ETimerType type, Action action)
  25. {
  26. curTime = 0;
  27. this.key = key;
  28. this.time = time;
  29. this.type = type;
  30. this.action = action;
  31. isDead = false;
  32. }
  33. }
  34. public class TimerMgr
  35. {
  36. static TimerMgr sInstance = null;
  37. public static TimerMgr Instance
  38. {
  39. get
  40. {
  41. if (sInstance == null)
  42. {
  43. sInstance = new TimerMgr();
  44. }
  45. return sInstance;
  46. }
  47. }
  48. //管理所有的定时器
  49. Dictionary<string, Timer> mAllTimerManagerMap;
  50. TimerMgr()
  51. {
  52. mAllTimerManagerMap = new Dictionary<string, Timer>();
  53. }
  54. //增
  55. public bool AddTimer(Timer timer)
  56. {
  57. if (mAllTimerManagerMap.ContainsKey(timer.key))
  58. {
  59. return false;
  60. }
  61. mAllTimerManagerMap.Add(timer.key, timer);
  62. return true;
  63. }
  64. //删
  65. public bool DeleteTimer(string key)
  66. {
  67. Timer timer = null;
  68. if (mAllTimerManagerMap.TryGetValue(key, out timer) == false)
  69. {
  70. return false;
  71. }
  72. timer.isDead = true;
  73. return true;
  74. }
  75. public void RefreshTimer(string Key)
  76. {
  77. if (mAllTimerManagerMap.ContainsKey(Key))
  78. {
  79. mAllTimerManagerMap[Key].curTime = 0;
  80. }
  81. }
  82. public bool DeleteAllTimer()
  83. {
  84. if (mAllTimerManagerMap.Count == 0) { return false; }
  85. string[] key = new string[mAllTimerManagerMap.Count];
  86. //将字典中的 Key 值从零开始拷贝至 mIds中
  87. mAllTimerManagerMap.Keys.CopyTo(key, 0);
  88. int count = mAllTimerManagerMap.Count;
  89. for (int i = 0; i < count; i++)
  90. {
  91. mAllTimerManagerMap[key[i]].isDead = true;
  92. }
  93. return true;
  94. }
  95. //OneShot
  96. public bool OneShot(string key, float time, Action action)
  97. {
  98. return AddTimer(new Timer(key, time, ETimerType.OneShot, action));
  99. }
  100. //Repeated
  101. public bool Repeated(string key, float time, Action action)
  102. {
  103. return AddTimer(new Timer(key, time, ETimerType.Repeated, action));
  104. }
  105. //运行定时器
  106. public void Update()
  107. {
  108. float dt = Time.deltaTime;
  109. List<string> needDel = new List<string>();
  110. foreach (var kv in mAllTimerManagerMap)
  111. {
  112. if (kv.Value.isDead)
  113. {
  114. needDel.Add(kv.Key);
  115. continue;
  116. }
  117. kv.Value.curTime += dt;
  118. if (kv.Value.curTime >= kv.Value.time)
  119. {
  120. kv.Value.curTime = 0f;
  121. kv.Value.action();
  122. if (kv.Value.type == ETimerType.OneShot)
  123. {
  124. kv.Value.isDead = true;
  125. }
  126. }
  127. if (kv.Value.isDead)
  128. {
  129. needDel.Add(kv.Key);
  130. }
  131. }
  132. for (int i = 0; i < needDel.Count; i++)
  133. {
  134. mAllTimerManagerMap.Remove(needDel[i]);
  135. }
  136. }
  137. }