Exemplo n.º 1
0
        /// <summary>
        /// 更换定时任务
        /// </summary>
        public bool ReplaceTimerTask(int tid, Action callback, float destTime, float delay, int count = 1, PETimeType timeType = PETimeType.Millisecond)
        {
            if (timeType != PETimeType.Millisecond)
            {
                switch (timeType)
                {
                case PETimeType.Second:
                    destTime = destTime * 1000;
                    break;

                case PETimeType.Minute:
                    destTime = destTime * 1000 * 60;
                    break;

                case PETimeType.Hour:
                    destTime = destTime * 1000 * 60 * 60;
                    break;

                case PETimeType.Day:
                    destTime = destTime * 1000 * 60 * 60 * 24;
                    break;

                default:
                    Debug.Log("Add Time Type Error!");
                    break;
                }
            }
            destTime = Time.realtimeSinceStartup * 1000 + destTime;
            PETimerTask newTask = new PETimerTask(callback, destTime, delay, count, tid);
            bool        isRep   = false;

            for (int i = 0; i < timerTasks.Count; i++)
            {
                if (tid == timerTasks[i].tid)
                {
                    timerTasks[i] = newTask;
                    isRep         = true;
                    break;
                }
            }

            if (!isRep)
            {
                for (int i = 0; i < tempTimerTasks.Count; i++)
                {
                    if (tid == tempTimerTasks[i].tid)
                    {
                        tempTimerTasks[i] = newTask;
                        isRep             = true;
                        break;
                    }
                }
            }

            return(isRep);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 删除定时任务
        /// </summary>
        public bool DeleteTimerTask(int tid)
        {
            //是否删除任务成功
            bool result = false;

            for (int i = 0; i < timerTasks.Count; i++)
            {
                PETimerTask timerTask = timerTasks[i];
                if (timerTask.tid == tid)
                {
                    timerTasks.RemoveAt(i);
                    for (int j = 0; j < idList.Count; j++)
                    {
                        if (tid == idList[j])
                        {
                            idList.RemoveAt(j);
                            break;
                        }
                    }
                    result = true;
                    break;
                }
            }

            //从缓存列表里面找
            if (!result)
            {
                for (int i = 0; i < tempTimerTasks.Count; i++)
                {
                    PETimerTask timerTask = tempTimerTasks[i];
                    if (timerTask.tid == tid)
                    {
                        tempTimerTasks.RemoveAt(i);
                        for (int j = 0; j < idList.Count; j++)
                        {
                            if (tid == idList[j])
                            {
                                idList.RemoveAt(j);
                                break;
                            }
                        }
                        result = true;
                        break;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 检测时间任务
        /// </summary>
        private void CheakTimerTask()
        {
            //为什么要先加入缓存列表,因为延长时间并不是当前帧执行,需要等待一帧,所以在上一帧加入缓存列表中,在下一帧再取出
            for (int tempIndex = 0; tempIndex < tempTimerTasks.Count; tempIndex++)
            {
                timerTasks.Add(tempTimerTasks[tempIndex]);
            }
            tempTimerTasks.Clear();


            for (int index = 0; index < timerTasks.Count; index++)
            {
                PETimerTask timerTask = timerTasks[index];
                if (timerTask.destTime > Time.realtimeSinceStartup * 1000)
                {
                    continue;
                }
                else
                {
                    Action action = timerTask.callBack;
                    if (action != null)
                    {
                        action();
                    }
                    if (timerTask.count == 1)
                    {
                        timerTasks.RemoveAt(index);
                        index--;
                        recTidList.Add(timerTask.tid);
                    }
                    else
                    {
                        if (timerTask.count != 0)
                        {
                            timerTask.count -= 1;
                        }
                        timerTask.destTime += timerTask.delay;
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 添加时间任务
        /// </summary>
        /// <param name="callback">等待一段时间后需要执行的方法</param>
        /// <param name="destTime">第一次执行等待的时间</param>
        /// <param name="delay">(第二次执行后)重复执行等待的时间</param>
        /// <param name="count">重复执行的次数,0为无限重复执行</param>
        /// <param name="timeType">添加的时间类型,默认毫秒为单位,用秒需要乘1000</param>
        /// <returns></returns>
        public int AddTimerTask(Action callback, float destTime, float delay, int count = 1, PETimeType timeType = PETimeType.Millisecond)
        {
            if (timeType != PETimeType.Millisecond)
            {
                switch (timeType)
                {
                case PETimeType.Second:
                    destTime = destTime * 1000;
                    break;

                case PETimeType.Minute:
                    destTime = destTime * 1000 * 60;
                    break;

                case PETimeType.Hour:
                    destTime = destTime * 1000 * 60 * 60;
                    break;

                case PETimeType.Day:
                    destTime = destTime * 1000 * 60 * 60 * 24;
                    break;

                default:
                    Debug.Log("Add Time Type Error!");
                    break;
                }
            }
            int tid = GetID();

            destTime = Time.realtimeSinceStartup * 1000 + destTime;
            PETimerTask timerTask = new PETimerTask(callback, destTime, delay, count, tid);

            tempTimerTasks.Add(timerTask);
            idList.Add(tid);
            return(tid);
        }