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.
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();
[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.