com.amanotes.statemachine
v1.1.0
Published
Generic state machine implementation for Unity
Readme
StateMachine
Package: com.amanotes.statemachine
Version: 1.0.0
Generic type-safe state machine for Unity with enum-based states and transition validation.
Features
- 🎮 Type-safe state management with generics
- 🎯 Automatic transition validation
- 🎨 Enter/Exit callbacks per state
- 🚀 Fluent API for state registration
- 🔧 State change event notifications
Documentation
Complete API inline in this README (mini-package: 2 classes).
Installation
Add the package to your Unity project via Packages/manifest.json:
{
"dependencies": {
"com.amanotes.statemachine": "1.0.0"
},
"scopedRegistries": [
{
"name": "npmjs",
"url": "https://registry.npmjs.org/",
"scopes": [
"com.amanotes.statemachine"
]
}
]
}Quick Start
public enum GameState { Menu, Playing, Paused }
public class GameStateMachine : StateMachine<GameState>
{
public GameStateMachine() : base(GameState.Menu)
{
RegisterState(GameState.Menu)
.WithTransitionTo(GameState.Playing);
RegisterState(GameState.Playing)
.WithTransitionTo(GameState.Paused);
RegisterState(GameState.Paused)
.WithTransitionTo(GameState.Playing);
}
}
var sm = new GameStateMachine();
sm.OnEnter(GameState.Playing, () => Debug.Log("Game started"));
sm.Reset();
sm.TransitionTo(GameState.Playing);API Reference
Key Classes
StateMachine
- Purpose: Generic state machine with transition validation
- Key methods:
RegisterState(),TransitionTo(),OnEnter(),OnExit() - Key properties:
CurrentState,PreviousState,IsStarted
StateInfo
- Purpose: State metadata and transition builder
- Key methods:
WithTransitionTo()
StateMachine
Properties:
TKey CurrentState- Current active stateTKey PreviousState- Previously active statebool IsStarted- Whether state machine has been startedstring CurrentStateName- Name of current stateAction<TKey, TKey> OnStateChanged- Event fired on state transitions
Lifecycle:
StateMachine()- Constructor without initial stateStateMachine(TKey initialState)- Constructor with initial statevoid Start(TKey state)- Start state machine at specific statevoid Reset()- Reset to initial state (if provided)bool TransitionTo(TKey key)- Transition to new state (validates transition)void Clear()- Clear current state without resetting
Registration:
StateInfo<TKey> RegisterState(TKey key, string name = null)- Register statevoid AddTransition(TKey from, TKey to)- Add valid transition
Callbacks:
void OnEnter(TKey key, Action callback)- Add enter callbackvoid OnExit(TKey key, Action callback)- Add exit callbackvoid RemoveOnEnter(TKey key, Action callback)- Remove enter callbackvoid RemoveOnExit(TKey key, Action callback)- Remove exit callbackvoid AddOnStateChanged(Action<TKey, TKey> callback)- Add state change listener
Queries:
bool IsState(TKey key)- Check if currently in statebool HasState(TKey key)- Check if state is registeredstring GetStateName(TKey key)- Get state name
StateInfo
Properties:
TKey Key- State keyint Id- Internal state IDstring Name- State nameAction OnEnter- Enter callbackAction OnExit- Exit callbackStateMachine<TKey> StateMachine- Parent state machine
Methods:
StateInfo<TKey> WithTransitionTo(TKey to)- Add transition (fluent API)
Dependencies
- Unity 2021.3+
Version History
See CHANGELOG.md
