A flexible finite state machine implementation for Unity game development
- Open
Window > Package Manager
- Click
+ > Add package from git URL
- Paste:
https://github.com/Trellcko/StateMachine.git
To use the State Machine in your own scripts, you must reference its assembly definition.
- In your project, create an
.asmdef
file (if you don't already have one). - In the Inspector, add a reference to the
Trellcko.StateMachine
assembly. - You can now use the package's namespaces in your code:
public class BootstrapState : BaseStateWithoutPayload
{
private readonly IInitService _initService;
public BootstrapState(StateMachine stateMachine, IInitService initService) : base(stateMachine)
{
_initService = initService;
GoToState<EmptyState>(()=> _initService.IsInited, 0);
}
public overide void Enter()
{
_initService.Init();
}
}
public class ExampleBehaviour : MonoBehaviour
{
private readonly IStateMachine _stateMachine = new StateMachine();
private readonly IInitService _initService = new();
private void Awake()
{
_stateMachine.AddStates(new EmptyState(_stateMachine),
new BootstrapState(_stateMachine, _initService));
_stateMachine.SetState<BootstrapState>();
}
private void Update()
{
_stateMachine.Update();
}
}