State
A state must be a MonoBehaviour and must implement the IState
interface. A default implementation of this interface is available as the StateBehaviour
class.
Enter/Exit
The enter and exit functions are Task based:
public class StateA : StateBehaviour {
private void Awake() {
// You have access to the Machine variable which is the StateMachine that is running this state
}
public override Task Enter() {
// Do stuff on enter
return base.Enter();
}
public override Task Exit() {
// Do stuff on exit
return base.Exit();
}
// TIP: The regular unity events are not called if you enable the sync options on the StateMachine component !
private void Update() {
}
}
Create your state
namespace MyProject {
[AddComponentMenu("My project/FSM 1/Game state")]
public class GameState : StateBehaviour {
StateMachine _machine => (StateMachine) Machine;
public override Task Enter() {
DoSomething();
return base.Enter();
}
async void DoSomething() {
await new WaitForSeconds(2f);
_machine.ChangeState<ScoreState>();
}
}
}