Struct UIMargin
Represents the external spacing (margin) around a UI element, expressed as individual per-side pixel values for left, right, top, and bottom edges.
Implements
Inherited Members
Namespace: Scylla.Core.Util.UI
Assembly: ScyllaCore.dll
Syntax
public readonly struct UIMargin : IEquatable<UIMargin>
Remarks
Margin describes the space outside an element's border, pushing the element away from adjacent siblings or the parent boundary. For internal spacing (space between an element's border and its content), use UIPadding instead.
UIMargin is a readonly value type. All mutation methods
(e.g. WithLeft(float), Scale(float)) return a new instance
rather than modifying the receiver, making it safe to pass by value without
defensive copies.
Equality comparisons use Mathf.Approximately so that minor floating-point
rounding differences do not produce false negatives.
Several named presets are available as static properties: Zero, Small, Medium, Large, and ExtraLarge.
Constructors
UIMargin(float)
Initializes a new UIMargin with the same pixel value applied to all four sides.
Declaration
public UIMargin(float uniform)
Parameters
| Type | Name | Description |
|---|---|---|
| float | uniform | The margin value in pixels to apply uniformly to the left, right, top, and bottom sides. Negative values are stored but typically produce overlapping layout results. |
UIMargin(float, float)
Initializes a new UIMargin with separate horizontal and vertical margin values. The horizontal value is applied equally to left and right; the vertical value is applied equally to top and bottom.
Declaration
public UIMargin(float horizontal, float vertical)
Parameters
| Type | Name | Description |
|---|---|---|
| float | horizontal | The margin value in pixels applied to both the left and right sides. |
| float | vertical | The margin value in pixels applied to both the top and bottom sides. |
UIMargin(float, float, float, float)
Initializes a new UIMargin with individually specified values for each side.
Declaration
public UIMargin(float left, float right, float top, float bottom)
Parameters
| Type | Name | Description |
|---|---|---|
| float | left | The left margin in pixels. |
| float | right | The right margin in pixels. |
| float | top | The top margin in pixels. |
| float | bottom | The bottom margin in pixels. |
Properties
Bottom
Gets the bottom margin in pixels. This is the space below the element's outer boundary.
Declaration
public float Bottom { get; }
Property Value
| Type | Description |
|---|---|
| float |
ExtraLarge
Gets a UIMargin with a uniform 24-pixel margin on all sides. Suitable for prominent UI regions or modal-style containers that require significant visual breathing room.
Declaration
public static UIMargin ExtraLarge { get; }
Property Value
| Type | Description |
|---|---|
| UIMargin |
Horizontal
Gets the sum of the left and right margins in pixels. Useful when calculating the total horizontal space consumed by the margin (e.g. to determine the remaining width available for content).
Declaration
public float Horizontal { get; }
Property Value
| Type | Description |
|---|---|
| float |
IsUniform
Gets a value indicating whether all four sides of this margin have the same value. Comparison uses Approximately(float, float, float) to tolerate minor floating-point imprecision.
Declaration
public bool IsUniform { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
IsZero
Gets a value indicating whether all four sides of this margin are zero. Comparison uses Approximately(float, float, float) to tolerate minor floating-point imprecision.
Declaration
public bool IsZero { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
Large
Gets a UIMargin with a uniform 16-pixel margin on all sides. Suitable for panels or sections that should be clearly separated from their surrounding content.
Declaration
public static UIMargin Large { get; }
Property Value
| Type | Description |
|---|---|
| UIMargin |
Left
Gets the left margin in pixels. This is the space to the left of the element's outer boundary.
Declaration
public float Left { get; }
Property Value
| Type | Description |
|---|---|
| float |
Medium
Gets a UIMargin with a uniform 8-pixel margin on all sides. A versatile default for most UI layouts that need comfortable spacing between elements without being overly spacious.
Declaration
public static UIMargin Medium { get; }
Property Value
| Type | Description |
|---|---|
| UIMargin |
Right
Gets the right margin in pixels. This is the space to the right of the element's outer boundary.
Declaration
public float Right { get; }
Property Value
| Type | Description |
|---|---|
| float |
Small
Gets a UIMargin with a uniform 4-pixel margin on all sides. Suitable for compact layouts where elements should be visually separated but space is limited.
Declaration
public static UIMargin Small { get; }
Property Value
| Type | Description |
|---|---|
| UIMargin |
Top
Gets the top margin in pixels. This is the space above the element's outer boundary.
Declaration
public float Top { get; }
Property Value
| Type | Description |
|---|---|
| float |
Vertical
Gets the sum of the top and bottom margins in pixels. Useful when calculating the total vertical space consumed by the margin (e.g. to determine the remaining height available for content).
Declaration
public float Vertical { get; }
Property Value
| Type | Description |
|---|---|
| float |
Zero
Gets a UIMargin with all four sides set to zero pixels. Use this as a default when no external spacing is required.
Declaration
public static UIMargin Zero { get; }
Property Value
| Type | Description |
|---|---|
| UIMargin |
Methods
Equals(UIMargin)
Determines whether this UIMargin is equal to another
UIMargin using per-side Mathf.Approximately comparisons.
Declaration
public bool Equals(UIMargin other)
Parameters
| Type | Name | Description |
|---|---|---|
| UIMargin | other | The UIMargin to compare against. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Determines whether this UIMargin is equal to the given object.
Returns false if obj is not a UIMargin.
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 UIMargin based on the exact floating-point values of all four sides.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | An integer hash code derived from Left, Right, Top, and Bottom. Note that because equality uses approximate comparison, two instances that compare as equal may produce different hash codes if their raw float values differ slightly. |
Overrides
Scale(float)
Returns a new UIMargin with all four side values multiplied by the given scale factor. Useful for DPI-aware scaling or responsive layout adjustments.
Declaration
public UIMargin Scale(float factor)
Parameters
| Type | Name | Description |
|---|---|---|
| float | factor | The scalar multiplier applied to each side. A value of |
Returns
| Type | Description |
|---|---|
| UIMargin | A new UIMargin where each side equals the original value multiplied
by |
ToRectOffset()
Converts this UIMargin to a UnityEngine.RectOffset suitable for
use with Unity uGUI layout groups such as HorizontalLayoutGroup and
VerticalLayoutGroup.
Declaration
public RectOffset ToRectOffset()
Returns
| Type | Description |
|---|---|
| RectOffset | A new UnityEngine.RectOffset whose integer fields are set by rounding each
side's pixel value using |
ToString()
Returns a human-readable string representation of this UIMargin.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | If IsUniform is |
Overrides
WithBottom(float)
Returns a new UIMargin that is identical to this instance except with the Bottom side replaced by the specified value.
Declaration
public UIMargin WithBottom(float bottom)
Parameters
| Type | Name | Description |
|---|---|---|
| float | bottom | The new bottom margin value in pixels. |
Returns
| Type | Description |
|---|---|
| UIMargin | A new UIMargin with Bottom set to |
WithLeft(float)
Returns a new UIMargin that is identical to this instance except with the Left side replaced by the specified value.
Declaration
public UIMargin WithLeft(float left)
Parameters
| Type | Name | Description |
|---|---|---|
| float | left | The new left margin value in pixels. |
Returns
| Type | Description |
|---|---|
| UIMargin | A new UIMargin with Left set to |
WithRight(float)
Returns a new UIMargin that is identical to this instance except with the Right side replaced by the specified value.
Declaration
public UIMargin WithRight(float right)
Parameters
| Type | Name | Description |
|---|---|---|
| float | right | The new right margin value in pixels. |
Returns
| Type | Description |
|---|---|
| UIMargin | A new UIMargin with Right set to |
WithTop(float)
Returns a new UIMargin that is identical to this instance except with the Top side replaced by the specified value.
Declaration
public UIMargin WithTop(float top)
Parameters
| Type | Name | Description |
|---|---|---|
| float | top | The new top margin value in pixels. |
Returns
| Type | Description |
|---|---|
| UIMargin | A new UIMargin with Top set to |
Operators
operator +(UIMargin, UIMargin)
Adds two UIMargin instances together, combining their per-side values. Useful for accumulating margins from multiple sources such as a base style and a per-element override.
Declaration
public static UIMargin operator +(UIMargin a, UIMargin b)
Parameters
| Type | Name | Description |
|---|---|---|
| UIMargin | a | The first margin. |
| UIMargin | b | The second margin to add to |
Returns
| Type | Description |
|---|---|
| UIMargin | A new UIMargin where each side equals the sum of the corresponding
sides of |
operator ==(UIMargin, UIMargin)
Returns a value indicating whether two UIMargin instances are equal,
using per-side Mathf.Approximately comparisons.
Declaration
public static bool operator ==(UIMargin left, UIMargin right)
Parameters
| Type | Name | Description |
|---|---|---|
| UIMargin | left | The first UIMargin to compare. |
| UIMargin | right | The second UIMargin to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator !=(UIMargin, UIMargin)
Returns a value indicating whether two UIMargin instances are not equal.
Declaration
public static bool operator !=(UIMargin left, UIMargin right)
Parameters
| Type | Name | Description |
|---|---|---|
| UIMargin | left | The first UIMargin to compare. |
| UIMargin | right | The second UIMargin to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator *(UIMargin, float)
Multiplies a UIMargin by a scalar value, scaling all four sides proportionally. Delegates to Scale(float).
Declaration
public static UIMargin operator *(UIMargin margin, float scalar)
Parameters
| Type | Name | Description |
|---|---|---|
| UIMargin | margin | The margin to scale. |
| float | scalar | The scalar multiplier. |
Returns
| Type | Description |
|---|---|
| UIMargin | A new scaled UIMargin. |
operator *(float, UIMargin)
Multiplies a scalar value by a UIMargin, scaling all four sides proportionally. Delegates to Scale(float). This overload supports scalar-on-the-left syntax.
Declaration
public static UIMargin operator *(float scalar, UIMargin margin)
Parameters
| Type | Name | Description |
|---|---|---|
| float | scalar | The scalar multiplier. |
| UIMargin | margin | The margin to scale. |
Returns
| Type | Description |
|---|---|
| UIMargin | A new scaled UIMargin. |
operator -(UIMargin, UIMargin)
Subtracts one UIMargin from another on a per-side basis. Useful for computing the difference between two margin values or for removing a known margin contribution from a cumulative value.
Declaration
public static UIMargin operator -(UIMargin a, UIMargin b)
Parameters
| Type | Name | Description |
|---|---|---|
| UIMargin | a | The margin to subtract from. |
| UIMargin | b | The margin to subtract from |
Returns
| Type | Description |
|---|---|
| UIMargin | A new UIMargin where each side equals the corresponding side of
|