UITransform
is a transform component specifically designed for UI elements, inheriting from the base Transform component. It provides rich layout and alignment features and is a core component for building flexible UI systems.
When a node with UI components is added, the UITransform
component will be automatically added (replacing the previous Transform component). In the editor, you can select the node and use the RectTool
(shortcut key T
) to quickly set properties, or set precise properties in the Inspector Panel.
When the main canvas's render mode is
Screen
, the editor will prohibit modifications to itstransform
to avoid screen adaptation issues. Therefore, in scripts, developers should avoid modifying thetransform
properties of the main canvas in screen space.
Property Name | Description |
---|---|
size | The size of the UI element. x represents width, and y represents height. Both default to 100 |
pivot | The anchor point of the UI element. It is a normalized 2D vector with the origin at the bottom-left corner, with the default value being the center point (0.5, 0.5) |
horizontalAlignment | Horizontal relative layout mode HorizontalAlignment |
alignLeft | Distance from the parent element's left edge, effective only when HorizontalAlignment.Left or HorizontalAlignment.LeftAndRight |
alignRight | Distance from the parent element's right edge, effective only when HorizontalAlignment.Right or HorizontalAlignment.LeftAndRight |
alignCenter | Horizontal center offset, effective only when HorizontalAlignment.Center |
verticalAlignment | Vertical relative layout mode VerticalAlignment |
alignTop | Distance from the parent element's top edge, effective only when VerticalAlignment.Top or VerticalAlignment.TopAndBottom |
alignBottom | Distance from the parent element's bottom edge, effective only when VerticalAlignment.Bottom or VerticalAlignment.TopAndBottom |
alignMiddle | Vertical center offset, effective only when VerticalAlignment.Middle |
Relative layout properties only take effect under the corresponding layout mode.
// Add canvas
const canvasEntity = root.createChild("canvas");
const canvas = canvasEntity.addComponent(UICanvas);
const imageEntity = canvasEntity.create("Image");
const uiTransform = <UITransform>imageEntity.transform;
// Set size
uiTransform.size = new Vector2(200, 100);
// Set pivot
uiTransform.pivot = new Vector2(0.5, 0.5);
// Left align, 10 pixels from left edge
uiTransform.horizontalAlignment = HorizontalAlignmentMode.Left;
uiTransform.alignLeft = 10;
// Right align, 20 pixels from right edge
uiTransform.horizontalAlignment = HorizontalAlignmentMode.Right;
uiTransform.alignRight = 20;
// Horizontal center, offset 15 pixels to the right
uiTransform.horizontalAlignment = HorizontalAlignmentMode.Center;
uiTransform.alignCenter = 15;
// Horizontal stretch, 30 pixels margin on both sides
uiTransform.horizontalAlignment = HorizontalAlignmentMode.LeftAndRight;
uiTransform.alignLeft = 30;
uiTransform.alignRight = 30;
// Top align, 10 pixels from top
uiTransform.verticalAlignment = VerticalAlignmentMode.Top;
uiTransform.alignTop = 10;
// Bottom align, 20 pixels from bottom
uiTransform.verticalAlignment = VerticalAlignmentMode.Bottom;
uiTransform.alignBottom = 20;
// Vertical center, offset 5 pixels up
uiTransform.verticalAlignment = VerticalAlignmentMode.Middle;
uiTransform.alignMiddle = 5;
// Vertical stretch, 25 pixels margin on top and bottom
uiTransform.verticalAlignment = VerticalAlignmentMode.TopAndBottom;
uiTransform.alignTop = 25;
uiTransform.alignBottom = 25;
Using LeftAndRight
and TopAndBottom
alignment modes can create responsive layouts where elements automatically adjust according to the parent container's size.
// Create an element that always matches the parent element's size
uiTransform.horizontalAlignment = HorizontalAlignmentMode.LeftAndRight;
uiTransform.verticalAlignment = VerticalAlignmentMode.TopAndBottom;
uiTransform.alignLeft = uiTransform.alignRight = 0;
uiTransform.alignTop = uiTransform.alignBottom = 0;
position
will be overridden by alignment calculationsLeftAndRight
or TopAndBottom
, the corresponding direction's size
will be automatically calculatedUITransform
is a transform component specifically designed for UI elements in the Galacean engine, inheriting from the base [Transform](/apis/core/#Transform)
component. It provides rich layout and alignment features and is a core component for building flexible UI systems.
UITransform provides four horizontal alignment modes:
Similarly provides four vertical alignment modes:
get size(): Vector2
set size(value: Vector2)
Width and height of the UI element.
Example:
// Set UI element size to 200x100
uiTransform.size = new Vector2(200, 100);
get pivot(): Vector2
set pivot(value: Vector2)
Anchor point of the UI element, ranging from (0,0) to (1,1).
Example:
// Set pivot to center
uiTransform.pivot = new Vector2(0.5, 0.5);
// Set pivot to bottom-left corner
uiTransform.pivot = new Vector2(0, 0);
get horizontalAlignment(): HorizontalAlignmentMode
set horizontalAlignment(value: HorizontalAlignmentMode)
Horizontal alignment mode that controls the element's horizontal position within its parent container.
get alignLeft(): number
set alignLeft(value: number)
Left margin, only effective when horizontalAlignment
is Left
or LeftAndRight
.
get alignRight(): number
set alignRight(value: number)
Right margin, only effective when horizontalAlignment
is Right
or LeftAndRight
.
get alignCenter(): number
set alignCenter(value: number)
Horizontal center offset, only effective when horizontalAlignment
is Center
.
get verticalAlignment(): VerticalAlignmentMode
set verticalAlignment(value: VerticalAlignmentMode)
Vertical alignment mode that controls the element's vertical position within its parent container.
get alignTop(): number
set alignTop(value: number)
Top margin, only effective when verticalAlignment
is Top
or TopAndBottom
.
get alignBottom(): number
set alignBottom(value: number)
Bottom margin, only effective when verticalAlignment
is Bottom
or TopAndBottom
.
get alignMiddle(): number
set alignMiddle(value: number)
Vertical center offset, only effective when verticalAlignment
is Middle
.
import { UITransform, HorizontalAlignmentMode, VerticalAlignmentMode } from "@galacean/engine";
// Get the Transform component of the UI element
const uiTransform = entity.getComponent(UITransform);
// Set size
uiTransform.size = new Vector2(200, 100);
// Set pivot to center
uiTransform.pivot = new Vector2(0.5, 0.5);
// Left align, 10 pixels from left edge
uiTransform.horizontalAlignment = HorizontalAlignmentMode.Left;
uiTransform.alignLeft = 10;
// Right align, 20 pixels from right edge
uiTransform.horizontalAlignment = HorizontalAlignmentMode.Right;
uiTransform.alignRight = 20;
// Horizontal center, offset 15 pixels to the right
uiTransform.horizontalAlignment = HorizontalAlignmentMode.Center;
uiTransform.alignCenter = 15;
// Horizontal stretch, 30 pixels margin on both sides
uiTransform.horizontalAlignment = HorizontalAlignmentMode.LeftAndRight;
uiTransform.alignLeft = 30;
uiTransform.alignRight = 30;
// Top align, 10 pixels from top
uiTransform.verticalAlignment = VerticalAlignmentMode.Top;
uiTransform.alignTop = 10;
// Bottom align, 20 pixels from bottom
uiTransform.verticalAlignment = VerticalAlignmentMode.Bottom;
uiTransform.alignBottom = 20;
// Vertical center, offset 5 pixels up
uiTransform.verticalAlignment = VerticalAlignmentMode.Middle;
uiTransform.alignMiddle = 5;
// Vertical stretch, 25 pixels margin on top and bottom
uiTransform.verticalAlignment = VerticalAlignmentMode.TopAndBottom;
uiTransform.alignTop = 25;
uiTransform.alignBottom = 25;
// Create a UI element in the top-right corner of parent container
uiTransform.horizontalAlignment = HorizontalAlignmentMode.Right;
uiTransform.verticalAlignment = VerticalAlignmentMode.Top;
uiTransform.alignRight = 10; // 10 pixels from right
uiTransform.alignTop = 10; // 10 pixels from top
// Create a UI element that fills the parent container (with margins)
uiTransform.horizontalAlignment = HorizontalAlignmentMode.LeftAndRight;
uiTransform.verticalAlignment = VerticalAlignmentMode.TopAndBottom;
uiTransform.alignLeft = 20;
uiTransform.alignRight = 20;
uiTransform.alignTop = 20;
uiTransform.alignBottom = 20;
Using LeftAndRight
and TopAndBottom
alignment modes can create responsive layouts where elements automatically adjust according to the parent container's size.
// Create an element that always occupies 80% of the parent container
uiTransform.horizontalAlignment = HorizontalAlignmentMode.LeftAndRight;
uiTransform.verticalAlignment = VerticalAlignmentMode.TopAndBottom;
const margin = parentSize * 0.1; // 10% margin
uiTransform.alignLeft = margin;
uiTransform.alignRight = margin;
uiTransform.alignTop = margin;
uiTransform.alignBottom = margin;
Choose appropriate pivots based on the UI element's purpose:
position
may be overridden by alignment calculationsLeftAndRight
or TopAndBottom
, the corresponding direction's size
will be automatically calculatedThe UITransform component provides powerful and flexible UI layout capabilities for the Galacean engine. By properly using these features, you can build beautiful UI interfaces that adapt to different screen sizes.