Skip to content

Dynamic Port List

Thor Brigsted edited this page Oct 5, 2019 · 8 revisions

Warning: Dynamic port lists are experimental. Expect bugs in the editor.

What?

Dynamic port lists are not a class or a type in itself. It is an editor-only convenience functionality that helps you maintain a list of dynamic ports, optionally synced with an array or list.

image

How?

You can create them in two ways, either by setting the parameter in the Input/Output attribute [Output(dynamicPortList = true)] or through a custom node editor using the method NodeEditorGUILayout.DynamicPortList

Using a list or array as backing field type, the list will assign an editable value for each port. Non-array fields will still work but won't have a backing value per port.

public class SimpleNode : Node {
    [Output(dynamicPortList = true)] public float[] myArray;
}

Getting the ports

Ports created by DynamicPortList are named in the pattern [fieldName][space][index] eg. "myArray 0" for the first port. You can access them through GetPort("myArray 0").

Tips

Customize dynamic port list You may want to customize the way the list is drawn, either for visual or functional purpose. You can do this by modifying the list delegates after the list is created, by manually drawing the list in a custom NodeEditor with the method NodeEditorGUILayout.DynamicPortList which takes an Action onCreation parameter.

A good starting point is to copy the delegate you want to override from the method NodeEditorGUILayout.CreateReorderableList, paste it in your onCreate override, and make your modifications.

More info on ReorderableLists here

public class MyNodeEditor : NodeEditor {
    public override void OnBodyGUI() {
        // Draw GUI
        NodeEditorGUILayout.DynamicPortList(
            "myFloatList", // field name
            typeof(float), // field type
            serializedObject, // serializable object
            NodePort.IO.Input, // new port i/o
            Node.ConnectionType.Override, // new port connection type
            OnCreateReorderableList); // onCreate override. This is where the magic happens.
    }

    void OnCreateReorderableList(ReorderableList list) {
        // Override drawHeaderCallback to display node's name instead
        list.drawHeaderCallback = (Rect rect) => {
            string title = serializedObject.targetObject;
            EditorGUI.Label(rect, title);
        };
    }
}

See also: [Input-Output]

Clone this wiki locally