-
-
Notifications
You must be signed in to change notification settings - Fork 602
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.
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;
}
}
}
Each node calls Init on start. If you, as an example, want to modiy the name of a node on creation, you can do so by overriding Init.
public void Init() {
name = "MyName";
}
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("")]
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
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)]
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)]