Node Attributes - McManning/BlueGraph GitHub Wiki

Class Attributes

[Node] Attribute

You can use the [Node] attribute without setting any fields and use all the defaults. But it does come with a suite of useful configurations for your node:

Field Description
Name Display name of the node. If not supplied, this will be inferred based on the class name.
Help Tooltip help content displayed for the node
Path Slash-delimited directory path to categorize this node in the search window.
Deletable Can this node be deleted from the graph (defaults to true)

[Tags] Attribute

You can use the optional [Tags] attribute on a node to add one or more tags to assist in organization. When defining a Graph asset, you can use a [IncludeTags] attribute on the Graph to only allow a subset of nodes to be added to that graph.

[Node(Path = "Heightmap/Factory")]
[Tags("Heightmap", "World Gen", "Experimental")]
public class NewPerlinHeightmap : Node
{
    [Input] public Rect region = new Rect(0, 0, 100, 100);
    [Input] public float scale = 10f;
    ...
}

Field Attributes

[Input] Attribute

Fields with the [Input] attribute will add a port that can accept one or more input connections from other nodes. You can then access that port via Node.GetPort(portName) or Node.GetInputValue(portName).

When the field is public the appropriate UIElement editors are added to the node's view in the canvas to allow you to set a constant value from directly within the editor.

Typically - that constant value in the field is used as a fallback when there are no edges connected to the associated input port.

The attribute contains a number of optional fields that you can use to control the port's behavior:

Field Description
Name Display name of the input slot. If not supplied, this will default to the field name
Multiple Can this input accept multiple outputs at once (defaults to false)
Editable Can the associated field be directly modified when there are no connections (defaults to true)

[Output] Attribute

Fields with the [Output] attribute will add an output port with the same name and type as the field. These do not provide a editor for the field - as outputs are calculated via the OnRequestValue method in the class.

Fields that are used as outputs are typically good candidates for storing memoized values for heavy calculations based on input values.

The attribute contains a number of optional fields that you can use to control the port's behavior:

Field Description
Name Display name of the output slot. If not supplied, this will default to the field name
Multiple Can this output go to multiple inputs at once (defaults to true)

[Editable] Attribute

If you want to expose an additional editable field to the node's view on the canvas without making an associated port you can add an [Editable] attribute to that field.

For example, if you wanted a Blend node with a local Mode setting, you can do the following:

[Node]
public class Blend : Node
{
    public enum BlendMode 
    {
        Add,
        Multiply,
        Screen,
        Dodge
    };

    [Input] public Texture2D foreground;
    [Input] public Texture2D background;
    [Output] Texture2D result;

    [Editable] public BlendMode mode;

    public override object OnRequestValue(Port port)
    {
        Texture2D fg = GetInputValue("foreground", foreground);
        Texture2D bg = GetInputValue("background", background);

        // You do not need to call GetInputValue() on editables
        switch (mode) {
            case BlendMode.Add:
                result = MyBlendMagic.Add(fg, bg);
            case BlendMode.Multiply:
                result = MyBlendMagic.Multiply(fg, bg);
            ...
        }

        return result;
    }
}

Method Attributes

[ContextMenu] Attribute

Use Unity's [ContextMenu] attribute on a non-static method in a node to add a menu item to the node's context menu that executes the function when selected.

[Node(Path = "Dialog")]
[Tags("Dialog")]
public class Say : Node
{
    [Input("Name")] public string speakerName;
    [Input("Portrait")] public Texture2D portrait;
    [Input("Text")] public string text;

    [ContextMenu("Do Something")]
    void DoSomething()
    {
        Debug.Log("Perform operation for Node " + ID);
    }
    
    ...
}