-
-
Notifications
You must be signed in to change notification settings - Fork 588
Getting Started
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.
You need to get familiar with the following concepts:
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
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.