docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class Graph

    Represents the core definition of a graph and defines its behavior.

    Inheritance
    object
    Graph
    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 class Graph
    Remarks

    Graph serves as the central entry point for:

    • Lifecycle management (via OnEnable(), OnDisable())
    • Change tracking (via OnGraphChanged(GraphLogger))
    • Access to nodes and variables
    To register a graph type and associate it with a custom file extension and configuration options, apply the GraphAttribute to your custom Graph class.

    You can further control the graph's behavior using the GraphOptions enum, which defines traits such as support for subgraphs. If your graph supports subgraphs (via SupportsSubgraphs), you can declare valid subgraph types using the SubgraphAttribute.

    Use the GraphDatabase utility class to create, load, and save graph assets in the Unity Editor. Graphs are serialized assets. You can create them through the editor UI with PromptInProjectBrowserToCreateNewAsset<T>(string) or load them from disk with LoadGraph<T>(string).

    Properties

    name

    The name of the graph.

    Declaration
    public string name { get; }
    Property Value
    Type Description
    string

    nodeCount

    The number of INodes in the graph.

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

    variableCount

    The number of IVariables declared in the graph.

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

    Methods

    GetNode(int)

    Retrieves a node defined in the graph by its index.

    Declaration
    public INode GetNode(int index)
    Parameters
    Type Name Description
    int index

    The zero-based index of the node to retrieve.

    Returns
    Type Description
    INode

    The INode at the specified index.

    Remarks

    Use this method to access a node based on its creation order in the graph. The index is zero-based and must be within range (see: nodeCount).
    The list includes:

    • Your own Nodes
    • ContextNodes
    • IVariableNodes
    • IConstantNodes
    • ISubgraphNodes
    It excludes BlockNodes, which are only accessible through their parent ContextNode.

    GetNodes()

    Retrieves all nodes in the graph.

    Declaration
    public IEnumerable<INode> GetNodes()
    Returns
    Type Description
    IEnumerable<INode>

    An IEnumerable of all INodes in the graph.

    Remarks

    Use this method to access every node in the graph. Nodes are returned in the order they were created.
    The list includes:

    • Your own Nodes
    • ContextNodes
    • IVariableNodes
    • IConstantNodes
    • ISubgraphNodes
    It excludes BlockNodes, which are only accessible through their parent ContextNode.

    GetVariable(int)

    Retrieves a variable declared in the graph by index.

    Declaration
    public IVariable GetVariable(int index)
    Parameters
    Type Name Description
    int index

    The index of the variable to retrieve.

    Returns
    Type Description
    IVariable

    The IVariable at the specified index.

    Remarks

    Use this method to access a specific IVariable from the list of variables declared in the graph. This list does not include variable nodes that reference variables. The index is zero-based and reflects the order in which the variables were created. The index must be within the valid range of the variable list (see: variableCount).

    GetVariables()

    Retrieves all variables declared in the graph.

    Declaration
    public IEnumerable<IVariable> GetVariables()
    Returns
    Type Description
    IEnumerable<IVariable>

    An IEnumerable of all IVariables declared in the graph.

    Remarks

    Use this method to enumerate all IVariables declared in the graph. This list does not include variable nodes that reference variables. The collection reflects the variables as declared, in their order of creation.

    OnDisable()

    Called when the graph is unloaded, or goes out of scope in the editor.

    Declaration
    public virtual void OnDisable()
    Remarks

    Unity calls this method when the graph is disabled, or is destroyed. Override this method to release resources, clear temporary data, or perform any required cleanup. This method complements OnEnable() and is useful for maintaining consistency across editor sessions.

    OnEnable()

    Called when the graph is created or loaded in the editor.

    Declaration
    public virtual void OnEnable()
    Remarks

    Override this method to perform setup tasks, such as allocating resources, initializing internal state, or preparing data for editing. This method is invoked each time the graph becomes active in the editor, including after domain reload or when reopening the asset. This method complements OnDisable() and is useful for maintaining consistency across editor sessions.

    OnGraphChanged(GraphLogger)

    Called after the graph has changed.

    Declaration
    public virtual void OnGraphChanged(GraphLogger graphLogger)
    Parameters
    Type Name Description
    GraphLogger graphLogger

    The GraphLogger that receives any errors or warnings related to the graph.

    Remarks

    Unity calls this method after any change to the graph. Override it to validate the graph's integrity and report issues using the provided GraphLogger. Use this method to detect invalid configurations, highlight issues in the editor, or provide user feedback. Do not modify the graph within this method, as it may cause instability or recursive updates.

    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)