Easy to use and safe (no scene setup, all you need will be created automatically at runtime)
NO singletons and static classes.
Basic usage:
publicclassSample:MonoBehaviour{privatevoidStart(){// Just like a FindObjectOfType, but if it is not exists => create it// (better singleton version)varstateMachine=this.FindLazy<GameStateMachine>();// Subscribing
stateMachine.On<RunningState>(()=> Debug.Log("Running"));
stateMachine.On<LoseState,WinState>(()=> Debug.Log("The game was finished (lose or win)"));// Pushing states
stateMachine.Push(new RunningState());
stateMachine.Push(new WinState());}}
Common use cases:
// On player trigger the finish => push WinState// GameStateMachineUser hashes StateMachine for us :)publicclassFinish:GameStateMachineUser{privatevoidOnTriggerEnter(Colliderother){if(other.TryGetComponent(out Player p))
StateMachine.Push(new WinState());}}// OnGameFinish will be called on WinState or LoseState (from GameStateMachineUser) publicclassPlayerMovement:GameStateMachineUser{privatefloat_speedFactor=1;protectedoverridevoidOnGameFinish(){_speedFactor=0;}}
Custom states be like:
// Create your own states!publicclassMyCustomState:GameState{// Which states can be after our custom state?publicoverride GameState[] PossibleNextStates => GameState[]{new RunningState(),new WinState(),new LoseState(),};// The state is not active (like pause, etc.)publicoverrideboolIsGameActiveDuringState=>false;}
About
Flexible and configurable game state machine for unity (it s not a real FSM but more like the game events aggregator)