docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class Node

    The base class for all user-accessible nodes in a graph.

    Inheritance
    object
    Node
    BlockNode
    ContextNode
    Implements
    INode
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.GraphToolkit.Editor
    Assembly: Unity.GraphToolkit.Editor.dll
    Syntax
    [Serializable]
    public abstract class Node : INode
    Remarks

    Inherit from this class to define custom node types that appear in the graph. The Node class provides lifecycle hooks, serialization support, and the structure needed to define ports, UI behaviors, and custom logic. This class forms the foundation of all user-defined nodes in a graph-based tool, including variable nodes, context nodes, and subgraph nodes.

    To create a custom node, derive from Node, define its input and output ports using a port builder in OnDefinePorts(IPortDefinitionContext), and define its INodeOptions in OnDefineOptions(IOptionDefinitionContext).

    This class is used in combination with other types like INode, IPort, and Graph to construct and manage node-based workflows.

    See also:

    • INode for the interface this class implements
    • ContextNode and BlockNode for composition patterns
    • IVariableNode for how to work with variable-based nodes
    • ISubgraphNode for how to work with subgraph-based nodes

    Properties

    inputPortCount

    The number of input ports on the node.

    Declaration
    public int inputPortCount { get; }
    Property Value
    Type Description
    int

    nodeOptionCount

    The number of node options defined in the node.

    Declaration
    public int nodeOptionCount { get; }
    Property Value
    Type Description
    int

    nodeOptions

    The node options defined on this node.

    Declaration
    public IEnumerable<INodeOption> nodeOptions { get; }
    Property Value
    Type Description
    IEnumerable<INodeOption>

    outputPortCount

    The number of output ports on the node.

    Declaration
    public int outputPortCount { get; }
    Property Value
    Type Description
    int

    Methods

    DefineNode()

    Defines the structure of the node by building its ports and options.

    Declaration
    public void DefineNode()
    Remarks

    This method calls both OnDefineOptions(IOptionDefinitionContext) and OnDefinePorts(IPortDefinitionContext) to allow custom definition of the node.

    GetInputPort(int)

    Retrieves an input port using its index.

    Declaration
    public IPort GetInputPort(int index)
    Parameters
    Type Name Description
    int index

    The index of the input port.

    Returns
    Type Description
    IPort

    The input port at the specified index.

    Remarks

    The index is zero-based. The list of input ports is ordered according to their display order in the node.

    GetInputPortByName(string)

    Retrieves an input port using its name.

    Declaration
    public IPort GetInputPortByName(string name)
    Parameters
    Type Name Description
    string name

    The unique name of the input port within this node.

    Returns
    Type Description
    IPort

    The input port with the specified name.

    Remarks

    The input port's name is unique within the node's input ports and node options.

    GetInputPorts()

    Retrieves all input ports on the node in the order they are displayed.

    Declaration
    public IEnumerable<IPort> GetInputPorts()
    Returns
    Type Description
    IEnumerable<IPort>

    An IEnumerable of input ports.

    GetNodeOption(int)

    Retrieves a node option using its zero-based index.

    Declaration
    public INodeOption GetNodeOption(int index)
    Parameters
    Type Name Description
    int index

    Index of the node option, based on display order.

    Returns
    Type Description
    INodeOption

    The node option at the specified index.

    Remarks

    The index is zero-based.

    Exceptions
    Type Condition
    IndexOutOfRangeException

    Thrown if the index is out of bounds.

    GetNodeOptionByName(string)

    Retrieves a node option using its name.

    Declaration
    public INodeOption GetNodeOptionByName(string name)
    Parameters
    Type Name Description
    string name

    The unique name of the node option.

    Returns
    Type Description
    INodeOption

    The node option with the specified name, or null if none is found.

    Remarks

    The node option's name is unique within the node's input ports and node options.

    GetOutputPort(int)

    Retrieves an output port using its index in the displayed order.

    Declaration
    public IPort GetOutputPort(int index)
    Parameters
    Type Name Description
    int index

    The zero-based index of the output port.

    Returns
    Type Description
    IPort

    The output port at the specified index.

    Remarks

    The index is zero-based. The list of output ports is ordered according to their display order in the node.

    GetOutputPortByName(string)

    Retrieves an output port using its name.

    Declaration
    public IPort GetOutputPortByName(string name)
    Parameters
    Type Name Description
    string name

    The unique name of the output port within this node.

    Returns
    Type Description
    IPort

    The output port with the specified name, or null if no match is found.

    Remarks

    The output port's name is unique within the node's output ports.

    GetOutputPorts()

    Retrieves all output ports on the node in the order they are displayed.

    Declaration
    public IEnumerable<IPort> GetOutputPorts()
    Returns
    Type Description
    IEnumerable<IPort>

    An IEnumerable of output ports.

    OnDefineOptions(IOptionDefinitionContext)

    Called during DefineNode() to define the options available on the node.

    Declaration
    protected virtual void OnDefineOptions(Node.IOptionDefinitionContext context)
    Parameters
    Type Name Description
    Node.IOptionDefinitionContext context

    Provides methods for defining node options.

    Remarks

    This method is called before OnDefinePorts(IPortDefinitionContext). Override this method to add node options using the provided Node.IOptionDefinitionContext.

    Examples
    protected override void OnDefineOptions(IOptionDefinitionContext context)
    {
        context.AddNodeOption<bool>(
            optionName: "My Bool",
            optionDisplayName: "myBoolId");
    
        context.AddNodeOption(
            optionName: "Label",
            dataType: typeof(string),
            optionDisplayName: "labelId",
            tooltip: "A label.",
            defaultValue: "Default Value");
    }

    OnDefinePorts(IPortDefinitionContext)

    Called during DefineNode() to define the input and output ports of the node.

    Declaration
    protected virtual void OnDefinePorts(Node.IPortDefinitionContext context)
    Parameters
    Type Name Description
    Node.IPortDefinitionContext context

    Provides methods for defining input and output ports.

    Remarks

    This method is called after OnDefineOptions(IOptionDefinitionContext) and is used to declare the structure of the node's connectivity. Use the provided Node.IPortDefinitionContext to define input and output ports using a builder pattern. The port builder pattern enables fluent configuration of ports by chaining methods such as WithDisplayName, WithDefaultValue, or WithConnectorUI, followed by Build() to finalize the port. The portName parameter passed to AddInputPort or AddOutputPort serves as the port's unique identifier. The portName parameter must be unique within its direction (input or output) on the node and is also used as the display name unless you explicitly call WithDisplayName. You also use the portName identifier to call GetInputPortByName(string).

    Examples
    protected override void OnDefinePorts(IPortDefinitionContext context)
    {
        var inputPort = context.AddInputPort<string>("stringInput")
            .WithDisplayName("String Input")
            .WithDefaultValue("Default Value")
            .Build();
    
        var outputPort = context.AddOutputPort("myOutput")
            .WithDisplayName("My Output Port")
            .WithDataType(typeof(float))
            .WithConnectorUI(PortConnectorUI.Arrowhead)
            .Build();
    }

    OnDisable()

    Called when the node is removed or when the graph is disabled.

    Declaration
    public virtual void OnDisable()
    Remarks

    Use this method to perform any cleanup logic.

    OnEnable()

    Called when the node is created or when the graph is enabled.

    Declaration
    public virtual void OnEnable()
    Remarks

    Use this method to perform initialization logic.

    Implements

    INode
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)