Skip to content

Graph Editors

Thor Brigsted edited this page Dec 6, 2022 · 18 revisions
class NodeGraphEditor : NodeEditorBase

What?

Every graph is drawn with a GraphEditor. Just like with Unity's custom editors, you can override a lot of the functionality that draws the graph.

How?

Creating custom node graph editors is very similar to creating custom inspectors for other classes.

Node graph editors inherit from NodeGraphEditor and have a [CustomNodeGraphEditor(System.Type type)] attribute. They can not be abstract.

Editor scripts also need to be in an editor folder and won't be included in build.

[CustomNodeGraphEditor(typeof(MyGraph))]
public class MyGraphEditor : NodeGraphEditor {

}

Tips

Filter which nodes are shown in creation menu GetNodeMenuName override lets you specify the path of a specified node type, but it also lets you hide it by returning null. To filter all but nodes deriving from a specific base class you can use this snippet.
public override string GetNodeMenuName(Type type) {
    if (typeof(MyBaseNode).IsAssignableFrom(type)) {
        return base.GetNodeMenuName(type);
    } else return null;
}
Store preferences specific to your node graph or project
  1. Add a unique EditorPrefs key to the attribute
[CustomNodeGraphEditor(typeof(MyGraph), "MyGraph.Settings")]
  1. Override GetDefaultPreferences(); to set default preferences
public override NodeEditorPreferences.Settings GetDefaultPreferences() {
    return new NodeEditorPreferences.Settings() {
        gridBgColor = Color.black,
        gridLineColor = Color.white,
        typeColors = new Dictionary<string, Color>() {
            { typeof(string).PrettyName(), Color.yellow },
            { typeof(MyType).PrettyName(), new Color(1,0.5f,0.6f) }
        }
    };
}
Repaint each frame

Sometimes you may want your nodes to repaint every frame. This is achievable though spamming 'repaint me' in OnGUI. Don't worry, it won't freeze :)

public override void OnGUI() {
    // Keep repainting the GUI of the active NodeEditorWindow
    NodeEditorWindow.current.Repaint();
}
Clone this wiki locally