Skip to content

Node scripting API

Antoine Lelievre edited this page May 23, 2019 · 3 revisions

New node

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GraphProcessor;
using System.Linq;

[System.Serializable, NodeMenuItem("Custom/#NAME#")]
public class #SCRIPTNAME# : BaseNode
{
	[Input(name = "In")]
    public float                input;

	[Output(name = "Out")]
	public float				output;

	public override string		name => "#NAME#";

	protected override void Process()
	{
	    output = input * 42;
	}
}

Attributes

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)

Custom view

To create a new node view use the context menu option Create/Node View C# Script, it will generate a class from this template:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Experimental.UIElements;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine.Experimental.UIElements;
using GraphProcessor;

[NodeCustomEditor(typeof(NODE_TYPE))]
public class #SCRIPTNAME# : BaseNodeView
{
	public override void Enable()
	{
		var node = nodeTarget as NODE_TYPE;

        // Create your fields using node's variables and add them to the controlsContainer

		controlsContainer.Add(new Label("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.

⚠️ If you override the default inspector you'll have to handle yourself the undo/redo of the fields you draw, here is an example how to record a modification of a node field:

DoubleField d = new DoubleField();

d.OnValueChanged((v) => {
	owner.RegisterCompleteObjectUndo("Updated floatNode output");
	// Important: Update the value in the node after registering the undo
	floatNode.output = (float)v.newValue;
});

Serialization

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.
⚠️ By default fields marked with Input or Output are not serialized, if you want them to be serialized add SerializeField to their attributes.

Style

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)

Debug

When you create a custom view for your nodes, you can add debug information into the debugContainer like so:

[NodeCustomEditor(typeof(ParameterNode))]
public class ParameterNodeView : BaseNodeView
{
	ParameterNode	parameterNode;
	Label           debugLabel;

	public override void Enable()
	{
		parameterNode = nodeTarget as ParameterNode;

		debugLabel = new Label("Debug info");
		debugContainer.Add(debugLabel);
	}
}

Then it can be displayed by toggling the Debug option in the context menu of the node: image
It is especially useful to debug runtime information after a graph have finished processing or just to use as a developer feature.

Clone this wiki locally