Struct UIAnchor
Represents a complete anchor and pivot configuration for positioning a
RectTransform within its parent canvas or panel.
Implements
Inherited Members
Namespace: Scylla.Core.Util.UI
Assembly: ScyllaCore.dll
Syntax
public readonly struct UIAnchor : IEquatable<UIAnchor>
Remarks
A UIAnchor bundles the four values that Unity's RectTransform
needs to place and size a UI element:
- Min - the normalized lower-left anchor point within the parent (both components in the range 0-1).
- Max - the normalized upper-right anchor point within the parent (both components in the range 0-1).
- Pivot - the normalized reference point within the element itself around which position offsets and rotations are applied (both components in the range 0-1).
- Position - the pixel offset of the pivot from the anchor point. A zero vector places the element's pivot exactly at the anchor.
When Min equals Max the element has a fixed size and is placed at a single anchor point (the nine standard positions). When they differ, the element stretches to fill the region between the two anchor points. Use IsStretch, IsHorizontalStretch, and IsVerticalStretch to detect stretch configurations.
Call ApplyTo(RectTransform) to write the stored values directly onto a
RectTransform. The UIAnchors static class provides
pre-built presets for the nine standard alignment positions and common stretch
configurations so that manual construction is rarely necessary.
UIAnchor is a readonly value type. Mutation helpers such as
WithPosition(Vector2) and WithPivot(Vector2) return
new instances rather than modifying the receiver.
Equality uses Vector2.Equals (exact component comparison) rather than
approximate float comparison, consistent with Unity's own vector equality.
Constructors
UIAnchor(float, float, float, float, float, float, float, float)
Initializes a new UIAnchor with all eight configuration values specified individually.
Declaration
public UIAnchor(float posX, float posY, float minX, float minY, float maxX, float maxY, float pivotX, float pivotY)
Parameters
| Type | Name | Description |
|---|---|---|
| float | posX | X component of the anchored position offset in pixels.
Set to |
| float | posY | Y component of the anchored position offset in pixels.
Set to |
| float | minX | X component of the minimum anchor point, normalized within the parent (0-1).
|
| float | minY | Y component of the minimum anchor point, normalized within the parent (0-1).
|
| float | maxX | X component of the maximum anchor point, normalized within the parent (0-1).
When equal to |
| float | maxY | Y component of the maximum anchor point, normalized within the parent (0-1).
When equal to |
| float | pivotX | X component of the pivot point, normalized within the element (0-1).
|
| float | pivotY | Y component of the pivot point, normalized within the element (0-1).
|
UIAnchor(Vector2, Vector2, Vector2, Vector2)
Initializes a new UIAnchor using pre-constructed Vector2
values for all four configuration fields.
Declaration
public UIAnchor(Vector2 position, Vector2 min, Vector2 max, Vector2 pivot)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | position | The anchored position offset in pixels. Both components are typically |
| Vector2 | min | The minimum anchor point, with both components normalized within the parent (0-1). |
| Vector2 | max | The maximum anchor point, with both components normalized within the parent (0-1).
When equal to |
| Vector2 | pivot | The pivot point, with both components normalized within the element (0-1). |
Properties
IsHorizontalStretch
Gets a value indicating whether this anchor stretches the element horizontally. The element's left and right edges will track the parent's width when this is true.
Declaration
public bool IsHorizontalStretch { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
IsStretch
Gets a value indicating whether this anchor represents a stretch configuration in any direction. A stretch configuration occurs when Min and Max are not equal, causing the element to resize as the parent resizes.
Declaration
public bool IsStretch { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
IsVerticalStretch
Gets a value indicating whether this anchor stretches the element vertically. The element's top and bottom edges will track the parent's height when this is true.
Declaration
public bool IsVerticalStretch { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
Max
Gets the maximum (upper-right) anchor point expressed as a normalized position within the parent rectangle. Both X and Y components are in the range 0 to 1. When Max equals Min, the element has a fixed size; when they differ, the element stretches to fill the region between the two points.
Declaration
public Vector2 Max { get; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
Min
Gets the minimum (lower-left) anchor point expressed as a normalized position
within the parent rectangle. Both X and Y components are in the range 0 to 1,
where (0, 0) is the parent's lower-left corner and (1, 1) is the
upper-right corner.
Declaration
public Vector2 Min { get; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
Pivot
Gets the pivot point expressed as a normalized position within the element itself.
Both X and Y components are in the range 0 to 1, where (0, 0) is the
element's lower-left corner and (1, 1) is its upper-right corner.
The pivot is the reference point for position offsets and rotations.
Declaration
public Vector2 Pivot { get; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
Position
Gets the anchored position offset in pixels - the distance from the anchor point
to the element's pivot. A value of (0, 0) places the pivot exactly on
the anchor point with no offset.
Declaration
public Vector2 Position { get; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
Methods
ApplyTo(RectTransform)
Writes this UIAnchor's configuration to the specified
RectTransform, setting its anchorMin, anchorMax,
pivot, and anchoredPosition properties.
Declaration
public void ApplyTo(RectTransform rectTransform)
Parameters
| Type | Name | Description |
|---|---|---|
| RectTransform | rectTransform | The |
Remarks
Properties are set in the order: anchorMin, anchorMax,
pivot, anchoredPosition. Changing the pivot or anchors of a
RectTransform at runtime may visually shift the element if it has
a non-zero anchored position; set Position to zero first if
you need a pivot change without a positional shift.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
Equals(UIAnchor)
Determines whether this UIAnchor is equal to another
UIAnchor. All four fields are compared using exact
Vector2 equality (bitwise float comparison).
Declaration
public bool Equals(UIAnchor other)
Parameters
| Type | Name | Description |
|---|---|---|
| UIAnchor | other | The UIAnchor to compare against. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Determines whether this UIAnchor is equal to the given object.
Returns false if obj is not a UIAnchor.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object to compare against. |
Returns
| Type | Description |
|---|---|
| bool |
|
Overrides
GetHashCode()
Computes the hash code for this UIAnchor derived from all four configuration fields.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | An integer hash code combining Position, Min, Max, and Pivot. |
Overrides
ToString()
Returns a human-readable string representation of this UIAnchor showing all four configuration fields.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string in the format
|
Overrides
WithPivot(float, float)
Returns a new UIAnchor that is identical to this instance except with Pivot replaced by the specified component values.
Declaration
public UIAnchor WithPivot(float x, float y)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | The new X component of the pivot point, normalized within the element (0-1).
|
| float | y | The new Y component of the pivot point, normalized within the element (0-1).
|
Returns
| Type | Description |
|---|---|
| UIAnchor | A new UIAnchor with Pivot set to
|
WithPivot(Vector2)
Returns a new UIAnchor that is identical to this instance except
with Pivot replaced by the specified Vector2.
Declaration
public UIAnchor WithPivot(Vector2 pivot)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | pivot | The new pivot point, with both components normalized within the element (0-1). |
Returns
| Type | Description |
|---|---|
| UIAnchor | A new UIAnchor with Pivot set to
|
WithPosition(float, float)
Returns a new UIAnchor that is identical to this instance except with Position replaced by the specified component values.
Declaration
public UIAnchor WithPosition(float x, float y)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | The new X component of the anchored position offset in pixels. |
| float | y | The new Y component of the anchored position offset in pixels. |
Returns
| Type | Description |
|---|---|
| UIAnchor | A new UIAnchor with Position set to
|
WithPosition(Vector2)
Returns a new UIAnchor that is identical to this instance except
with Position replaced by the specified Vector2.
Declaration
public UIAnchor WithPosition(Vector2 position)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | position | The new anchored position offset in pixels. |
Returns
| Type | Description |
|---|---|
| UIAnchor | A new UIAnchor with Position set to
|
Operators
operator ==(UIAnchor, UIAnchor)
Returns a value indicating whether two UIAnchor instances are equal,
using exact Vector2 component comparison for all four fields.
Declaration
public static bool operator ==(UIAnchor left, UIAnchor right)
Parameters
| Type | Name | Description |
|---|---|---|
| UIAnchor | left | The first UIAnchor to compare. |
| UIAnchor | right | The second UIAnchor to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator !=(UIAnchor, UIAnchor)
Returns a value indicating whether two UIAnchor instances are not equal.
Declaration
public static bool operator !=(UIAnchor left, UIAnchor right)
Parameters
| Type | Name | Description |
|---|---|---|
| UIAnchor | left | The first UIAnchor to compare. |
| UIAnchor | right | The second UIAnchor to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|