Create and implement a graph tool to visualize and manage complex data structures in Unity.
The foundation of any Graph Toolkit tool is a custom Graph class. To create one, do the following:
Create a new C# script file.
Import the necessary namespaces:
using System;
using UnityEditor;
using UnityEngine;
using Unity.GraphToolkit.Editor;
Define a class that inherits from Graph.
Define an AssetExtension string constant to specify your graph’s file extension.
Apply the [Graph] attribute, and pass your extension as a parameter.
Mark your class with [Serializable] to ensure proper data persistence as you extend your graph tool.
Add a new CreateAssetFile function that calls the PromptInProjectBrowserToCreateNewAsset method, and decorate this function with a MenuItem attribute.
[Graph(AssetExtension)]
[Serializable]
class MySimpleGraph : Graph
{
public const string AssetExtension = "simpleg";
[MenuItem("Assets/Create/Graph Toolkit Samples/Simple Graph", false)]
static void CreateAssetFile()
{
GraphDatabase.PromptInProjectBrowserToCreateNewAsset<MySimpleGraph>();
}
}
This basic implementation creates the core structure for your graph tool. The AssetExtension constant defines the file format Unity uses to save your graph assets.
Create a new graph asset directly from the Unity Editor’s menu. For example, navigate to Assets > Create > Graph Toolkit Samples > Simple Graph. The new asset appears in the Assets section of the Project window with its name ready for you to enter.
Name the asset.
Double-click the graph asset file. The Graph window opens with an empty canvas.