Class CanvasEdge
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class CanvasEdge : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, ISelectableElement
Remarks
Pair it with CanvasNode: GetPortAnchor(CanvasPortSide, int) gives the two points, and the edge positions itself over the bounding box that covers them, so from and to are in the same content-space units as a node's x and y. Add edges before nodes, so nodes paint on top.
Picking follows the line, not the box. The web draws a second, transparent, fat path to make a thin line clickable; here ContainsPoint(Vector2) answers with the distance from the pointer to the sampled path, so there is no second path to keep in sync with the first.
Every colour the painter uses is read from a custom style property, because a colour written in
C# is a colour the theme cannot reach. --edge-color and its siblings are declared in
CanvasEdge.uss and bound to App UI tokens there.
Examples
An edge between two nodes' ports.
var edge = new CanvasEdge
{
from = source.GetPortAnchor(CanvasPortSide.Output, 0),
to = target.GetPortAnchor(CanvasPortSide.Input, 0),
curveStyle = CanvasCurveStyle.Bezier,
isDirectional = true,
};
canvas.Add(edge);
In UXML.
<appui:CanvasEdge curve-style="SmoothStep" is-directional="true" />
Constructors
CanvasEdge()
Default constructor.
Declaration
public CanvasEdge()
Fields
activeUssClassName
The styling class of an active edge.
Declaration
public const string activeUssClassName = "appui-canvas-edge--active"
Field Value
| Type | Description |
|---|---|
| string |
curveStyleUssClassName
The CanvasEdge curve style styling class.
Declaration
public const string curveStyleUssClassName = "appui-canvas-edge--"
Field Value
| Type | Description |
|---|---|
| string |
defaultStrokeWidth
The default stroke width, in pixels.
Declaration
public const float defaultStrokeWidth = 2
Field Value
| Type | Description |
|---|---|
| float |
directionalUssClassName
The styling class of a directional edge.
Declaration
public const string directionalUssClassName = "appui-canvas-edge--directional"
Field Value
| Type | Description |
|---|---|
| string |
hitTolerance
Half the width of the band around the line that answers a pick, in pixels.
Declaration
public const float hitTolerance = 7
Field Value
| Type | Description |
|---|---|
| float |
Remarks
A hairline is impossible to hit exactly, so the edge accepts a pointer within this distance of the path. It is the equivalent of the web's transparent fat hit-path.
padding
The margin left around the path's bounding box, in pixels, so a thick stroke and the arrowhead are not clipped by the element's own box.
Declaration
public const float padding = 16
Field Value
| Type | Description |
|---|---|
| float |
ussClassName
The CanvasEdge main styling class.
Declaration
public const string ussClassName = "appui-canvas-edge"
Field Value
| Type | Description |
|---|---|
| string |
Properties
active
Whether the edge is an active path, for instance the branch a blend tree is currently taking.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool active { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
bounds
The element's box in the canvas' content space: the path's bounding box grown by padding.
Declaration
public Rect bounds { get; }
Property Value
| Type | Description |
|---|---|
| Rect |
color
An explicit stroke colour, overriding the stylesheet's.
Declaration
public Optional<Color> color { get; set; }
Property Value
| Type | Description |
|---|---|
| Optional<Color> |
Remarks
Unset by default, which is the normal case: the colour then comes from --edge-color
and follows the theme. Set it only when the colour carries data, such as a connection type.
curveStyle
The path's shape. Bezier by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public CanvasCurveStyle curveStyle { get; set; }
Property Value
| Type | Description |
|---|---|
| CanvasCurveStyle |
from
The edge's start point, in the canvas' content space.
Declaration
[CreateProperty]
[UxmlAttribute]
public Vector2 from { get; set; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
highlighted
Whether the edge is drawn emphasised, which is true when it is either active or selected.
Declaration
public bool highlighted { get; }
Property Value
| Type | Description |
|---|---|
| bool |
isDirectional
Whether an arrowhead is drawn at the path's midpoint. False by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool isDirectional { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
lod
How much of the edge to draw. Full by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public CanvasLevelOfDetail lod { get; set; }
Property Value
| Type | Description |
|---|---|
| CanvasLevelOfDetail |
Remarks
At Box, a far zoom-out, the edge draws its line only: no hit area and no arrow, which are too small to use at that scale. Compact draws like Full.
midpoint
Where the arrowhead sits, in the canvas' content space.
Declaration
public Vector2 midpoint { get; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
midpointAngle
The direction the arrowhead points, in degrees clockwise from the positive X axis.
Declaration
public float midpointAngle { get; }
Property Value
| Type | Description |
|---|---|
| float |
resolvedColor
The stroke colour resolved from the stylesheet.
Declaration
public Color resolvedColor { get; }
Property Value
| Type | Description |
|---|---|
| Color |
resolvedHighlightColor
The emphasised stroke colour resolved from the stylesheet.
Declaration
public Color resolvedHighlightColor { get; }
Property Value
| Type | Description |
|---|---|
| Color |
sag
How far a Cable edge droops at its middle, in pixels. Zero for every other curve style.
Declaration
public float sag { get; }
Property Value
| Type | Description |
|---|---|
| float |
selected
Whether the edge is selected. Drawn the same way as active.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool selected { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
strokeWidth
The stroke width in pixels. defaultStrokeWidth by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public float strokeWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Remarks
A highlighted edge draws one pixel thicker, so it reads as emphasised at any width.
to
The edge's end point, in the canvas' content space.
Declaration
[CreateProperty]
[UxmlAttribute]
public Vector2 to { get; set; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
Methods
ContainsPoint(Vector2)
Whether a point in the element's local space is close enough to the line to count as a hit.
Declaration
public override bool ContainsPoint(Vector2 localPoint)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | localPoint | The point, in the element's local coordinates. |
Returns
| Type | Description |
|---|---|
| bool | Whether the point is within hitTolerance of the path. |
Overrides
GetCurveStyleUssClassName(CanvasCurveStyle)
Declaration
public static string GetCurveStyleUssClassName(CanvasCurveStyle enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| CanvasCurveStyle | enumValue |
Returns
| Type | Description |
|---|---|
| string |
SamplePath()
The path, sampled into a polyline in the canvas' content space.
Declaration
public IReadOnlyList<Vector2> SamplePath()
Returns
| Type | Description |
|---|---|
| IReadOnlyList<Vector2> |
Remarks
The same samples drive both painting and picking, so the line a user sees is exactly the line they can click.
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
selectRequested
Raised when the edge is pressed.
Declaration
public event Action selectRequested
Event Type
| Type | Description |
|---|---|
| Action |