NodeView<T>
Derive from this class to add custom UI to the view generated for a specific NodeView<T>.Node type.
Read time 3 minutesLast updated 6 days ago
Definition
- Type: Class
- Namespace: Unity.GraphToolkit.Editor
- Assembly: UnityEditor
public class NodeView<T> where T : Node
Remarks
A VisualElement is built for every NodeView<T>.Node that appears in a graph. A NodeView<T> lets you inject custom UI into that generated element. Subclasses are discovered by their type parameter: for a NodeView<T>.Node of type , the concrete NodeView<T> whose type argument matches is used, walking up the NodeView<T>.Node inheritance chain if there is no exact match.
TThe NodeView<T>.Node instance is available through NodeView<T>.Node once the view is constructed. Access the generated visual element through NodeView<T>.View and add custom UI to INodeView.Root.
Allocate custom UI in NodeView<T>.OnViewBuilt and re-add it in NodeView<T>.OnCullingChanged (when is ). Do not allocate UI in NodeView<T>.OnViewAttached: that callback can fire multiple times during a node's lifetime — for example when the user tabs away from and back to the graph view, or when a block node is dragged and re-parented — and any UI you allocate there accumulates as duplicates. When a node returns from being culled, the contents of INodeView.Root are cleared, so a cached reference is no longer parented; re-add it from NodeView<T>.OnCullingChanged to avoid allocating a new element.
cullingEnabledfalseAdditional Resources: NodeView<T>.Node, INodeView
Examples
// The Node type that this view customizes.class MyNode : Node{ protected override void OnDefinePorts(IPortDefinitionContext context) { context.AddInputPort<float>("input"); }}// This view is constructed for every MyNode instance in a graph.class MyNodeView : NodeView<MyNode>{ Label m_Label; public override void OnViewBuilt() { // Allocate custom UI once, after the built-in UI is ready and before the node attaches to a // panel. Cache the reference so OnCullingChanged can re-add it without allocating again. m_Label = new Label($"Node: {Node.Title}"); View.Root.Add(m_Label); } public override void OnCullingChanged(bool cullingEnabled) { // When cullingEnabled is false, the node has just returned from being culled and Root has // been cleared. Re-add the cached element — no allocation needed. if (!cullingEnabled && m_Label != null) View.Root.Add(m_Label); }}
Properties
Property | Description |
|---|---|
| Node | The NodeView<T>.Node instance this view customizes. |
| View | The generated view for this node. Add custom UI to INodeView.Root. |
Methods
Method | Description |
|---|---|
| OnCullingChanged | Called when the node's culling state changes. |
| OnViewAttached | Called when the node's INodeView.Root is attached to a UI panel. |
| OnViewBuilt | Called once, after the node's built-in UI is fully constructed and before its VisualElement is attached to the graph view. |
| OnViewDetached | Called when the node's INodeView.Root is detached from its UI panel. |
| OnViewLODChanged | Called when the graph view's zoom level changes. |