Skip to content

Getting Started

Thor Brigsted edited this page Sep 5, 2019 · 18 revisions

Getting started with your own node system is quick and easy. Summarized, a graph class derives from NodeGraph, and node derive from Node. The graph contains a list of nodes, and nodes provide access to their ports and their connected nodes. What you do with these nodes, their connections, and whatnot, is up to your implementation.

Both graphs and nodes derive from ScriptableObject, allowing them to exist as assets inside your Project folder.

If you are looking for examples, I recommend you start with the examples branch or this list of opensource xNode plugins.

Core concepts

You need to get familiar with the following concepts:

Creating a NodeGraph

You need a NodeGraph before you can make any nodes. The NodeGraph is a very simple class mainly for manipulation and evaluation of the nodes inside.

A simple NodeGraph script can look like this:

// SimpleGraph.cs

[CreateAssetMenu]
public class SimpleGraph : NodeGraph { }

Tip: Quickly create a NodeGraph script by selecting Assets > Create > xNode > NodeGraph C# Script

The [CreateAssetMenu] lets you create an asset file in your Project window through Right-Click > Create > SimpleGraph. More info

Nothing else is required for the NodeGraph to work, simply open your node graph by double-clicking on the asset. Many methods are available to overload, giving you control over appearance as well as user experience.

FAQ: Why can't I just access xNode from the Window menu?

xNode is built as a framework. A plugin developer might not be interested in his plugin existing under Window>xNode, and he wouldn't be able to remove it without modifying the xNode core. If you want the shortcut you can add it yourself through any editor script https://docs.unity3d.com/ScriptReference/MenuItem.html

Creating a Node

Nodes are like regular ScriptableObjects, except they are editable through a NodeGraph, and let you create and access inputs and outputs. To create a node, simply derive from the Node class.

// SimpleNode.cs

public class SimpleNode : Node { 
  [Input] public float a;
  [Output] public float b;

  public override object GetValue(NodePort port) {
    if (port.fieldName == "b") return GetInputValue<float>("a", a);
    else return null;
  }
}

Tip: Quickly create a Node script by selecting Assets > Create > xNode > Node C# Script

[Input] and [Output] lets you define static ports for the node which can be connected to other node ports. The field name is cached and used to access the NodePort at any time using methods such as NodePort GetInputPort(string fieldName) and T GetInputValue<T>(string fieldName, T fallback). These ports will only show on serialized fields.

For all nodes with output ports, it is important to override GetValue(NodePort port), as this will be called when using GetInputValue. The requested NodePort is passed and the returned data will be passed to the asking node. In this example we check if the fieldName of the requested node port is "b". If it is, we return the value of "a". Since there is only one output, such a check is largely unnecessary, as no other ports will ever be requested.

Tips

Press F to home view Homing the view means automatically snapping your view to your nodes
Double click a node header to center it Does exactly what it says on the tin. It would look silly to have just one tip without a drop-down description, wouldn't it?
Right-click while dragging a connection to place a reroute point Reroute points allow node connection noodles to follow a certain path before reaching their destination.
Clone this wiki locally