Version: Unity 6.5 (6000.5)
Language : English
Add subgraph support
Working with graphs

Introduction to the Graph Processor

Use OnGraphChanged and GraphLogger to provide feedback to users about the state of their graph, and guide them in making the necessary corrections. Unity calls OnGraphChanged automatically whenever a user modifies their graph. This allows you to validate the graph state in real-time and provide appropriate feedback. Unity displays any messages logged with GraphLogger in the Unity console.

Example

This example demonstrates how to use the graph processor to check the state of a graph and provide feedback. In this example, the graph needs to have exactly one node of the type SingleNode and no more. The graph processor checks that the graph contains at least one node of the SingleNode type. It logs an appropriate message if it finds that the graph contains either no SingleNode type nodes, or more than one SingleNode type node. The code sample is as follows:

[Serializable]
public class OnGraphChangedExampleGraph : Graph
{
    public override void OnGraphChanged(GraphLogger graphLogger)
    {
        var numberOfNodes = 0;
        // Count how many SingleNodes are in the graph
        foreach (var node in GetNodes())
        {
            if (node is SingleNode)
            {
                numberOfNodes++;
            }
        }

        if (numberOfNodes == 0)
        {
            // Log an error if there are no SingleNodes in the graph
            graphLogger.LogError("Node of type SingleNode not found. Graph must have one node of type SingleNode.");
        }
        else if (numberOfNodes > 1)
        {
            // Log an error if there is more than one SingleNode in the graph
            graphLogger.LogError("Multiple nodes of type SingleNode found. Graph must have only one node of type SingleNode.");
        }
    }
}

Additional resources

Add subgraph support
Working with graphs