protected void OnTick(float dt) { _isUpdating = true; // CLog.Log ("_listSchedulerEntity.count:"+_listSchedulerEntity.Count); for (int i = 0; i < _listSchedulerEntity.Count; i++) { SchedulerEntity entity = _listSchedulerEntity [i]; if (entity.state == SchedulerEntityState.Error) { continue; } if (entity.state == SchedulerEntityState.ToDoAction) { try { if (!entity.OnTick(dt)) { entity.state = SchedulerEntityState.ToRemove; SchedulerEntity RemoveEntity = _pool.GetObject(); RemoveEntity.Init(entity.handler); RemoveEntity.state = SchedulerEntityState.ToRemove; _listOperateEntity.Add(RemoveEntity); } } //捕获异常,防止有异常后整个scheduler全部卡死 catch (System.Exception ex) { entity.state = SchedulerEntityState.Error; CLog.LogError(ex.Message + "\n" + ex.StackTrace); } } } _isUpdating = false; for (int i = 0; i < _listOperateEntity.Count; i++) { SchedulerEntity entity = _listOperateEntity [i]; if (entity.state == SchedulerEntityState.ToAdd) { AddScheduler(entity.handler, entity.delay, entity.times); } else if (entity.state == SchedulerEntityState.ToRemove) { RemoveScheduler(entity.handler); } _pool.SaveObject(entity); } _listOperateEntity.Clear(); }
public bool AddScheduler(SchedulerHandler handler, float delay, int times = 0) { if (HasScheduler(handler)) { return(false); } SchedulerEntity entity = _pool.GetObject(); entity.Init(handler, delay, times); if (_isUpdating) { entity.state = SchedulerEntityState.ToAdd; _listOperateEntity.Add(entity); } else { entity.state = SchedulerEntityState.ToDoAction; _listSchedulerEntity.Add(entity); } return(true); }
public void RemoveScheduler(SchedulerHandler handler) { if (_isUpdating) { SchedulerEntity entity = _pool.GetObject(); entity.Init(handler); entity.state = SchedulerEntityState.ToRemove; _listOperateEntity.Add(entity); //有可能当前的scheduleEntity.handler里移除了一个即将调用的scheduleEntity(被移掉的不再执行) for (int i = 0; i < _listSchedulerEntity.Count; i++) { if (_listSchedulerEntity [i].handler == handler) { _listSchedulerEntity [i].state = SchedulerEntityState.ToRemove; } } } else { RealRemoveSchedule(handler); } }