# Graph

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

## Definition

* **Type:** Class
* **Namespace:** [Unity.GraphToolkit.Editor](/engine/6000.5/script-reference/unity/graphtoolkit/editor.md)
* **Assembly:** UnityEditor

```csharp
public class Graph
```

## Remarks

`Graph` serves as the central entry point for:

* Lifecycle management (via [Graph.OnEnable](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/onenable.md), [Graph.OnDisable](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/ondisable.md))

* Change tracking (via [Graph.OnGraphChanged](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/ongraphchanged.md))

* Access to nodes and variables

To register a graph type and associate it with a custom file extension and configuration options, apply the [GraphAttribute](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graphattribute.md) to your custom `Graph` class.

You can further control the graph's behavior using the [GraphOptions](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graphoptions.md) enum, which defines traits such as support for subgraphs. If your graph supports subgraphs (via [GraphOptions.SupportsSubgraphs](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graphoptions/supportssubgraphs.md)), you can declare valid subgraph types using the [SubgraphAttribute](/engine/6000.5/script-reference/unity/graphtoolkit/editor/subgraphattribute.md).

Use the [GraphDatabase](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graphdatabase.md) 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 [GraphDatabase.PromptInProjectBrowserToCreateNewAsset](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graphdatabase/promptinprojectbrowsertocreatenewasset.md) or load them from disk with [GraphDatabase.LoadGraph](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graphdatabase/loadgraph.md).

## Properties

| Property                                                                                          | Description                                                                                                               |
| ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| [Name](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/name.md)                   | The name of the graph.                                                                                                    |
| [NodeCount](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/nodecount.md)         | The number of [INode](/engine/6000.5/script-reference/unity/graphtoolkit/editor/inode.md)s in the graph.                  |
| [VariableCount](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/variablecount.md) | The number of [IVariable](/engine/6000.5/script-reference/unity/graphtoolkit/editor/ivariable.md)s declared in the graph. |

## Methods

| Method                                                                                                                        | Description                                                                                                                                                                                                                                                                                                    |
| ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [AddNode](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/addnode.md)                                         | Adds a node to the Graph.                                                                                                                                                                                                                                                                                      |
| [AddSubgraphNode](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/addsubgraphnode.md)                         | Creates and adds a new subgraph node referencing an existing Graph asset.                                                                                                                                                                                                                                      |
| [AddVariableNode](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/addvariablenode.md)                         | Creates and adds a new variable node referencing an existing variable.                                                                                                                                                                                                                                         |
| [Connect](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/connect.md)                                         | Creates a wire connection between two ports.                                                                                                                                                                                                                                                                   |
| [CreateConstantNode](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/createconstantnode.md)                   | Creates and adds a new constant node to the graph.                                                                                                                                                                                                                                                             |
| [CreateLocalSubgraphNode](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/createlocalsubgraphnode.md)         | Creates and adds a new local subgraph node.                                                                                                                                                                                                                                                                    |
| [CreateVariable](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/createvariable.md)                           | Creates and adds a new variable to the graph.                                                                                                                                                                                                                                                                  |
| [Disconnect](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/disconnect.md)                                   | Removes the wire connection between two ports.                                                                                                                                                                                                                                                                 |
| [GetNode](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/getnode.md)                                         | Retrieves a node defined in the graph by its index.                                                                                                                                                                                                                                                            |
| [GetNodes](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/getnodes.md)                                       | Retrieves all nodes in the graph.                                                                                                                                                                                                                                                                              |
| [GetVariable](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/getvariable.md)                                 | Retrieves a variable declared in the graph by index.                                                                                                                                                                                                                                                           |
| [GetVariables](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/getvariables.md)                               | Retrieves all variables declared in the graph.                                                                                                                                                                                                                                                                 |
| [OnDefineSubgraphNodeOptions](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/ondefinesubgraphnodeoptions.md) | Called to define the [INodeOption](/engine/6000.5/script-reference/unity/graphtoolkit/editor/inodeoption.md)s available on subgraph nodes that reference this type of graph. Similar in function to [Node.OnDefineOptions](/engine/6000.5/script-reference/unity/graphtoolkit/editor/node/ondefineoptions.md). |
| [OnDisable](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/ondisable.md)                                     | Called when the graph is unloaded, or goes out of scope in the editor.                                                                                                                                                                                                                                         |
| [OnEnable](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/onenable.md)                                       | Called when the graph is created or loaded in the editor.                                                                                                                                                                                                                                                      |
| [OnGraphChanged](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/ongraphchanged.md)                           | Called after the graph has changed.                                                                                                                                                                                                                                                                            |
| [RemoveNode](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/removenode.md)                                   | Removes a node from the Graph.                                                                                                                                                                                                                                                                                 |
| [RemoveVariable](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/removevariable.md)                           | Removes a variable from the Graph.                                                                                                                                                                                                                                                                             |
| [UndoBeginRecordGraph](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/undobeginrecordgraph.md)               | Signals the beginning of an undoable operation.                                                                                                                                                                                                                                                                |
| [UndoEndRecordGraph](/engine/6000.5/script-reference/unity/graphtoolkit/editor/graph/undoendrecordgraph.md)                   | Signals the end of an undoable operation. Sends the undo data to the editor undo system and refreshes the graph view with the changes made.                                                                                                                                                                    |
