# friendsea/statemachinegraph architecture

> Architecture diagram of the GitHub repository friendsea/statemachinegraph, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/friendsea/statemachinegraph
- Repository: https://github.com/friendsea/statemachinegraph

## Overview

Based on our analysis of the file tree and the README, this project is a Unity-integrated toolkit that provides a state machine system together with a custom GraphView editor interface. In other words, it’s a specialized Unity extension that lets designers and developers build and edit state machines through a graphical interface, with the underlying logic provided by runtime components and optionally enhanced by source generation.

##### Main Components
- Editor Components:
- The “FriendSea/StateMachine/Editor” folder contains most of the code required for the editor UI, such as node definitions (State, Transition, Trigger, etc.), the GraphView window, property drawers, and asset importers.
- The “GraphViewSerializer” folder under Editor provides components for serializing graph view data, including nodes, edges, groups, and search menu providers.
- Runtime Components:
- The “FriendSea/StateMachine/Runtime” folder holds the core state machine logic. It includes base classes, state machine asset definitions (e.g., StateMachineAsset, State, Transition, etc.), and even specific implementations such as GameObjectStateMachine integration.
- This section also contains integration points for Unity’s Input System, with extensions for handling input behaviors.
- Generator Components:
- The “FriendSea/StateMachine/Generator” folder covers source generation aspects. Although there isn’t an extensive README section describing it, the presence of a “Debug/netstandard2.0” path with a StateMachineSourceGenerator assembly shows that the project leverages source generation to potentially create boilerplate code or streamline state machine code integration.
- Sample Project:
- The “SampleProject” directory demonstrates how the state machine system is consumed within a Unity project. It shows practical integration with character control, animations, and state machine asset usage.

##### Relationships and Interactions
- The Editor components (GraphView window, asset property drawers, and custom nodes) provide the graphical editing interface, and they interact with serialized graph view data (handled by GraphViewSerializer) to let the developer visually define and edit a state machine.
- The state machine definitions created and modified via the editor feed into the runtime logic. In the runtime, these definitions are used to instantiate and run state machines, drive behaviors (like Animator and Movement Behaviours), and react to input as defined in the InputSystem extensions.
- The Generator might analyze state machine assets at compile time to generate code or verify configurations, bridging the gap between design time and runtime behavior.
- The Sample Project demonstrates an end-to-end workflow: from asset creation in the Editor (using GraphView) to their execution in a running Unity scene.

##### Important Architectural Patterns and Design Principles
- Separation of Concerns: The project decouples the editor functionality from the runtime execution of state machines—ensuring that custom editors (which use Unity’s UI elements) do not mingle with the logic executed at runtime.
- Modular Design: Components such as GraphViewSerializer, runtime core, and source generator are located in separate folders (and even separate asmdef assemblies). This modularity supports easier maintenance, testing, and potential reuse.
- Unity Integration: The design capitalizes on Unity’s extensibility features (like .asmdef for assembly definitions, custom property drawers, and custom inspectors) to integrate seamlessly into a Unity workflow.

##### Relevant Technologies, Frameworks, and Libraries
- Unity’s GraphView UI framework (or a similar UI system) for the editor’s state machine graphical interface.
- Unity’s input system (noted in the InputSystem folder) for handling input-related state transitions.
- .NET’s code generation capabilities through the “Generator” components.
- Usage of Assembly Definition (.asmdef) files for modularizing code in Unity.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Editor Components
    subgraph "Editor Components"
        A1["GraphView Window (CS)"]:::editor
        A2["GraphView Window (UXML)"]:::editor
        A3["GraphView Window (USS)"]:::editor
        A4["Node Definitions"]:::editor
        A5["StateMachineAssetPropertyDrawer"]:::editor
        A6["StateMachineImporter"]:::editor
    end

    %% GraphView Serialization
    subgraph "GraphView Serialization"
        B1["GraphView Serializer"]:::serializer
    end

    %% Runtime Components
    subgraph "Runtime Components"
        C1["State Machine Core"]:::runtime
        C2["Input System Extensions"]:::runtime
    end

    %% Generator Components
    subgraph "Generator Components"
        D1["Source Generator"]:::generator
        D2["StateMachineSourceGenerator"]:::generator
    end

    %% Sample Project
    subgraph "Sample Project"
        E1["SampleProject"]:::sample
    end

    %% Connections from Editor to GraphView Serialization
    A1 -->|"sends data"| B1
    A2 -->|"sends data"| B1
    A3 -->|"sends data"| B1
    A4 -->|"sends data"| B1
    A5 -->|"sends data"| B1
    A6 -->|"sends data"| B1

    %% Connections from Editor and Serialization to Runtime
    A4 -->|"defines assets"| C1
    B1 -->|"serializes layout"| C1

    %% Generator flow connections
    A4 -->|"triggers gen"| D1
    D1 -->|"build process"| D2
    D2 -->|"enhances logic"| C1

    %% Runtime Extensions internal connection
    C2 ---|"integrates with"| C1

    %% Runtime to Sample Project
    C1 -->|"runtime usage"| E1

    %% Click Events for Editor Components
    click A1 "https://github.com/friendsea/statemachinegraph/blob/main/FriendSea/StateMachine/Editor/StateMachineGraphWindow.cs"
    click A2 "https://github.com/friendsea/statemachinegraph/blob/main/FriendSea/StateMachine/Editor/StateMachineGraphWindow.uxml"
    click A3 "https://github.com/friendsea/statemachinegraph/blob/main/FriendSea/StateMachine/Editor/StateMachineGraphWindow.uss"
    click A4 "https://github.com/friendsea/statemachinegraph/tree/main/FriendSea/StateMachine/Editor/Nodes"
    click A5 "https://github.com/friendsea/statemachinegraph/blob/main/FriendSea/StateMachine/Editor/StateMachineAssetPropertyDrawer.cs"
    click A6 "https://github.com/friendsea/statemachinegraph/blob/main/FriendSea/StateMachine/Editor/StateMachineImporter.cs"

    %% Click Event for GraphView Serialization
    click B1 "https://github.com/friendsea/statemachinegraph/tree/main/FriendSea/StateMachine/GraphViewSerializer"

    %% Click Events for Runtime Components
    click C1 "https://github.com/friendsea/statemachinegraph/tree/main/FriendSea/StateMachine/Runtime"
    click C2 "https://github.com/friendsea/statemachinegraph/tree/main/FriendSea/StateMachine/Runtime/Extensions/InputSystem"

    %% Click Events for Generator Components
    click D1 "https://github.com/friendsea/statemachinegraph/tree/main/FriendSea/StateMachine/Generator"
    click D2 "https://github.com/friendsea/statemachinegraph/tree/main/FriendSea/StateMachine/Generator/Debug/netstandard2.0/StateMachineSourceGenerator"

    %% Click Event for Sample Project
    click E1 "https://github.com/friendsea/statemachinegraph/tree/main/SampleProject"

    %% Styles
    classDef editor fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef serializer fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef runtime fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef generator fill:#FFFFE0,stroke:#000,stroke-width:2px;
    classDef sample fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
