private static int Add(System.Action action, int interval, int repeatCount, bool ignoreTimeScale) { HS_Scheduler.Task task; if (mTaskDict.TryGetValue(action, out task)) { return(task.id); } task = new HS_Scheduler.Task(action, interval, repeatCount, (int)(GetTimer(ignoreTimeScale) * 1000) + interval, ignoreTimeScale); if (repeatCount == -1) { int index = 0; for (int i = 0, count = mTaskList.Count; i < count; i++) { if (mTaskList[i].repeat != -1) { index = i; break; } } mTaskList.Insert(index, task); } else { InsertTask(task); } mTaskMap.Add(task.id, task); mTaskDict.Add(task.action, task); return(task.id); }
void Update() { int currentTimer = (int)(GetTimer() * 1000), currentTimerOnTimeScale = (int)(GetTimer(false)); HS_Scheduler.Task[] tasks = mTaskList.ToArray(); int index = 0, count = tasks.Length; for (; index < count; index++) { HS_Scheduler.Task task = tasks[index]; if (task.destroyed) { continue; } if (task.repeat == -1) { task.action(); } else { int timer = task.ignoreTimeScale ? currentTimer : currentTimerOnTimeScale; if (timer >= task.nextExecTime) { mTaskList.Remove(task); if (task.repeat > 0 && --task.repeat == 0) { mTaskMap.Remove(task.id); mTaskDict.Remove(task.action); task.destroyed = true; } task.action(); if (!task.destroyed) { task.nextExecTime = timer + task.interval; InsertTask(task); } } } } }
private static void InsertTask(HS_Scheduler.Task task) { int left = 0, right = mTaskList.Count, middle = right; while (left < right) { middle = (left + right) / 2; HS_Scheduler.Task obj = mTaskList[middle]; if (obj.repeat == -1) { left = middle + 1; } else if (task.nextExecTime >= obj.nextExecTime) { left = middle + 1; } else if (task.nextExecTime < obj.nextExecTime) { right = middle - 1; } } mTaskList.Insert(middle, task); }