Documentation

Unity Engine

Unity Engine

Implement nodes for your graph tool

Create custom node types with input and output ports.
Read time 3 minutesLast updated a month ago

Create custom node types with input and output ports.
Nodes are the fundamental building blocks of any graph. Standard nodes can have input and output ports to connect with other nodes.
This page covers how to implement nodes only. For information about node types, refer to About nodes. For information on how to implement node options, refer to Implement node options.

Prerequisites

Before you implement nodes, you need to have created a graph class. If you haven't done this yet, follow the instructions in Implement a graph tool.

Implement a node

To implement a node, you need to define a class, and define ports.

Define a class

To define a class:
  1. Define a class that inherits from the
    Node
    base class.
  2. To ensure automatic discovery, define the node class in the same assembly as the graph tool. You can also link a node to a graph tool if you decorate the node class with the
    UseWithGraph
    attribute. The node's class name serves as the node's title in the interface.
[Serializable] public class BasicNode : Node { }
Note
The nodes include a
Serializable
attribute. This attribute enables the tool to write the nodes into the graph asset and deserialize them when you open the graph.

Define the ports for the node

To configure the data connections for the node, override the
OnDefinePorts
method and use the
AddInputPort
and
AddOutputPort
methods of the
IPortDefinitionContext
interface.
protected override void OnDefinePorts(IPortDefinitionContext context) { context.AddInputPort("Input").Build(); context.AddInputPort<int>("a").Build(); context.AddOutputPort("Output").Build(); context.AddOutputPort<int>("result").Build(); }
Note
Ports support any Unity-compatible type, including your custom types. To implement a port that doesn't specify a data type, use
ctx.AddInputPort<Untyped>()
or
ctx.AddInputPort()
.
Tip
Use optional methods such as
WithDisplayName
or
WithConnectorUI
to customize your port.
context.AddInputPort<int>("a") .WithDisplayName("My Int") .WithConnectorUI(PortConnectorUI.Arrowhead) .Build();
Note
In Unity versions 6.6 and newer
port.DataType
is always assigned. If you don't pass a specific type,
port.DataType
is
Untyped
.

Add vertical ports

By default, input ports appear on the left of the node and output ports appear on the right. To place ports vertically (input ports on the top of the node and output ports on the bottom), call
AsVertical
in the port builder chain before
Build
.
[Serializable] public class NodeWithVerticalPorts : Node { protected override void OnDefinePorts(IPortDefinitionContext context) { context.AddInputPort("Vertical Input") .AsVertical() .WithTooltip("Vertical input") .Build(); context.AddOutputPort("Vertical Output") .AsVertical() .WithTooltip("Vertical output") .Build(); } }
Call
AsVertical
on each port you want to orient vertically. You can mix vertical and horizontal ports on the same node. Vertical ports can connect to horizontal ports of the same type, and vice versa.
Note
Vertical ports don't display their labels on the node. Hovering over a connector displays a tooltip that shows the port name and type. You can use
WithTooltip
to customize the tooltip.
Note
Vertical ports aren't supported on block nodes. Block node ports always display horizontally. For more information on block nodes, refer to Implement block nodes for your graph tool.

Node complete example

[Serializable] public class BasicNodeFinal : Node { protected override void OnDefinePorts(IPortDefinitionContext context) { context.AddInputPort("Input").Build(); context.AddInputPort<int>("a").Build(); context.AddOutputPort("Output").Build(); context.AddOutputPort<int>("result").Build(); } }

Additional resources