# cemuka/unityruntimenodeeditor architecture

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

- Interactive diagram: https://gitdiagram.com/cemuka/unityruntimenodeeditor
- Repository: https://github.com/cemuka/unityruntimenodeeditor

## Overview

##### Project Type and Purpose
- This is a Unity runtime library and example project (“Runtime Node Editor”) that provides an in-game node-graph editor using Unity UI (no editor-only code).
- Its goal is to allow creation, display, serialization, and manipulation of node graphs at runtime, with support for context menus, pan/zoom, multiple instances, custom prefabs, and input-system agnosticism.

##### Key Directories and Their Roles
- Assets/RuntimeNodeEditor
- Core runtime code: scripts for Graph, NodeEditor, Node, Socket, Serializer, Events, ContextMenu, UI rendering (lines, bezier curves).
- Prefab templates: default node containers, sockets, context menus.
- Imported assets: fonts, textures used by the runtime.
- Assets/Examples
- Two complete example editors (ColorEditor and SimpleMathEditor) demonstrating how to extend NodeEditor, custom nodes, scenes, and graph serialization.
- Assets/TextMesh Pro
- Third-party UI text library (shaders, fonts, materials).
- Assets/docs
- User documentation.
- ProjectSettings & Packages
- Unity build and package metadata.

##### Main Architectural Components
- Unity Runtime (Engine & UI)
- Scene(s) host RectTransforms into which editors are injected.
- NodeEditor (inherits MonoBehaviour)
- Public API: CreateGraph<T>(), StartEditor(graph)
- Manages the lifecycle of one NodeGraph instance, camera (pan/zoom), input routing, context-menu display.
- NodeGraph
- Data model representing nodes, sockets, and connections.
- Emits events via SignalSystem/Events when user interacts (pointer click/drag on graph, nodes, connections).
- Node / Socket Classes
- Node: base class for all graph nodes (Setup(), OnSerialize(), OnDeserialize()).
- SocketInput/Output: typed connection points registered to a node.
- ContextMenuBuilder
- Constructs nested context-menu options bound to graph/node/connection event handlers.
- Serializer
- Converts NodeGraph state to/from JSON (using UnityEngine.JsonUtility under the hood).
- Examples Layer
- ExampleNodeEditor: extends NodeEditor, hooks into Events, adds custom context-menu entries, demonstrates graph load/save.
- Example nodes (FloatNode, MathOperationNode, ColorDisplayNode).
- Persistence
- Graph JSON files in Resources folder and Application.dataPath for saving/loading.

##### Component Relationships and Data Flows
- User → Unity UI (click/drag) → NodeEditor input handlers
- NodeEditor → SignalSystem → NodeGraph / ContextMenuBuilder
- NodeGraph updates internal model (nodes, connections) → dispatches update events → NodeEditor redraws UI via Prefabs and BezierCurveDrawer
- Node/Socket registration → Serializer stores node parameters → JSON file system
- ApplicationStartup in examples wires everything together: on Start(), call editor.CreateGraph() → editor.StartEditor()

