Version: Unity 6.5 Alpha (6000.5)
Language : English
About nodes
Implement context nodes for your graph tool

Implement nodes for your graph tool

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.

Prerequisites

  1. Set up the code structure.
  2. Implement a graph tool.

Implement and instantiate a node

To implement a node, you must define a class, define ports, and add the node to the graph.

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
{
}
[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.

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

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();
    }
}

Instantiate the node

To instantiate a node:

  1. Right-click on the graph canvas, and select Add Node to open the Add a graph node window.
  2. Navigate to the Nodes category.
  3. Double-click on the node name to add it to the graph.

The node appears on the canvas.

Additional resources

About nodes
Implement context nodes for your graph tool