- Notifications
You must be signed in to change notification settings - Fork 435
Node scripting API
To create a new node, you have an option in the context menu Create/Node C# Script which will create an empty node from this template:
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingGraphProcessor;usingSystem.Linq;[System.Serializable,NodeMenuItem("Custom/#NAME#")]publicclass #SCRIPTNAME# :BaseNode{[Input(name="In")]publicfloatinput;[Output(name="Out")]publicfloatoutput;publicoverridestringname=>"#NAME#";protectedoverridevoidProcess(){output=input*42;}}Input is used to create an input port so we can connect outputs from other nodes, there is two parameters: the name which will be used to display the label beside the port and is it allows multiple input connection.Output takes only one parameter: the name of the port.NodeMenuItem placed on the node class is used to record the node in the NodeProvider class so we can use it to populate a menu to create new nodes (example here)
To create a new node view use the context menu option Create/Node View C# Script, it will generate a class from this template:
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEditor;usingUnityEditor.Experimental.UIElements;usingUnityEditor.Experimental.UIElements.GraphView;usingUnityEngine.Experimental.UIElements;usingGraphProcessor;[NodeCustomEditor(typeof(NODE_TYPE))]publicclass #SCRIPTNAME# :BaseNodeView{publicoverridevoidEnable(){varnode=nodeTargetasNODE_TYPE;// Create your fields using node's variables and add them to the controlsContainercontrolsContainer.Add(newLabel("Hello World !"));}}The NodeCustomEditor allow you to override the default inspector of the nodes and gives you access to the containers of the Node class plus some other useful such as controlsContainer which is the body of the node and debugContainer for debug.
DoubleFieldd=newDoubleField();d.OnValueChanged((v)=>{owner.RegisterCompleteObjectUndo("Updated floatNode output");// Important: Update the value in the node after registering the undofloatNode.output=(float)v.newValue;});Each serializable parameters of the node is saved as JSON in the graph asset so if you use structs or custom classes be sure that they are tagged serializable.Input or Output are not serialized, if you want them to be serialized add SerializeField to their attributes.
Currently the only thing you can change about style is the color of the ports (and so their links). You can define in a file named PortViewTypes.uss in a Resources folder custom port colors to use per field type (example here)
When you create a custom view for your nodes, you can add debug information into the debugContainer like so:
[NodeCustomEditor(typeof(ParameterNode))]publicclassParameterNodeView:BaseNodeView{ParameterNodeparameterNode;LabeldebugLabel;publicoverridevoidEnable(){parameterNode=nodeTargetasParameterNode;debugLabel=newLabel("Debug info");debugContainer.Add(debugLabel);}}Then it can be displayed by toggling the Debug option in the context menu of the node:

It is especially useful to debug runtime information after a graph have finished processing or just to use as a developer feature.