Class CanvasNode
A node-graph box, for Animator-style state machines and blend trees drawn inside a Canvas.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class CanvasNode : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, ISelectableElement
Remarks
A titled header, an optional body and labelled input/output ports down the left and right edges. Place it with x and y in the canvas' content space, set movable for drag-to-move, and connect two ports with a CanvasEdge.
The node lives inside a Canvas and owns none of its pan or zoom: the canvas applies its zoom as a transform on the viewport container, so a drag delta converted through the node's parent is already in content space. That is why this component has no zoom property of its own, unlike the web original, which has to divide its pointer deltas by the zoom by hand.
headerHeight, portRowHeight and portsPaddingY are the published geometry an edge routes against, and GetPortAnchor(CanvasPortSide, int) turns them into a content-space point. They are compile-time constants, so the stylesheet expresses the same geometry through the static sizing ramp — the one ramp that does not rescale with density — and the two cannot drift apart.
Examples
Two connected nodes.
var from = new CanvasNode
{
title = "Idle",
x = 0, y = 0,
outputPorts = new[] { new CanvasPort("out", "Next") },
};
var to = new CanvasNode
{
title = "Run",
x = 260, y = 80,
inputPorts = new[] { new CanvasPort("in", "Previous") },
};
var edge = new CanvasEdge
{
from = from.GetPortAnchor(CanvasPortSide.Output, 0),
to = to.GetPortAnchor(CanvasPortSide.Input, 0),
isDirectional = true,
};
canvas.Add(edge); // edges first, so nodes paint on top
canvas.Add(from);
canvas.Add(to);
In UXML.
<appui:CanvasNode title="Idle" variant="Entry" movable="true" />
Constructors
CanvasNode()
Default constructor.
Declaration
public CanvasNode()
Fields
bodyUssClassName
The styling class of the body, which holds the node's children.
Declaration
public const string bodyUssClassName = "appui-canvas-node__body"
Field Value
| Type | Description |
|---|---|
| string |
defaultWidth
The node's default width, in pixels.
Declaration
public const float defaultWidth = 180
Field Value
| Type | Description |
|---|---|
| float |
Remarks
Density-invariant: the stylesheet draws it from --appui-static-sizing-2250.
grabbingUssClassName
The styling class of a node currently being dragged.
Declaration
public const string grabbingUssClassName = "appui-canvas-node--grabbing"
Field Value
| Type | Description |
|---|---|
| string |
headerHeight
The height of the node's header, in pixels.
Declaration
public const float headerHeight = 30
Field Value
| Type | Description |
|---|---|
| float |
Remarks
Density-invariant: the stylesheet draws it from --appui-static-sizing-375.
headerUssClassName
The styling class of the header.
Declaration
public const string headerUssClassName = "appui-canvas-node__header"
Field Value
| Type | Description |
|---|---|
| string |
lodUssClassName
The CanvasNode level-of-detail styling class.
Declaration
public const string lodUssClassName = "appui-canvas-node--lod-"
Field Value
| Type | Description |
|---|---|
| string |
movableUssClassName
The styling class of a node that can be dragged.
Declaration
public const string movableUssClassName = "appui-canvas-node--movable"
Field Value
| Type | Description |
|---|---|
| string |
portColumnUssClassName
The styling class of a column of ports.
Declaration
public const string portColumnUssClassName = "appui-canvas-node__ports--"
Field Value
| Type | Description |
|---|---|
| string |
portDotUssClassName
The styling class of a port's dot.
Declaration
public const string portDotUssClassName = "appui-canvas-node__port-dot"
Field Value
| Type | Description |
|---|---|
| string |
portHitUssClassName
The styling class of a port's pointer target.
Declaration
public const string portHitUssClassName = "appui-canvas-node__port-hit"
Field Value
| Type | Description |
|---|---|
| string |
portLabelUssClassName
The styling class of a port's label.
Declaration
public const string portLabelUssClassName = "appui-canvas-node__port-label"
Field Value
| Type | Description |
|---|---|
| string |
portRowHeight
The height of one port row, in pixels.
Declaration
public const float portRowHeight = 24
Field Value
| Type | Description |
|---|---|
| float |
Remarks
Density-invariant: the stylesheet draws it from --appui-static-sizing-300.
portRowUssClassName
The styling class of one port row.
Declaration
public const string portRowUssClassName = "appui-canvas-node__port-row"
Field Value
| Type | Description |
|---|---|
| string |
portsPaddingY
The vertical padding above the first port row and below the last, in pixels.
Declaration
public const float portsPaddingY = 8
Field Value
| Type | Description |
|---|---|
| float |
Remarks
Density-invariant: the stylesheet draws it from --appui-static-sizing-100.
portsUssClassName
The styling class of the port area.
Declaration
public const string portsUssClassName = "appui-canvas-node__ports"
Field Value
| Type | Description |
|---|---|
| string |
positionedUssClassName
The styling class of a node placed at an explicit position.
Declaration
public const string positionedUssClassName = "appui-canvas-node--positioned"
Field Value
| Type | Description |
|---|---|
| string |
titleUssClassName
The styling class of the header's title.
Declaration
public const string titleUssClassName = "appui-canvas-node__title"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The CanvasNode main styling class.
Declaration
public const string ussClassName = "appui-canvas-node"
Field Value
| Type | Description |
|---|---|
| string |
variantUssClassName
The CanvasNode variant styling class.
Declaration
public const string variantUssClassName = "appui-canvas-node--"
Field Value
| Type | Description |
|---|---|
| string |
Properties
contentContainer
The node's body, which is also its content container.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
header
The header element, exposed so a caller can style or decorate it.
Declaration
public VisualElement header { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
inputPorts
The ports down the node's left edge.
Declaration
public IEnumerable<CanvasPort> inputPorts { get; set; }
Property Value
| Type | Description |
|---|---|
| IEnumerable<CanvasPort> |
Remarks
Setting this rebuilds the input column. Null or empty removes every input row.
lod
How much of the node to draw. Full by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public CanvasLevelOfDetail lod { get; set; }
Property Value
| Type | Description |
|---|---|
| CanvasLevelOfDetail |
Remarks
Compact hides the port labels and the body. Box also hides the title and the port dots, leaving the header colour over the port area. Every level keeps the header and port rows' geometry, so edges anchored with GetPortAnchor(CanvasPortSide, int) still meet the node. Pick the level from the canvas zoom with LevelOfDetailForZoom(float, float, float).
movable
Whether the node can be dragged with the primary pointer. False by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool movable { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
A movable node moves itself and reports the result through positionChanged. It also stops the press from reaching the canvas, so dragging a node never pans the canvas.
outputPorts
The ports down the node's right edge.
Declaration
public IEnumerable<CanvasPort> outputPorts { get; set; }
Property Value
| Type | Description |
|---|---|
| IEnumerable<CanvasPort> |
Remarks
Setting this rebuilds the output column. Null or empty removes every output row.
selected
Whether the node is selected. False by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool selected { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
title
The header label. Null or empty hides the header entirely.
Declaration
[CreateProperty]
[UxmlAttribute]
public string title { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
variant
The node's colour role. Default by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public CanvasNodeVariant variant { get; set; }
Property Value
| Type | Description |
|---|---|
| CanvasNodeVariant |
width
The node's width in pixels. defaultWidth by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public float width { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
x
The node's X coordinate in the canvas' content space. Unset leaves the node in normal flow.
Declaration
[CreateProperty]
[UxmlAttribute]
public Optional<float> x { get; set; }
Property Value
| Type | Description |
|---|---|
| Optional<float> |
y
The node's Y coordinate in the canvas' content space. Unset leaves the node in normal flow.
Declaration
[CreateProperty]
[UxmlAttribute]
public Optional<float> y { get; set; }
Property Value
| Type | Description |
|---|---|
| Optional<float> |
Methods
GetLodUssClassName(CanvasLevelOfDetail)
Declaration
public static string GetLodUssClassName(CanvasLevelOfDetail enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| CanvasLevelOfDetail | enumValue |
Returns
| Type | Description |
|---|---|
| string |
GetPortAnchor(CanvasPortSide, int)
The point an edge should attach to for one port, in the canvas' content space.
Declaration
public Vector2 GetPortAnchor(CanvasPortSide side, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| CanvasPortSide | side | The side the port sits on. |
| int | index | The port's index within that side. |
Returns
| Type | Description |
|---|---|
| Vector2 | The anchor point. |
Remarks
Computed from headerHeight, portsPaddingY and portRowHeight, so it is usable while a graph is still being built, before the node has ever been laid out. It assumes the node is positioned: an unset x or y counts as zero, and a hidden header contributes no offset.
The node's border is part of its box, so the ports are inset by one border width from the coordinates x and y name. That inset is added here once the border has resolved, which is what keeps an edge landing on the dot rather than a pixel beside it. Before the first layout the border resolves to zero and this returns exactly the registry's formula.
GetPortColumnUssClassName(CanvasPortSide)
Declaration
public static string GetPortColumnUssClassName(CanvasPortSide enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| CanvasPortSide | enumValue |
Returns
| Type | Description |
|---|---|
| string |
GetPortCount(CanvasPortSide)
The number of ports on one side.
Declaration
public int GetPortCount(CanvasPortSide side)
Parameters
| Type | Name | Description |
|---|---|---|
| CanvasPortSide | side | The side to count. |
Returns
| Type | Description |
|---|---|
| int | The number of ports on that side. |
GetPortElement(CanvasPortSide, int)
The row element that draws one port, or null when the index is out of range.
Declaration
public VisualElement GetPortElement(CanvasPortSide side, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| CanvasPortSide | side | The side the port sits on. |
| int | index | The port's index within that side. |
Returns
| Type | Description |
|---|---|
| VisualElement | The row element, or null. |
GetVariantUssClassName(CanvasNodeVariant)
Declaration
public static string GetVariantUssClassName(CanvasNodeVariant enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| CanvasNodeVariant | enumValue |
Returns
| Type | Description |
|---|---|
| string |
SetSelectedWithoutNotify(bool)
Sets the selected state without notifying.
Declaration
public void SetSelectedWithoutNotify(bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | newValue | The new selected state. |
Events
portPointerDown
Raised when a pointer goes down on a port — begin dragging a new connection from it.
Declaration
public event Action<CanvasPortEvent> portPointerDown
Event Type
| Type | Description |
|---|---|
| Action<CanvasPortEvent> |
portPointerEntered
Raised when a pointer enters a port, which is where a dragged connection would snap to it.
Declaration
public event Action<CanvasPortEvent> portPointerEntered
Event Type
| Type | Description |
|---|---|
| Action<CanvasPortEvent> |
portPointerLeft
Raised when a pointer leaves a port.
Declaration
public event Action<CanvasPortEvent> portPointerLeft
Event Type
| Type | Description |
|---|---|
| Action<CanvasPortEvent> |
portPointerUp
Raised when a pointer is released on a port — drop a connection onto it. Validate in the handler.
Declaration
public event Action<CanvasPortEvent> portPointerUp
Event Type
| Type | Description |
|---|---|
| Action<CanvasPortEvent> |
positionChanged
Raised while a movable node is dragged, with its next content-space position.
Declaration
public event Action<Vector2> positionChanged
Event Type
| Type | Description |
|---|---|
| Action<Vector2> |
Remarks
The node has already moved itself by the time this fires; the event exists so the caller can persist the position and re-route any edges attached to it.
selectRequested
Raised when the node is pressed with the primary button, so the caller can select it.
Declaration
public event Action selectRequested
Event Type
| Type | Description |
|---|---|
| Action |
Remarks
A pan press inside a Canvas -- the middle button, or the primary button with the pan modifier held -- is taken by the canvas before the node sees it, so it never selects.