Esempio n. 1
0
    void Update()
    {
        //计时器任务
        for (int i = 0, imax = mTimeTasks.Count; i < imax; ++i)
        {
            TimerTask timerTask = mTimeTasks[i];
            if (timerTask.isVaild)
            {
                if (timerTask.isPause)
                {
                    continue;
                }

                float deltaTime = timerTask.timeScale ? Time.deltaTime : Time.unscaledDeltaTime;
                timerTask.currentAccumulate += deltaTime;
                if (timerTask.currentAccumulate >= timerTask.updateFrequence)
                {
                    timerTask.currentAccumulate = 0f;
                    timerTask.DoUpdate();
                }
            }
            else
            {
                mTimerToRemove.Add(timerTask);
            }
        }

        if (mTimerToRemove.Count > 0)
        {
            for (int i = 0; i < mTimerToRemove.Count; ++i)
            {
                TimerTask timerTask = mTimerToRemove[i];
                timerTask.OnDispose();
                mTimerToRemove.Remove(timerTask);
            }

            mTimerToRemove.Clear();
        }

        //倒计器任务
        for (int i = 0, imax = mCdTasks.Count; i < imax; ++i)
        {
            CDTask cdTask = CdTasks[i];
            if (cdTask.isVaild)
            {
                if (cdTask.isPause)
                {
                    continue;
                }

                float deltaTime = cdTask.timeScale ? Time.deltaTime : Time.unscaledDeltaTime;
                cdTask.remainTime -= deltaTime;
                if (cdTask.remainTime <= 0)
                {
                    cdTask.remainTime = 0f;
                    cdTask.isVaild    = false;
                    cdTask.DoUpdate();
                    cdTask.DoFinish();
                }
                else
                {
                    cdTask.currentAccumulate += deltaTime;
                    if (cdTask.currentAccumulate >= cdTask.updateFrequence)
                    {
                        cdTask.currentAccumulate = 0f;
                        cdTask.DoUpdate();
                    }
                }
            }
            else
            {
                mCoolDownToRemove.Add(cdTask);
            }
        }

        if (mCoolDownToRemove.Count > 0)
        {
            for (int i = 0; i < mCoolDownToRemove.Count; ++i)
            {
                CDTask cdTask = mCoolDownToRemove[i];
                cdTask.OnDispose();
                mCdTasks.Remove(cdTask);
            }
            mCoolDownToRemove.Clear();
        }
    }