AspectRatioContainer
Places child Controls in a rectangle with a requested width-to-height ratio.
Set Ratio to width divided by height, for example 16f / 9f. Supply a positive finite value: the setter does not validate zero, negative values, NaN or infinity. The inspector hint does not replace input validation in code. Defaults are Ratio = 1, StretchMode = Fit and Center on both axes.
Fit keeps the entire target rectangle inside; Cover fills the container while overflowing one dimension. WidthControlsHeight uses container width to calculate height; HeightControlsWidth does the reverse. In a 100 × 100 area with Ratio = 2 and centered alignment, Fit produces size 100 × 50 at (0, 25); Cover produces 200 × 100 at (-50, 0). Cover neither enables ClipContents nor selects how a TextureRect displays its texture.
Ratio is a layout target rather than an unconditional guarantee. Child minimum size is applied separately per axis: a minimum height of 80 changes the Fit example to 100 × 80. Child SizeFlags and maximum size also affect final layout. Check those constraints first if the ratio is not preserved.
Layout is deferred. Eligible direct child Controls receive the same target rectangle and can overlap; this is not a list or grid. The container replaces their Position/Size and resets Rotation/Scale. Horizontal Begin/End account for RTL.
Do not use FitWidthProportional or FitHeightProportional ExpandMode on a child TextureRect: this combination is unsupported and the element is skipped during layout. Choose a different ExpandMode and configure the TextureRect’s own StretchMode separately.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
AspectRatioContainerConstructor
public AspectRatioContainer();Creates a container with Ratio = 1, StretchMode = Fit and Center on both axes. Layout changes child rectangles rather than automatically scaling an image.
Properties4
AlignmentHorizontalProperty
public AspectRatioContainer.AlignmentMode AlignmentHorizontal { get; set; }Begin/Center/End places the target rectangle horizontally; default Center. Start and end mirror in RTL. Overflow can produce negative offsets.
AlignmentVerticalProperty
public AspectRatioContainer.AlignmentMode AlignmentVertical { get; set; }Begin/Center/End places the target rectangle at the top, center or bottom; default Center. Does not change the child’s own vertical SizeFlags.
RatioProperty
public float Ratio { get; set; }Target child width divided by height, default 1. Supply a positive finite number. Child minima, maxima and SizeFlags can alter the final ratio; changes queue layout.
StretchModeProperty
public AspectRatioContainer.StretchModeEnum StretchMode { get; set; }How the target rectangle is calculated: Fit fits inside, Cover fills with possible overflow, WidthControlsHeight/HeightControlsWidth use one controlling axis. Default Fit. Cover does not enable clipping.
Methods8
GetAlignmentHorizontalAspectRatioContainer.AlignmentMode
public AspectRatioContainer.AlignmentMode GetAlignmentHorizontal();Begin/Center/End places the target rectangle horizontally; default Center. Start and end mirror in RTL. Overflow can produce negative offsets.
AspectRatioContainer.AlignmentModeStored setting rather than measured child size or ratio after layout.
GetAlignmentVerticalAspectRatioContainer.AlignmentMode
public AspectRatioContainer.AlignmentMode GetAlignmentVertical();Begin/Center/End places the target rectangle at the top, center or bottom; default Center. Does not change the child’s own vertical SizeFlags.
AspectRatioContainer.AlignmentModeStored setting rather than measured child size or ratio after layout.
GetRatiofloat
public float GetRatio();Target child width divided by height, default 1. Supply a positive finite number. Child minima, maxima and SizeFlags can alter the final ratio; changes queue layout.
floatStored setting rather than measured child size or ratio after layout.
GetStretchModeAspectRatioContainer.StretchModeEnum
public AspectRatioContainer.StretchModeEnum GetStretchMode();How the target rectangle is calculated: Fit fits inside, Cover fills with possible overflow, WidthControlsHeight/HeightControlsWidth use one controlling axis. Default Fit. Cover does not enable clipping.
AspectRatioContainer.StretchModeEnumStored setting rather than measured child size or ratio after layout.
SetAlignmentHorizontalvoid
public void SetAlignmentHorizontal(AspectRatioContainer.AlignmentMode alignmentHorizontal);Begin/Center/End places the target rectangle horizontally; default Center. Start and end mirror in RTL. Overflow can produce negative offsets.
| Name | Type | Default |
|---|---|---|
alignmentHorizontal | AspectRatioContainer.AlignmentMode | — |
alignmentHorizontalHorizontal Begin, Center or End respecting RTL.
SetAlignmentVerticalvoid
public void SetAlignmentVertical(AspectRatioContainer.AlignmentMode alignmentVertical);Begin/Center/End places the target rectangle at the top, center or bottom; default Center. Does not change the child’s own vertical SizeFlags.
| Name | Type | Default |
|---|---|---|
alignmentVertical | AspectRatioContainer.AlignmentMode | — |
alignmentVerticalBegin at the top, Center in the middle, End at the bottom.
SetRatiovoid
public void SetRatio(float ratio);Target child width divided by height, default 1. Supply a positive finite number. Child minima, maxima and SizeFlags can alter the final ratio; changes queue layout.
| Name | Type | Default |
|---|---|---|
ratio | float | — |
ratioPositive finite width-to-height ratio, for example 16f / 9f. Validate external data before calling.
SetStretchModevoid
public void SetStretchMode(AspectRatioContainer.StretchModeEnum stretchMode);How the target rectangle is calculated: Fit fits inside, Cover fills with possible overflow, WidthControlsHeight/HeightControlsWidth use one controlling axis. Default Fit. Cover does not enable clipping.
| Name | Type | Default |
|---|---|---|
stretchMode | AspectRatioContainer.StretchModeEnum | — |
stretchModeFit, Cover, WidthControlsHeight or HeightControlsWidth. Configure content clipping separately.
AspectRatioContainer.AlignmentMode
| Value | Description |
|---|---|
Begin = 0 | Aligns child controls with the beginning (left or top) of the container. |
Center = 1 | Aligns child controls with the center of the container. |
End = 2 | Aligns child controls with the end (right or bottom) of the container. |
AspectRatioContainer.StretchModeEnum
| Value | Description |
|---|---|
Cover = 3 | The width and height of child controls is automatically adjusted to make their bounding rectangle cover the entire area of the container while keeping the aspect ratio.
When the bounding rectangle of child controls exceed the container's size and |
Fit = 2 | The bounding rectangle of child controls is automatically adjusted to fit inside the container while keeping the aspect ratio. |
HeightControlsWidth = 1 | The width of child controls is automatically adjusted based on the height of the container. |
WidthControlsHeight = 0 | The height of child controls is automatically adjusted based on the width of the container. |