/// <summary> /// 将临时入栈的state出栈, 并设置成当前状态 /// </summary> public void PopStackState() { GameStateBase oldState = currentState; if (oldState != null) { oldState.PrepareExit(); } currentState = stateStack.Pop(); currentState.PreparePopStack(); if (oldState != null) { var oldStateType = oldState.StateType; oldState.Exit(); // 记录上次的状态 stateChangingContex.RecordLastState(oldStateType); } currentState.PopStack(); if (oldState != null) { oldState.DoneExit(); oldState.Dispose(); GameObject.Destroy(oldState); } currentState.DonePopStack(); }
/// <summary> /// 清理掉所有如栈的状态 /// </summary> public void ClearPushedStates() { while (stateStack.Count > 0) { GameStateBase state = stateStack.Pop(); state.PrepareExit(); var oldStateType = state.StateType; state.Exit(); // 记录上次的状态 stateChangingContex.RecordLastState(oldStateType); state.DonePushStack(); state.Dispose(); GameObject.Destroy(state); } }
/// <summary> /// 切换GameState /// </summary> /// <param name="force">false时, 如果当前状态为目标状态, 不执行切换, 直接返回当前状态</param> public T EnterState <T>(object userData = null, bool force = false) where T : GameStateBase { if (force == false) { // 如果当前状态为目标状态, 直接返回 if (currentState != null && typeof(T) == currentState.GetType()) { return(currentState as T); } } GameStateBase oldState = currentState; currentState = rootGo.AddComponent <T>(); currentState.MachineManager = this; currentState.Create(userData); if (oldState != null) { oldState.PrepareExit(); } currentState.PrepareEnter(); if (oldState != null) { var oldStateType = oldState.StateType; oldState.Exit(); // 记录上次的状态 stateChangingContex.RecordLastState(oldStateType); } currentState.Enter(); if (oldState != null) { oldState.DoneExit(); oldState.Dispose(); GameObject.Destroy(oldState); } currentState.DoneEnter(); return(currentState as T); }