/// <summary> /// 压入一个延迟函数,按照时间来延迟 /// </summary> /// <param name="task"></param> /// <param name="delayTime">延迟时间,单位:ms毫秒</param> /// <param name="type"></param> /// <param name="tag"></param> /// <returns></returns> public static DelayTask Invoke(Action task, int delayTime, object tag = null) { var delayTask = new DelayTask { Task = task, Delay = delayTime, Type = DelayType.Time, Tag = tag }; delayTask.taskLite = TaskLite.Invoke(t => { if (t < delayTime) { return(false); } task(); return(true); }); m_tasks.Add(delayTask); return(delayTask); }
private static void Release(TaskLite task) { lock (m_tasklock) { int index = m_tasks.IndexOf(task); if (index < 0) { return; } int current = currentOperateTaskIndex; if (index <= current) { //移除当前索引之前的task //移除当前索引的task m_tasks.RemoveAt(index); currentOperateTaskIndex--; } else { //移除当前索引的之后的task m_tasks.RemoveAt(index); } } }
private void Update() { int elapseTime = (int)(Time.smoothDeltaTime * 1000); TaskLite.Update(elapseTime); TweenLite.Update(elapseTime); OnUpdate?.Invoke(elapseTime); }
/// <summary> /// 压入一个执行函数 /// 如果返回true,则表示函数执行完成 /// 如果返回false,则下一帧还会继续执行 /// </summary> /// <param name="action"></param> /// <param name="delayFrame"></param> /// <param name="tag"></param> /// <returns></returns> public static TaskLite Invoke(Func <int, bool> action, int delayFrame = 0, object tag = null) { lock (m_tasklock) { var r = new TaskLite { Func = action, DelayFrame = delayFrame, Tag = tag }; m_tasks.Add(r); return(r); } }
/// <summary> /// 压入一个任务,按照一定时间间隔来执行 /// </summary> /// <param name="task"></param> /// <param name="interval"></param> /// <param name="stopType"></param> /// <param name="stopValue"></param> /// <param name="tag"></param> /// <returns></returns> public static IntermittentTask Invoke(Action task, int interval, ThrottleStopType stopType, int stopValue, object tag = null) { lock (m_tasklock) { var throttleTask = new IntermittentTask { Task = task, Interval = interval, stopType = stopType, stopValue = stopValue, Tag = tag }; throttleTask.taskLite = TaskLite.Invoke(t => { if (t < throttleTask.lastRunTime + throttleTask.Interval) { return(false); } throttleTask.lastRunTime = t; task?.Invoke(); throttleTask.Times++; if (throttleTask.stopType == ThrottleStopType.Times) { return(throttleTask.Times >= stopValue); } else if (throttleTask.stopType == ThrottleStopType.Duration) { return(throttleTask.Timer >= stopValue); } else { return(false); } }); return(throttleTask); } }