##### Architectural Patterns & Principles
- Modular package: “RuntimeNodeEditor” separated from “Examples” for clear boundaries.
- Event-driven: decouples UI, graph model, and user logic via Events.On… callbacks.
- Template-based UI: Prefabs drive node and socket visuals, allowing full customization.
- Serialization interface: Nodes implement OnSerialize/OnDeserialize for extensible persistence.

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% External Systems
  subgraph "External Systems"
    direction TB
    UE["Unity Engine & UI"]:::external
    FS["File System / Resources"]:::external
  end

  %% RuntimeNodeEditor Core
  subgraph "RuntimeNodeEditor" 
    direction TB
    NE["NodeEditor"]:::core
    SS["SignalSystem"]:::core
    NG["NodeGraph"]:::core
    GPL["GraphPointerListener"]:::core
    BCD["BezierCurveDrawer"]:::core
    LRR["UILineRenderer / UIPrimitiveBase / LinePointerListener"]:::core
    NB["Node (base)"]:::core
    NDP["NodeDraggablePanel"]:::core
    SCK["Socket Classes"]:::core
    SER["Serializer"]:::persistence
    CM["ContextMenuBuilder & Subsystem"]:::core
    UT["Utility & Types Helpers"]:::core
    PF["Prefab Templates"]:::core
    IM["Imported Assets"]:::external
  end

  %% Examples Layer
  subgraph "Examples Layer"
    direction TB
    CE1["ColorNodeEditor"]:::examples
    CDSN["ColorDisplayNode"]:::examples
    CES["ColorEditorStarter"]:::examples
    SE2["SimpleMathEditor"]:::examples
    ASU["ApplicationStartup"]:::examples
    FN["FloatNode"]:::examples
    MON["MathOperationNode"]:::examples
  end

  %% Relationships
  UE -->|"UI Events"| NE
  NE -->|"dispatches events"| SS
  SS -->|"triggers"| NG
  NE -->|"renders via"| BCD
  NE -->|"listens via"| GPL
  NE -->|"uses prefabs"| PF
  NG -->|"updates model"| CM
  NG -->|"provides data"| BCD
  NG -->|"serialize/deserialize"| SER
  NG -->|"notifies"| NE
  CM -->|"builds menus"| NE
  SER -->|"reads/writes JSON"| FS
  IM --> PF

  %% Examples integration
  CE1 -->|"extends"| NE
  CDSN --> CE1
  CES --> CE1
  SE2 -->|"extends"| NE
  ASU --> SE2
  FN --> SE2
  MON --> SE2

  %% Click Events
  click NE "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/NodeEditor/NodeEditor.cs"
  click SS "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/NodeEditor/SignalSystem.cs"
  click NG "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Graph/NodeGraph.cs"
  click BCD "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Graph/BezierCurveDrawer.cs"
  click GPL "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Graph/GraphPointerListener.cs"
  click LRR "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Line/UILineRenderer.cs"
  click LRR "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Line/UIPrimitiveBase.cs"
  click LRR "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Line/LinePointerListener.cs"
  click NB "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Node/Node.cs"
  click NDP "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Node/NodeDraggablePanel.cs"
  click SCK "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Socket/Connection.cs"
  click SCK "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Socket/Socket.cs"
  click SCK "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Socket/SocketInput.cs"
  click SCK "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Socket/SocketOutput.cs"
  click SCK "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Socket/SocketHandle.cs"
  click SCK "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Socket/IOutput.cs"
  click SER "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Serializer/Serializer.cs"
  click SER "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Serializer/SerializationData.cs"
  click CM "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/ContextMenu/ContextMenuBuilder.cs"
  click CM "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/ContextMenu/ContextMenu.cs"
  click CM "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/ContextMenu/ContextContainer.cs"
  click CM "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/ContextMenu/ContextItem.cs"
  click UT "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Utility.cs"
  click UT "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/RuntimeNodeEditor/Scripts/Types.cs"
  click PF "https://github.com/cemuka/unityruntimenodeeditor/tree/master/Assets/RuntimeNodeEditor/Prefabs/Templates"
  click IM "https://github.com/cemuka/unityruntimenodeeditor/tree/master/Assets/RuntimeNodeEditor/Imported"

  click CE1 "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/Examples/1_ColorEditor/Scripts/ColorNodeEditor.cs"
  click CDSN "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/Examples/1_ColorEditor/Scripts/ColorDisplayNode.cs"
  click CES "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/Examples/1_ColorEditor/Scripts/ColorEditorStarter.cs"
  click SE2 "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/Examples/2_SimpleMathEditor/Scripts/ExampleNodeEditor.cs"
  click ASU "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/Examples/2_SimpleMathEditor/Scripts/ApplicationStartup.cs"
  click FN "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/Examples/2_SimpleMathEditor/Scripts/FloatNode.cs"
  click MON "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/Examples/2_SimpleMathEditor/Scripts/MathOperationNode.cs"
  click FS "https://github.com/cemuka/unityruntimenodeeditor/blob/master/Assets/Examples/2_SimpleMathEditor/Resources/Nodes/graph.json"

  %% Styles
  classDef core fill:#D0E8FF,stroke:#0366D6,color:#000;
  classDef examples fill:#DFFFE0,stroke:#28A745,color:#000;
  classDef external fill:#F0F0F0,stroke:#888,color:#000;
  classDef persistence fill:#FFF4D0,stroke:#B58900,color:#000;
```
