计时器是很常用的功能,本文主要做计时器类的封装,优化多次调用效率。
上代码:
TimerEvent
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 |
using UnityEngine; using System.Collections; using System; public class TimerEvent { public string m_key = ""; /// <summary> /// 重复调用次数,-1代表一直调用 /// </summary> public int m_repeatCount = 0; public int m_currentRepeat = 0; /// <summary> /// 是否忽略时间缩放 /// </summary> public bool m_isIgnoreTimeScale = false; public TimerCallBack m_callBack; public object[] m_objs; public float m_interval; public float m_currentTimer = 0; public bool m_isDone = false; public void Update() { if (m_isIgnoreTimeScale) { m_currentTimer += Time.unscaledDeltaTime; } else { m_currentTimer += Time.deltaTime; } if (m_currentTimer >= m_interval) { m_isDone = true; } } public void CompleteTimer() { CallBackTimer(); if (m_repeatCount > 0) { m_currentRepeat++; } if (m_currentRepeat != m_repeatCount) { m_isDone = false; m_currentTimer = 0; } } public void CallBackTimer() { if (m_callBack != null) { try { m_callBack(m_objs); } catch (Exception e) { Debug.LogError(e.ToString()); } } } public void ResetTimer() { m_currentTimer = 0; m_currentRepeat = 0; } } public delegate void TimerCallBack(params object[] objs); |
Timer
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
using UnityEngine; using System.Collections; using System.Collections.Generic; public class Timer { public static List<TimerEvent> m_timers = new List<TimerEvent>(); public static void Init() { EventMgr.AddListener(EventType.Update, Update); } static void Update() { for (int i = 0; i < m_timers.Count; i++) { TimerEvent e = m_timers[i]; e.Update(); if (e.m_isDone) { e.CompleteTimer(); if (e.m_isDone) { m_timers.Remove(e); } } } } public static bool GetIsExistTimer(string key) { for (int i = 0; i < m_timers.Count; i++) { var e = m_timers[i]; if (e.m_key == key) { return true; } } return false; } public static TimerEvent GetTimer(string key) { for (int i = 0; i < m_timers.Count; i++) { var e = m_timers[i]; if (e.m_key == key) { return e; } } return null; } /// <summary> /// 延迟调用 /// </summary> /// <param name="interval">开始延时</param> /// <param name="callBack">回调函数</param> /// <param name="objs">回调函数的参数</param> /// <returns></returns> public static TimerEvent Register(float interval, TimerCallBack callBack, params object[] objs) { return AddTimer(null, interval, 0, false, callBack, objs); } /// <summary> /// 间隔一定时间重复调用 /// </summary> /// <param name="interval">开始延时</param> /// <param name="repeatCount">重复调用的次数</param> /// <param name="callBack">回调函数</param> /// <param name="objs">回调函数的参数</param> /// <returns></returns> public static TimerEvent Register(float interval, int repeatCount, TimerCallBack callBack, params object[] objs) { return AddTimer(null, interval, repeatCount, false, callBack, objs); } /// <summary> /// 延迟调用 忽略TimeScale /// </summary> /// <param name="interval">开始延时</param> /// <param name="isIgnoreTimeScale">是否忽略时间缩放</param> /// <param name="callBack">回调函数</param> /// <param name="objs">回调函数的参数</param> /// <returns></returns> public static TimerEvent Register(float interval, bool isIgnoreTimeScale, TimerCallBack callBack, params object[] objs) { return AddTimer(null, interval, 0, isIgnoreTimeScale, callBack, objs); } /// <summary> /// 延迟调用 /// </summary> /// <param name="key">Timer的名字</param> /// <param name="interval">开始延时</param> /// <param name="callBack">回调函数</param> /// <param name="objs">回调函数的参数</param> /// <returns></returns> public static TimerEvent Register(string key, float interval, TimerCallBack callBack, params object[] objs) { return AddTimer(key, interval, 0, false, callBack, objs); } /// <summary> /// 间隔一定时间重复调用 /// </summary> /// <param name="key">Timer的名字</param> /// <param name="interval">间隔时间</param> /// <param name="repeatCount">重复调用的次数</param> /// <param name="callBack">回调函数</param> /// <param name="objs">回调函数的参数</param> /// <returns></returns> public static TimerEvent Register(string key, float interval, int repeatCount, TimerCallBack callBack, params object[] objs) { return AddTimer(key, interval, repeatCount, false, callBack, objs); } /// <summary> /// 延迟调用 忽略TimeScale /// </summary> /// <param name="key">Timer的名字</param> /// <param name="interval">开始延时</param> /// <param name="isIgnoreTimeScale">是否忽略时间缩放</param> /// <param name="callBack">回调函数</param> /// <param name="objs">回调函数的参数</param> /// <returns></returns> public static TimerEvent Register(string key, float interval, bool isIgnoreTimeScale, TimerCallBack callBack, params object[] objs) { return AddTimer(key, interval, 0, isIgnoreTimeScale, callBack, objs); } /// <summary> /// 间隔一定时间重复调用 忽略TimeScale /// </summary> /// <param name="key">Timer的名字</param> /// <param name="interval">间隔时间</param> /// <param name="repeatCount">重复调用的次数</param> /// <param name="isIgnoreTimeScale">是否忽略时间缩放</param> /// <param name="callBack">回调函数</param> /// <param name="objs">回调函数的参数</param> /// <returns></returns> public static TimerEvent Register(string key, float interval, int repeatCount, bool isIgnoreTimeScale, TimerCallBack callBack, params object[] objs) { return AddTimer(key, interval, repeatCount, isIgnoreTimeScale, callBack, objs); } /// <summary> /// 添加一个Timer /// </summary> /// <param name="interval">间隔时间</param> /// <param name="repeatCount">重复调用的次数</param> /// <param name="isIgnoreTimeScale">是否忽略时间缩放</param> /// <param name="key">Timer的名字</param> /// <param name="callBack">回调函数</param> /// <param name="objs">回调函数的参数</param> /// <returns></returns> public static TimerEvent AddTimer(string key, float interval, int repeatCount, bool isIgnoreTimeScale, TimerCallBack callBack, params object[] objs) { TimerEvent te = new TimerEvent(); te.m_key = key ?? te.GetHashCode().ToString(); te.m_currentTimer = 0; te.m_interval = interval; te.m_repeatCount = repeatCount; te.m_isIgnoreTimeScale = isIgnoreTimeScale; te.m_callBack = callBack; te.m_objs = objs; m_timers.Add(te); return te; } public static void DestroyTimer(TimerEvent timer, bool isCallBack = false) { if (m_timers.Contains(timer)) { if (isCallBack) { timer.CallBackTimer(); } m_timers.Remove(timer); } else { Debug.LogError("Dont exist timer:" + timer); } } public static void UnRegister(string key, bool isCallBack = false) { for (int i = 0; i < m_timers.Count; i++) { TimerEvent te = m_timers[i]; if (te.m_key.Equals(key)) { DestroyTimer(te, isCallBack); } } } public static void UnRegister(bool isCallBack = false) { for (int i = 0; i < m_timers.Count; i++) { if (isCallBack) { m_timers[i].CallBackTimer(); } } m_timers.Clear(); } public static void ResetTimer(TimerEvent timer) { if (m_timers.Contains(timer)) { timer.ResetTimer(); } else { Debug.LogError("Dont exist timer:" + timer); } } public static void ResetTimer(string key) { for (int i = 0; i < m_timers.Count; i++) { var e = m_timers[i]; if (e.m_key.Equals(key)) { ResetTimer(e); } } } } |
测试代码:
1 2 3 4 5 6 7 8 9 10 |
void Start () { Timer.Register(1, (param) => { Debug.Log("这是Timer延迟1秒执行,,还有很多Timer参数,详见Timer类"); }); Timer.Register("timer_delay_test", 2, 5, (param) => { Debug.Log("这是Timer每2秒执行一次,执行5次,,还有很多Timer参数,详见Timer类 参数:" + param[0]); }, "abc") ; } |
输出结果:
- 本文固定链接: http://www.u3d8.com/?p=1817
- 转载请注明: 网虫虫 在 u3d8.com 发表过