How cemuka/unityruntimenodeeditor works
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.