Class GraphDatabase
Provides functionality needed to access, and perform operations on, graph assets.
Inherited Members
Namespace: Unity.GraphToolkit.Editor
Assembly: Unity.GraphToolkit.Editor.dll
Syntax
public static class GraphDatabase
Remarks
The GraphDatabase
class is similar to Unity's AssetDatabase, but it is tailored for graph-based tools.
Use this class to create, load, and save Graph instances and their associated assets.
This API supports typical asset workflows such as creating new graph assets in the Project window, accessing graphs by path or GUID,
and ensuring changes to graph data are saved.
Use PromptInProjectBrowserToCreateNewAsset<T>(string) to create and name a new asset,
CreateGraph<T>(string) to generate an asset file, and LoadGraph<T>(string) to retrieve an existing one.
Use SaveGraphIfDirty(Graph) to persist graph data changes, and LoadGraphForImporter<T>(string) to load a clean instance during import.
Methods
CreateGraph<T>(string)
Creates a new graph asset of type T
at the specified file path.
Declaration
public static T CreateGraph<T>(string assetPath) where T : Graph, new()
Parameters
Type | Name | Description |
---|---|---|
string | assetPath | The relative path for the new asset (e.g., "Assets/Graphs/MyGraph.mygraph"). |
Returns
Type | Description |
---|---|
T | The created graph instance. |
Type Parameters
Name | Description |
---|---|
T | The type of the Graph to create. Must inherit from Graph and have a public parameterless constructor. |
Remarks
Use this method to programmatically create a new graph asset of type T
at a specific location in the project.
The path must be relative to the Unity project folder and must include a valid file extension recognized by the graph importer.
If an asset already exists at the specified assetPath
, this method overwrites it.
This method works similarly to CreateAsset(Object, string) but is scoped for Graph assets.
GetGraphAssetGUID(Graph)
Retrieves the globally unique identifier (GUID) for the asset associated with the specified Graph.
Declaration
public static GUID GetGraphAssetGUID(Graph graph)
Parameters
Type | Name | Description |
---|---|---|
Graph | graph | The graph whose asset GUID you want to retrieve. |
Returns
Type | Description |
---|---|
GUID | The UnityEditor.GUID of the graph asset. |
Remarks
Use this method to get a persistent identifier for a graph asset. The UnityEditor.GUID allows reliable tracking, referencing, and linking to graph assets across different Unity sessions.
GetGraphAssetPath(Graph)
Retrieves the file path of the asset associated with the specified Graph.
Declaration
public static string GetGraphAssetPath(Graph graph)
Parameters
Type | Name | Description |
---|---|---|
Graph | graph | The graph whose asset path you want to retrieve. |
Returns
Type | Description |
---|---|
string | The asset's file path. |
Remarks
Use this method to get the relative file path of the graph asset within the Unity project (for example,
"Assets/Graphs/MyGraph.mygraph"
). Do not use AssetDatabase.GetAssetPath
with graph objects, as it will not return the correct result.
LoadGraphForImporter<T>(string)
Loads a fresh instance of the Graph of type T
from disk for use in the asset import pipeline.
Declaration
public static T LoadGraphForImporter<T>(string assetPath) where T : Graph
Parameters
Type | Name | Description |
---|---|---|
string | assetPath | The path to the graph asset file. |
Returns
Type | Description |
---|---|
T | A new instance of the graph read directly from disk. |
Type Parameters
Name | Description |
---|---|
T | The type of graph to load. |
Remarks
Use this method to load a Graph instance from disk without referencing the in-memory version. This ensures consistent, deterministic behavior required by Unity’s asset import pipeline. This method is intended for importers, it bypasses any in-memory modifications that may have occurred. Unlike LoadGraph<T>(string), this method always returns a clean copy of the graph as it exists on disk.
LoadGraph<T>(string)
Loads a Graph of type T
from the asset at the specified path.
Declaration
public static T LoadGraph<T>(string assetPath) where T : Graph
Parameters
Type | Name | Description |
---|---|---|
string | assetPath | The relative path to the graph asset (for example, "Assets/Graphs/MyGraph.mygraph"). |
Returns
Type | Description |
---|---|
T | The loaded graph instance, or |
Type Parameters
Name | Description |
---|---|
T | The type of Graph to load. |
Remarks
Use this method to load a graph asset of type T
from a given asset path.
The assetPath
must be relative to the Unity project folder.
This method returns the graph object currently loaded in memory, which might differ from the version on disk if the asset was modified
or opened in an editor. This behavior is similar to LoadAssetAtPath(string, Type).
For deterministic loading during import, use LoadGraphForImporter<T>(string) instead.
PromptInProjectBrowserToCreateNewAsset<T>(string)
Creates a new graph asset and activates the naming field in the Project Browser.
Declaration
public static void PromptInProjectBrowserToCreateNewAsset<T>(string defaultName = "New Graph") where T : Graph, new()
Parameters
Type | Name | Description |
---|---|---|
string | defaultName | The default name for the new asset if the user does not rename it. Defaults to "New Graph" if not specified. |
Type Parameters
Name | Description |
---|---|
T | The type of graph to create. Must inherit from Graph and have a public parameterless constructor. |
Remarks
Use this method to create a new graph asset directly from the editor UI.
This action opens the Project Browser with the asset selected and its name field ready for editing.
If the user does not provide a name, the system uses the value from defaultName
.
This method streamlines asset creation by combining instantiation and naming in one step.
SaveGraphIfDirty(Graph)
Saves the asset of the specified Graph to disk if it has unsaved changes.
Declaration
public static void SaveGraphIfDirty(Graph graph)
Parameters
Type | Name | Description |
---|---|---|
Graph | graph | The graph to save. |
Remarks
Use this method to persist any pending modifications made to a Graph instance. It prevents data loss by ensuring the asset on disk reflects the in-memory graph state. This method is similar to SaveAssetIfDirty(Object) and only performs a save if the graph is marked dirty.