Version: Unity 6.5 Alpha (6000.5)
Language : English
Set up the code structure
About nodes

Implement a graph tool

Create and implement a graph tool to visualize and manage complex data structures in Unity.

Prerequisites

Set up the code structure

Define a custom Graph class

The foundation of any Graph Toolkit tool is a custom Graph class. To create one, do the following:

  1. Create a new C# script file.

  2. Import the necessary namespaces:

    using System;
    using UnityEditor;
    using UnityEngine;
    using Unity.GraphToolkit.Editor;
    
  3. Define a class that inherits from Graph.

  4. Define an AssetExtension string constant to specify your graph’s file extension.

  5. Apply the [Graph] attribute, and pass your extension as a parameter.

  6. Mark your class with [Serializable] to ensure proper data persistence as you extend your graph tool.

  7. 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.

  8. 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.

  9. Name the asset.

  10. Double-click the graph asset file. The Graph window opens with an empty canvas.

Additional resources

Set up the code structure
About nodes