- Notifications
You must be signed in to change notification settings - Fork70
Unity runtime node editor using with Unity UI.
License
cemuka/UnityRuntimeNodeEditor
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Almost every node editor made in unity is using unity editor to make it.
My goal was make it in runtime with unity ui.
- Socket based connection
- Load and save a graph using node serializer
- Context Menu for graph, nodes and connections
- Event based notification
- Pan and zoom
- Multiple editors can be spawned in a scene
- Create graph by api or custom prefab
- Legacy and new Input System
Unity version 2021.3.3f1
RGB color display example
Simply extend theNodeEditor.
publicclassExampleNodeEditor:NodeEditor{publicoverridevoidStartEditor(NodeGraphgraph){base.StartEditor(graph);// make your custom initialization here}}
Create graph using the api, graph will stretch to holder object. (no prefab involves)
publicclassApplicationStartup:MonoBehaviour{publicRectTransformeditorHolder;publicExampleNodeEditoreditor;// asigned in unity from hierarchyprivatevoidStart(){vargraph=editor.CreateGraph<NodeGraph>(editorHolder);// var graph = editor.CreateGraph<NodeGraph>(editorHolder, bgColor, connColor);editor.StartEditor(graph);}}
You may want to use your own custom graph and prefab as well.
publicclassApplicationStartup:MonoBehaviour{publicRectTransformeditorHolder;publicExampleNodeEditoreditor;privatevoidStart(){editor.StartEditor(graph);}}
Graph actions are event based.
You'll find a complete example in the Example folder. Let's walkthrough over.
Listen events from editor
publicclassExampleNodeEditor:NodeEditor{privatestring_savePath;publicoverridevoidStartEditor(NodeGraphgraph){base.StartEditor(graph);_savePath=Application.dataPath+"/Example/Resources/graph.json";Events.OnGraphPointerClickEvent+=OnGraphPointerClick;Events.OnGraphPointerDragEvent+=OnGraphPointerDrag;Events.OnNodePointerClickEvent+=OnNodePointerClick;Events.OnConnectionPointerClickEvent+=OnNodeConnectionPointerClick;}}
- graph context menu
protectedoverridevoidOnGraphPointerClick(PointerEventDataeventData){switch(eventData.button){casePointerEventData.InputButton.Right:{varctx=newContextMenuBuilder().Add("nodes/float",CreateFloatNode).Add("nodes/math op",CreateMatOpNode).Add("graph/load",()=>LoadGraph(_savePath)).Add("graph/save",()=>SaveGraph(_savePath)).Build();SetContextMenu(ctx);DisplayContextMenu();}break;casePointerEventData.InputButton.Left:CloseContextMenu();break;}}
- node context menu
protectedoverridevoidOnNodePointerClick(Nodenode,PointerEventDataeventData){if(eventData.button==PointerEventData.InputButton.Right){varctx=newContextMenuBuilder().Add("duplicate",()=>DuplicateNode(node)).Add("clear connections",()=>ClearConnections(node)).Add("delete",()=>DeleteNode(node)).Build();SetContextMenu(ctx);DisplayContextMenu();}}
- connection context menu
protectedoverridevoidOnNodeConnectionPointerClick(stringconnId,PointerEventDataeventData){if(eventData.button==PointerEventData.InputButton.Right){varctx=newContextMenuBuilder().Add("clear connection",()=>DisconnectConnection(connId)).Build();SetContextMenu(ctx);DisplayContextMenu();}}
That's been said, to create a new node:
publicclassMyAwesomeNode:Node{publicTMP_InputFieldvalueField;// added from editorpublicSocketOutputoutputSocket;// added from editorpublicSocketInputinputSocket;// added from editorpublicoverridevoidSetup(){Register(outputSocket);Register(inputSocket);SetHeader("float");}publicoverridevoidOnSerialize(Serializerserializer){// save values on graph saveserializer.Add("floatValue",valueField.text);// it would be good idea to use JsonUtility for complex data}publicoverridevoidOnDeserialize(Serializerserializer){// load values on graph loadvarvalue=serializer.Get("floatValue");valueField.SetTextWithoutNotify(value);}}
To create a node from your editor, pass its path fromResources folder.
// context item actionsprivatevoidCreateMyNode(){graph.Create("Prefabs/Nodes/MyAwesomeNode");// your prefab path in resources}
Check out the complete expample inExampleScene for more details.
This project is actively in development.Feel free to drop an issue for any suggestion or feedback.
Wanna support me? Please, consider checkin out my new projectknightborn.
MIT
Copyright (c) 2022 Cem Ugur Karacam
About
Unity runtime node editor using with Unity UI.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.




