Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/paulnonatomic/visualstatemachinev2
A Unity C# Visual State Machine
https://github.com/paulnonatomic/visualstatemachinev2
editor state-machine state-management stateflow tool tooling unity
Last synced: about 2 months ago
JSON representation
A Unity C# Visual State Machine
- Host: GitHub
- URL: https://github.com/paulnonatomic/visualstatemachinev2
- Owner: PaulNonatomic
- License: mit
- Created: 2024-01-31T23:26:41.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-09-17T18:32:45.000Z (4 months ago)
- Last Synced: 2024-09-17T22:54:50.778Z (4 months ago)
- Topics: editor, state-machine, state-management, stateflow, tool, tooling, unity
- Language: C#
- Homepage: https://github.com/PaulNonatomic/VisualStateMachineV2
- Size: 450 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Visual State Machine V2 (VSM2)
## Overview
Currently still a work in progress and subject to breaking changes.
Visual State Machine is a Unity package designed to simplify the creation and management of state machines in Unity projects. It provides a visual editor for designing state machines, making it easier to create complex behaviors without writing extensive code.https://github.com/user-attachments/assets/61388bea-8bd4-4f92-9f12-c0f5768e1e8f
## Features
- **Visual Editor**: Design state machines using a user-friendly graphical interface.
- **Unity Integration**: Seamlessly integrates with Unity, allowing for easy implementation in your game projects.
- **Custom State Support**: Create your own states to handle specific game behaviors.
- **Transition Management**: Easily manage transitions between states with intuitive controls.## Installation
To install Visual State Machine in your Unity project, follow these steps:
1. Via package manager add a package from git url https://github.com/PaulNonatomic/VisualStateMachineV2.git## Usage
1. Create a state machine asset from the project panel. Right click -> Create -> State Machine -> State Machine
2. Either right click and select "Add State" or drag out from the Entry Statehttps://github.com/user-attachments/assets/ff8fb491-fc7a-4658-8ee0-de0ddb2e8c0b
3. The State Selection window appears listing all available states.
- States are grouped by namespace with the inbuilt states appearing at the top.
- The group of states nearest to the location of the state machine asset will open by default but all states remain accessible.![Unity_QcTKNF3bSr](https://github.com/user-attachments/assets/05eb3410-81ef-4085-860c-d95683d24b8b)
4. Create a custom state. Here's the built in DelayState as an example.
- Add a Transition attribute to an exposed event Action in order for it to appear upon the states node in the State Machine Editor
- Serialized and public properties are also exposed in the states node in the State Machine Editor. Note fields should be populated with value types and assets and not scene types.```cs
[NodeWidth(width:190), NodeColor(NodeColor.Teal), NodeIcon(NodeIcon.Clock)]
public class DelayState : BaseDelayState
{
[Transition(frameDelay:0)]
public event Action OnComplete;
[NonSerialized]
private float _elapsedTime;public override void OnEnterState()
{
_elapsedTime = 0f;
}
public override void OnUpdateState()
{
_elapsedTime += Time.deltaTime;if (_elapsedTime < Duration) return;
OnComplete?.Invoke();
}public override void OnExitState()
{
//...
}
}
```5. Create a game object with a StateMachineController component upon it and assign it your new state machine asset.
6. Run the application with the StateMachineController selected to see the state of your state machine within the State Machine Editor window.## Loops with Jump Nodes
Add JumpOutState state and set it's Id. Then create a JumpInState with the corresponding Id to jump from one node to another.https://github.com/user-attachments/assets/35715234-e532-464b-9031-4b7780efd3ef
## Transition Delay
The process of transitioning between nodes originally incurred no delay at all but when wiring up a looping state machine
it could cause a stack overflow. To prevent this a delay of 1 frame has been added to all transitions by default, but this
can be configured on a per transition bases by passing a frameDelay value through the Transition attribute, but please use
with caution as a frameDelay of 0 can cause a stack overflow.## Copy & Paste
The State Machine Editor supports copy and paste within and between state machines.https://github.com/user-attachments/assets/09c1b644-c519-4bfd-8c29-4b0771d8d8b6
## Shared Data
States have access to a shared data store```cs
public class StateOne : State
{
[Transition] public event Action OnComplete;public override void OnEnterState()
{
SharedData.SetData("age", 42);
OnComplete?.Invoke();
}public override void OnExitState()
{
//...
}
}
public class StateTwo : State
{
[Transition] public event Action OnComplete;
public override void OnEnterState()
{
var age = SharedData.GetData("age");
Debug.Log($"StateTwo - Age:{age}");
OnComplete?.Invoke();
}public override void OnExitState()
{
//...
}
}
```This same data store is exposed externally to the state machine through the StateMachineController.SharedData
## License
VisualStateMachineV2 is licensed under the MIT license