How thefuntastic/unity3d-finite-state-machine works
Main Components
• Core StateMachine Library:
• This is the heart of the package. Include modules for:
• StateMachine (the core FSM controller)
• StateDriverRunner and StateDriverUnity (which help manage method invocations)
• StateMapping and StateEvent, including Data-Driven State Events.
• Unity Integration:
• MonoBehaviour-based usage and how the state logic integrates into Unity’s event lifecycle (i.e., Update, FixedUpdate, and coroutine-based Enter/Exit methods).
• Custom Driver/Subscribers Layer:
• This is clearly delineated by the “Driver” concept in the README, where state events (like OnCollisionEnter, OnHealthPickup, etc.) are wrapped in a driver class.
• Testing and Samples:
• The samples directory shows practical use cases (prefabs, scripts such as ExampleAdvanced.cs and ExampleBasic.cs).
• The Tests folder contains both Editor and Runtime tests validating basic transitions, custom drivers, and additional event handling.
• Support Components:
• Assets (like MonsterLove meta files) and ProjectSettings indicate the Unity-specific configuration necessary for the library.
Relationships and Data Flow
• The game’s main MonoBehaviour, launching the state machine.
• The StateMachine Controller invoking state methods (e.g., Play_Enter, Play_Update, etc.) based on state transitions.
• The Driver’s role in representing additional events and how they are invoked from Unity event functions (Update, OnCollisionEnter) to drive the state transitions.
• How asynchronous transitions operate (Safe vs. Overwrite) and how the ‘Finally’ method is conceptually used for safe cleanup.
Architectural Patterns and Design Principles
• Convention over Configuration:
• How the naming convention (StateName_Method) ties state definitions to specific methods.
• Encapsulation:
• The library encapsulates state transitions, event invocation, and coroutine handling, minimizing side effects in external code.
• Layered Architecture:
• Different layers can be visually separated: the FSM core, Unity integration layer, and test/examples usage.
Technologies / Libraries
• Unity 2019.4 (or later) as the overall runtime environment.
• C#, utilising reflection (only at initialization) to reduce boilerplate.
• Use of coroutines (IEnumerator) for asynchronous state transition handling.
• Testing frameworks (likely Unity Test Runner) for both Editor and Runtime tests.