Skip to content
Thor Brigsted edited this page Aug 21, 2019 · 23 revisions
class Node : ScriptableObject

What?

Nodes are the building blocks of xNode. Through the Node class you can access the node's ports, and its parent graph.

You can customize the look of your nodes through a node editor

How?

Any class that derives from Node is a valid node, and is automatically added to the default graph context menu.

A simple node with a single input and a single output can look like this:

public class SimpleNode : Node {
    [Input] public float value;
    [Output] public float result;
}

You can turn any serialized type into an input or output by adding the appropriate attribute.

Inputs and Outputs

Any serialized field on the node can be registered as either an input or an output by adding an [Input] or [Output] attribute.

Read more about which fields Unity can serialize here.

Read more about ports here.

When another node asks for the output of this node, the method object GetValue(NodePort port); is called. You need to override it in order to have your node return something sensible. In this example, we will just return the input value.

// Returns value + 1
public class SimpleNode : Node {
    [Input] public float value;
    [Output] public float result;

    public override object GetValue(NodePort port) {
        // Check which output is being requested. 
        // In this node, there aren't any other outputs than "result".
        if (port.fieldName == "result") {
            // Return input value + 1
            return GetInputValue<float>("value", this.value) + 1;
        }
        // Hopefully this won't ever happen, but we need to return something
        // in the odd case that the port isn't "result"
        else return null;
    }
}

Init

Init is called in the OnEnable phase, which means it is called whenever the node is loaded. This is good for initializing non-serialized values.

Bear in mind that Init might soon be deprecated, and you'll be able to use regular OnEnable instead.

Reset

Since nodes all derive from ScriptableObject, Unity calls Reset() on the node as soon as it is created. This comes before Awake and OnEnable. If you want to set a custom name for the node it is recommended that you do it here.

private void Reset() {
    name = "MyName";
}

Tips

Customize creation menu path with [CreateNodeMenu]

By default, the context menu path will be based upon namespace and class name. If you wish to change this, you can use the [CreateNodeMenu] class attribute. Simply add it to your class and supply it with your desired path.

  • [CreateNodeMenu("MyMathNodes/Subtraction")]

Alternatively, you can hide a node from the context menu by supplying a null or empty string to the attribute.

  • [CreateNodeMenu("")]

For more control over node menu path, refer to node editors

Set node color tint [NodeTint] Nodes types can be tinted for easier grouping and overview. To do this, simply add a [NodeTint] attribute to your node class, and supply it with either a hex color string, or 3 floats representing rgb.
  • [NodeTint("#ffaaaa")]
  • [NodeTint(1.0f, 0.8f, 0.8f)]

For more control over node tint, refer to node editors

Set node width [NodeWidth] Sometimes you want a wider node for layout purposes. For this, add the [NodeWidth] attribute to your node class, and supply it with your desired width. The default width of a node is 208 pixels.
  • [NodeWidth(304)] For more control over node width, refer to node editors
Nodes support [ContextMenu] Nodes support the `[ContextMenu]` attribute. Simply add `[ContextMenu]` to a non-static method and it will show up when you right-click the node header. Select it to execute the method. You can read more about it [here](https://docs.unity3d.com/ScriptReference/ContextMenu.html)
Fix enum popup position with [NodeEnum] Due to the sometimes odd nature of GUI zoom, enum popup fields can appear offset when opened. You can use the [NodeEnum] attribute on the field as an easy fix.
Clone this wiki locally