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 on how to implement node options, refer to Implement node options. For information about node types, refer to About nodes.
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.
To implement a node, you must define a class, define ports, and add the node to the graph.
To define a class:
Node base class.UseWithGraph attribute. The node’s class name serves as the node’s title in the interface.[Serializable]
public class BasicNode : Node
{
}
[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();
}
}
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.
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. The omission of a type creates connection-only ports that link nodes but don’t transfer data.
Tip: Use optional methods such as WithDisplayName or WithConnectorUI to customize your port.
context.AddInputPort<int>("a")
.WithDisplayName("My Int")
.WithConnectorUI(PortConnectorUI.Arrowhead)
.Build();
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.
[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();
}
}
To instantiate a node:
The node appears on the canvas.