/// <summary> /// 倒计时 /// </summary> /// <param name="taskName"></param> /// <param name="totalTime"></param> /// <param name="onUpdate"></param> /// <param name="onFinished"></param> /// <param name="updateFrequence"></param> /// <param name="timeScale"></param> /// <returns></returns> public CDTask SetupCoolDown(string taskName, float totalTime, CDTask.OnUpdate onUpdate, CDTask.OnFinish onFinished, float updateFrequence = 0.1f, bool timeScale = false) { if (string.IsNullOrEmpty(taskName)) { return(null); } if (totalTime <= 0) { if (onFinished != null) { onFinished(); } return(null); } CDTask cdTask = GetCdTask(taskName); if (cdTask != null) { cdTask.Reset(totalTime, onUpdate, onFinished, updateFrequence, timeScale); } else { cdTask = new CDTask(taskName, totalTime, onUpdate, onFinished, updateFrequence, timeScale); } return(cdTask); }
public void RemoveCdFinishedandler(string taskName, CDTask.OnFinish onFinishedHandler) { CDTask task = GetCdTask(taskName); if (task != null) { task.OnFinished -= onFinishedHandler; } }
public void RemoveCdUpdateHandler(string taskName, CDTask.OnUpdate onUpdateHandler) { CDTask task = GetCdTask(taskName); if (task != null) { task.onUpdate -= onUpdateHandler; } }
public void CancelCd(string taskName) { CDTask task = GetCdTask(taskName); if (task != null) { task.isVaild = false; } }
public float GetRemainTime(string taskName) { CDTask task = GetCdTask(taskName); if (task != null) { return(task.remainTime); } return(0); }
public bool ResumeCd(string taskName) { CDTask task = GetCdTask(taskName); if (task != null) { task.isPause = false; return(true); } return(false); }
public bool AddCdIsNotExist(CDTask pCdTask) { if (null == pCdTask) { return(false); } if (IsCdExist(pCdTask.taskName)) { return(false); } mCdTasks.Add(pCdTask); return(true); }
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(); } }