Struct Size2D
Represents a 2D float-valued extent with a Width (X) and Height (Y) component. Intended as the canonical "size" type for any planar measurement: 2D shape dimensions, UI sizes, plane / quad footprints, sprite extents, rectangle dimensions.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Geom
Assembly: ScyllaCore.dll
Syntax
[Serializable]
public struct Size2D : IEquatable<Size2D>
Remarks
Size2D is a value type (struct) and is fully serializable by
Unity's Inspector. Equality uses
Approximately(float, float, float) so two
instances that differ only by floating-point rounding compare equal.
Implicit conversions to and from UnityEngine.Vector2 make it interoperate cleanly with every Unity API that takes or returns a vector. An explicit conversion to UnityEngine.Vector2Int rounds via RoundToInt(float); an explicit conversion from UnityEngine.Vector2Int widens losslessly to float.
If you need a truly integer-valued extent (grid coordinates, tile counts), use UnityEngine.Vector2Int directly. A typed integer-extent struct can be added back later if a concrete use case appears.
Constructors
Size2D(float)
Initializes a uniform Size2D where both components share the same
value (IsSquare will return true).
Declaration
public Size2D(float size)
Parameters
| Type | Name | Description |
|---|---|---|
| float | size | Common value assigned to both components. |
Size2D(float, float)
Initializes a new Size2D with the specified component values.
Declaration
public Size2D(float width, float height)
Parameters
| Type | Name | Description |
|---|---|---|
| float | width | Extent along the X axis. |
| float | height | Extent along the Y axis. |
Size2D(Vector2)
Initializes a new Size2D from a UnityEngine.Vector2, mapping its
x and y components to Width and Height.
Declaration
public Size2D(Vector2 size)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | size | The UnityEngine.Vector2 to convert. |
Size2D(Vector2Int)
Initializes a new Size2D from a UnityEngine.Vector2Int, widening
each integer component to float without loss.
Declaration
public Size2D(Vector2Int size)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2Int | size | The UnityEngine.Vector2Int to convert. |
Fields
Empty
A Size2D with both components set to zero.
Declaration
public static readonly Size2D Empty
Field Value
| Type | Description |
|---|---|
| Size2D |
Height
Extent along the Y axis. May be negative.
Declaration
[SerializeField]
public float Height
Field Value
| Type | Description |
|---|---|
| float |
One
A Size2D with both components set to one (unit square).
Declaration
public static readonly Size2D One
Field Value
| Type | Description |
|---|---|
| Size2D |
Width
Extent along the X axis. May be negative.
Declaration
[SerializeField]
public float Width
Field Value
| Type | Description |
|---|---|
| float |
Properties
Area
Declaration
public float Area { get; }
Property Value
| Type | Description |
|---|---|
| float |
AspectRatio
Ratio of Width to Height. Returns PositiveInfinity or NaN when Height is zero.
Declaration
public float AspectRatio { get; }
Property Value
| Type | Description |
|---|---|
| float |
IsEmpty
true when both components are approximately zero (using
Approximately(float, float, float)).
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsSquare
true when both components are approximately equal. Use to test whether this
size represents a uniform square.
Declaration
public bool IsSquare { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Max
The larger of the two components.
Declaration
public float Max { get; }
Property Value
| Type | Description |
|---|---|
| float |
Min
The smaller of the two components.
Declaration
public float Min { get; }
Property Value
| Type | Description |
|---|---|
| float |
Methods
Clamp(in Size2D, in Size2D, in Size2D)
Returns a Size2D whose components are clamped per-axis between the
corresponding components of min and max.
Declaration
public static Size2D Clamp(in Size2D value, in Size2D min, in Size2D max)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | value | The size whose components are to be clamped. |
| Size2D | min | The per-component lower bound. Each axis of |
| Size2D | max | The per-component upper bound. Each axis of |
Returns
| Type | Description |
|---|---|
| Size2D | A new Size2D where each component equals
|
Equals(Size2D)
Determines whether two Size2D values are approximately equal using Approximately(float, float, float) on both components.
Declaration
public bool Equals(Size2D other)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | other |
Returns
| Type | Description |
|---|---|
| bool |
Equals(object)
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj |
Returns
| Type | Description |
|---|---|
| bool |
Overrides
GetHashCode()
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int |
Overrides
Lerp(in Size2D, in Size2D, float)
Linearly interpolates between two sizes component-wise. t is not
clamped; values outside [0, 1] extrapolate beyond the a
and b endpoints.
Declaration
public static Size2D Lerp(in Size2D a, in Size2D b, float t)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | a | The start size, returned when |
| Size2D | b | The end size, returned when |
| float | t | The interpolation factor. Values in |
Returns
| Type | Description |
|---|---|
| Size2D | A new Size2D whose Width and Height are
each independently interpolated from the corresponding component of
|
Square(float)
Returns a uniform Size2D where both components equal
size. Convenience static equivalent to new Size2D(size).
Declaration
public static Size2D Square(float size)
Parameters
| Type | Name | Description |
|---|---|---|
| float | size |
Returns
| Type | Description |
|---|---|
| Size2D | A new Size2D with Width and Height both
set to |
ToString()
Returns a string in the format (Width, Height) for debugging, logging, and
Inspector display.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string |
Overrides
WithHeight(float)
Returns a copy of this size with Height replaced by
height. Width is preserved unchanged.
Useful for fluent construction when you need to modify a single axis.
Declaration
public Size2D WithHeight(float height)
Parameters
| Type | Name | Description |
|---|---|---|
| float | height | The new value for the Height component. |
Returns
| Type | Description |
|---|---|
| Size2D | A new Size2D with Height set to |
WithWidth(float)
Returns a copy of this size with Width replaced by
width. Height is preserved unchanged.
Useful for fluent construction when you need to modify a single axis.
Declaration
public Size2D WithWidth(float width)
Parameters
| Type | Name | Description |
|---|---|---|
| float | width | The new value for the Width component. |
Returns
| Type | Description |
|---|---|
| Size2D | A new Size2D with Width set to |
Operators
operator +(Size2D, Size2D)
Component-wise addition.
Declaration
public static Size2D operator +(Size2D a, Size2D b)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | a | |
| Size2D | b |
Returns
| Type | Description |
|---|---|
| Size2D |
operator /(Size2D, float)
Uniform division by a scalar. Standard C# float-division semantics apply: division by zero produces PositiveInfinity or NaN rather than throwing.
Declaration
public static Size2D operator /(Size2D size, float scalar)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | size | |
| float | scalar |
Returns
| Type | Description |
|---|---|
| Size2D |
operator ==(Size2D, Size2D)
Approximate equality via Equals(Size2D).
Declaration
public static bool operator ==(Size2D left, Size2D right)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | left | |
| Size2D | right |
Returns
| Type | Description |
|---|---|
| bool |
explicit operator Vector2Int(Size2D)
Explicit conversion to UnityEngine.Vector2Int; each component is rounded via RoundToInt(float). Use this when a Unity API requires an integer vector and you accept the rounding.
Declaration
public static explicit operator Vector2Int(Size2D size)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | size |
Returns
| Type | Description |
|---|---|
| Vector2Int |
explicit operator Size2D(Vector2Int)
Explicit conversion from UnityEngine.Vector2Int (lossless widening to float).
Declaration
public static explicit operator Size2D(Vector2Int vector)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2Int | vector |
Returns
| Type | Description |
|---|---|
| Size2D |
implicit operator Vector2(Size2D)
Implicit conversion to UnityEngine.Vector2.
Declaration
public static implicit operator Vector2(Size2D size)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | size |
Returns
| Type | Description |
|---|---|
| Vector2 |
implicit operator Size2D(Vector2)
Implicit conversion from UnityEngine.Vector2.
Declaration
public static implicit operator Size2D(Vector2 vector)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | vector |
Returns
| Type | Description |
|---|---|
| Size2D |
operator !=(Size2D, Size2D)
Negation of operator ==(Size2D, Size2D).
Declaration
public static bool operator !=(Size2D left, Size2D right)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | left | |
| Size2D | right |
Returns
| Type | Description |
|---|---|
| bool |
operator *(Size2D, Size2D)
Component-wise multiplication.
Declaration
public static Size2D operator *(Size2D a, Size2D b)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | a | |
| Size2D | b |
Returns
| Type | Description |
|---|---|
| Size2D |
operator *(Size2D, float)
Uniform scaling by a scalar.
Declaration
public static Size2D operator *(Size2D size, float scalar)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | size | |
| float | scalar |
Returns
| Type | Description |
|---|---|
| Size2D |
operator *(float, Size2D)
Uniform scaling by a scalar (left-hand-side overload).
Declaration
public static Size2D operator *(float scalar, Size2D size)
Parameters
| Type | Name | Description |
|---|---|---|
| float | scalar | |
| Size2D | size |
Returns
| Type | Description |
|---|---|
| Size2D |
operator -(Size2D, Size2D)
Component-wise subtraction.
Declaration
public static Size2D operator -(Size2D a, Size2D b)
Parameters
| Type | Name | Description |
|---|---|---|
| Size2D | a | |
| Size2D | b |
Returns
| Type | Description |
|---|---|
| Size2D |