/// <summary> /// 检测定时帧任务是否到达可执行 /// </summary> private void CheckTimerFrameTask() { // 帧计数(会不会越界) frameCounter += 1; // 加入临时定时任务列表的定时任务 (这样做保证同帧不干扰下面遍历任务列表) for (int tempIndex = 0; tempIndex < tmpTaskFrameTimeList.Count; tempIndex++) { taskFrameTimeList.Add(tmpTaskFrameTimeList[tempIndex]); } tmpTaskFrameTimeList.Clear(); // 遍历检测定时任务是否满足执行条件 for (int index = 0; index < taskFrameTimeList.Count; index++) { TimerFrameTask task = taskFrameTimeList[index]; if (frameCounter < task.destFrame) { continue; } else { Action callback = task.callback; // 异常捕获,避免回调函数出错 try { if (callback != null) { callback(); } } catch (Exception e) { Debug.Log(e.Message); } // 任务循环只剩一次 if (task.loopCount == 1) { // 把结束任务不用的 taskid 缓存到 回收任务 id 列表中 recycleTaskIdList.Add(task.taskId); taskFrameTimeList.RemoveAt(index); index--; } else { // 不是无限循环,循环次数 -1 if (task.loopCount != 0) { task.loopCount -= 1; } // 目标时间更新一个 delay 时间 task.destFrame += task.delay; } } } }
/// <summary> /// 删除指定任务 /// </summary> /// <param name="taskId">任务id</param> /// <returns>返回是否删除</returns> public bool DeleteTimerFrameTask(int taskId) { bool exist = false; for (int i = 0; i < taskFrameTimeList.Count; i++) { TimerFrameTask task = taskFrameTimeList[i]; if (task.taskId == taskId) { taskFrameTimeList.RemoveAt(i); for (int j = 0; j < taskIdList.Count; j++) { if (taskIdList[j] == taskId) { taskIdList.RemoveAt(j); break; } } exist = true; break; } } // 从临时缓存任务列表中查找 if (exist == false) { for (int i = 0; i < tmpTaskFrameTimeList.Count; i++) { TimerFrameTask task = tmpTaskFrameTimeList[i]; if (task.taskId == taskId) { tmpTaskFrameTimeList.RemoveAt(i); for (int j = 0; j < taskIdList.Count; j++) { if (taskIdList[j] == taskId) { taskIdList.RemoveAt(j); break; } } exist = true; break; } } } return(exist); }
/// <summary> /// 添加定时帧任务 /// </summary> /// <param name="callback">任务回调</param> /// <param name="delay">延迟帧数</param> /// <param name="loopCount">循环次数(0 为 无限循环)</param> /// <returns>返回任务id</returns> public int AddTimerFrameTask(Action callback, int delay, int loopCount = 1) { int tid = GetTaskId(); int destTime = frameCounter + delay; TimerFrameTask timerTask = new TimerFrameTask(tid, callback, destTime, delay, loopCount); // 添加新定时帧任务的时候,先添加到临时列表(避免同帧遍历任务列表时干扰遍历列表) tmpTaskFrameTimeList.Add(timerTask); // tid 添加到使用列表中 taskIdList.Add(tid); return(tid); }
/// <summary> /// 替换存在的任务 /// </summary> /// <param name="taskId">任务id</param> /// <param name="callback">任务回调</param> /// <param name="delay">延迟帧数</param> /// <param name="loopCount">循环次数(0 为 无限循环)</param> /// <returns>返回是否替换成功</returns> public bool ReplaceTimerFrameTask(int taskId, Action callback, int delay, int loopCount = 1) { int destTime = frameCounter + delay; TimerFrameTask newTimerFrameTask = new TimerFrameTask(taskId, callback, destTime, delay, loopCount); bool isReplace = false; // 从任务列表中查找对应的 taskId 进行任务替换 for (int i = 0; i < taskFrameTimeList.Count; i++) { TimerFrameTask task = taskFrameTimeList[i]; if (task.taskId == taskId) { taskFrameTimeList[i] = newTimerFrameTask; isReplace = true; break; } } // 从临时缓冲任务列表中替换 if (isReplace == false) { for (int i = 0; i < tmpTaskFrameTimeList.Count; i++) { TimerFrameTask task = tmpTaskFrameTimeList[i]; if (task.taskId == taskId) { tmpTaskFrameTimeList[i] = newTimerFrameTask; isReplace = true; break; } } } return(isReplace); }