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:
- Define a class that inherits from the base class.
Node - 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 attribute. The node's class name serves as the node's title in the interface.
UseWithGraph
[Serializable] public class BasicNode : Node { }
Define the ports for the node
To configure the data connections for the node, override the method and use the and methods of the interface.
OnDefinePortsAddInputPortAddOutputPortIPortDefinitionContextprotected override void OnDefinePorts(IPortDefinitionContext context) { context.AddInputPort("Input").Build(); context.AddInputPort<int>("a").Build(); context.AddOutputPort("Output").Build(); context.AddOutputPort<int>("result").Build(); }
context.AddInputPort<int>("a") .WithDisplayName("My Int") .WithConnectorUI(PortConnectorUI.Arrowhead) .Build();
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 in the port builder chain before .
AsVerticalBuild[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 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.
AsVerticalNode 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(); } }