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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
| public class TimerManager { public const string commonGroupFlag = "COMMON_TIMER"; private static List<Timer> timers_idle = new List<Timer>(); private static Dictionary<string, TimerGroup> timerGroupDict = new Dictionary<string, TimerGroup>();
public static Timer OpenTimer(float timer, string groupFlag, bool isPlay = true, bool isAutoClose = true) { if (timers_idle == null) timers_idle = new List<Timer>(); Timer t; if (timers_idle.Count > 0) { t = timers_idle[0]; timers_idle.RemoveAt(0); } else t = new Timer(); if (timerGroupDict.ContainsKey(groupFlag)) timerGroupDict[groupFlag].Add(t); else { timerGroupDict.Add(groupFlag, new TimerGroup().Init(groupFlag)); timerGroupDict[groupFlag].Add(t); } t.Init(timer, isAutoClose, timerGroupDict[groupFlag]); if (isPlay) t.Play(true); return t; }
public static Timer OpenTimer(float timer, bool isPlay = true, bool isAutoClose = true) { return OpenTimer(timer, commonGroupFlag, isPlay, isAutoClose); }
public static TimerGroup[] GetGroups() { if (timerGroupDict == null || timerGroupDict.Count == 0) return null; return timerGroupDict.Values.ToArray(); }
public static void StopGroup(string groupFlag) { if (!timerGroupDict.ContainsKey(groupFlag)) return; timerGroupDict[groupFlag].Stop(); }
public static void PauseGroup(string groupFlag) { if (!timerGroupDict.ContainsKey(groupFlag)) return; timerGroupDict[groupFlag].Pause(); }
public static void ContinueGroup(string groupFlag) { if (!timerGroupDict.ContainsKey(groupFlag)) return; timerGroupDict[groupFlag].Continue(); }
public static void CloseTimer(Timer timer, string groupFlag) { if (timer == null) return; if (!timerGroupDict.ContainsKey(groupFlag)) return; timerGroupDict[groupFlag].Remove(timer); }
public static void CloseGroup(string groupFlag) { if (!timerGroupDict.ContainsKey(groupFlag)) return; timerGroupDict[groupFlag].CloseGroup(); }
public static void SetTimerScale(string groupFlag, float timeScale) { if (timeScale < 0) return; if (!timerGroupDict.ContainsKey(groupFlag)) return; timerGroupDict[groupFlag].timeScale = timeScale; }
public static float GetTimerScale(string groupFlag) { if (!timerGroupDict.ContainsKey(groupFlag)) return -1f; return timerGroupDict[groupFlag].timeScale; }
public static void StopAll() { TimerGroup[] groups = GetGroups(); if (groups == null || groups.Length == 0) return; for (int i = groups.Length - 1; i >= 0; i--) groups[i].Stop(); }
public static void PauseAll() { TimerGroup[] groups = GetGroups(); if (groups == null || groups.Length == 0) return; for (int i = groups.Length - 1; i >= 0; i--) groups[i].Pause(); }
public static void ContinueGroup() { TimerGroup[] groups = GetGroups(); if (groups == null || groups.Length == 0) return; for (int i = groups.Length - 1; i >= 0; i--) groups[i].Continue(); }
public static void CloseAll() { TimerGroup[] groups = GetGroups(); if (groups == null || groups.Length == 0) return; for (int i = groups.Length - 1; i >= 0; i--) groups[i].CloseGroup(); }
static void CloseTimered(Timer timer, TimerGroup group) { if (timers_idle.Contains(timer)) return; timers_idle.Add(timer); }
static void CloseGrouped(TimerGroup group) { if (group == null) return; if (timerGroupDict.ContainsKey(group.groupFlag)) timerGroupDict.Remove(group.groupFlag); }
public class TimerGroup { public float timeScale = 1; public string groupFlag { get; private set; } private List<Timer> list = new List<Timer>();
public TimerGroup Init(string groupFlag) { this.groupFlag = groupFlag; return this; }
public void Add(Timer timer) { if (list.Contains(timer)) return; list.Add(timer); }
public void Remove(Timer timer) { if (list == null) return; if (!list.Contains(timer)) return; timer.Dispose(); list.Remove(timer); CloseTimered(timer, this); }
public void Stop() { if (list == null || list.Count == 0) return; for (int i = list.Count - 1; i >= 0; i--) list[i]?.Stop(); }
public void Pause() { if (list == null || list.Count == 0) return; for (int i = list.Count - 1; i >= 0; i--) list[i]?.Pause(); }
public void Continue() { if (list == null || list.Count == 0) return; for (int i = list.Count - 1; i >= 0; i--) list[i]?.Play(); }
public void CloseGroup() { if (list != null && list.Count > 0) for (int i = list.Count - 1; i >= 0; i--) Remove(list[i]); list?.Clear(); CloseGrouped(this); list = null; groupFlag = null; } }
public class Timer : CommonBehaviour.NormalLoop { private TimerGroup timerGroup; public string groupFlag { get; private set; } private bool isAutoClose = true; private Action<float, float> progressAct1; private Action<float> progressAct2; private Action completeAct; public bool isPlaying { get; private set; } = false; public float timer { get; private set; } public float curTimer { get; private set; }
public Timer() { PauseLoop(); }
public Timer Init(float timer, bool isAutoClose, TimerGroup timerGroup) { if (isPlaying) { Debug.LogError("Timer 初始化失败!"); return null; } this.timerGroup = timerGroup; groupFlag = timerGroup.groupFlag; this.isAutoClose = isAutoClose; this.timer = timer; curTimer = 0; return this; }
public Timer Play(bool restart = false) { if (restart) curTimer = 0; progressAct1?.Invoke(curTimer, timer); progressAct2?.Invoke(curTimer / timer); ContinueLoop(); isPlaying = true; return this; }
public Timer Pause() { if (!isPlaying) return this; isPlaying = false; PauseLoop(); return this; }
public Timer Stop() { PauseLoop(); isPlaying = false; curTimer = 0; return this; }
public Timer OnComplete(Action completeAct) { this.completeAct = completeAct; return this; }
public Timer OnProgress(Action<float, float> progressAct) { progressAct1 = progressAct; return this; }
public Timer OnProgress(Action<float> progressAct) { progressAct2 = progressAct; return this; }
public override void Update() { curTimer += Time.deltaTime * timerGroup.timeScale; if (curTimer > timer) curTimer = timer; progressAct1?.Invoke(curTimer, timer); progressAct2?.Invoke(curTimer / timer); if (curTimer == timer) { isPlaying = false; completeAct?.Invoke(); if (!isAutoClose) Stop(); else timerGroup.Remove(this); } }
public void Dispose() { Stop(); timer = 0; progressAct1 = null; progressAct2 = null; completeAct = null; } } }
|