-
-
Notifications
You must be signed in to change notification settings - Fork 588
Nodes
class Node : ScriptableObject
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
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.
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 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.
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";
}
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