跳转到内容

UI 布局

Estella 提供了完整的 UI 框架,包含布局、交互、文本渲染和一系列预制控件。布局计算在 C++/WASM 中运行以确保性能,所有组件通过 TypeScript 配置。

快速开始

在编辑器中:右键 Hierarchy → CreateUI → 选择一个控件(Button、Slider、Text 等)。编辑器会创建预配置好所有必需组件的实体层级。

通过代码创建 UI:

import { defineSystem, addStartupSystem, Commands, Transform, Sprite, UIRect, Text, Parent } from 'esengine';
addStartupSystem(defineSystem([Commands()], (cmds) => {
const root = cmds.spawn()
.insert(Transform)
.insert(UIRect, { anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 } })
.id();
cmds.spawn()
.insert(Transform)
.insert(Sprite)
.insert(UIRect, { size: { x: 200, y: 40 } })
.insert(Text, { content: 'Hello World', fontSize: 24 })
.insert(Parent, { entity: root });
}));

UIRect

每个 UI 元素都需要 UIRect 来参与布局。它使用锚点系统定义相对于父级的位置和尺寸。

属性类型默认值说明
anchorMinVec2{0.5, 0.5}左下锚点(0–1)
anchorMaxVec2{0.5, 0.5}右上锚点(0–1)
offsetMinVec2{0, 0}相对 anchorMin 的像素偏移
offsetMaxVec2{0, 0}相对 anchorMax 的像素偏移
sizeVec2{100, 100}宽高(锚点相等时使用)
pivotVec2{0.5, 0.5}旋转和缩放中心点

anchorMin 等于 anchorMax 时,元素在锚点处具有固定 size。当两者不同时,元素在锚点之间拉伸,尺寸自动计算。

FlexContainer

类似 CSS Flexbox 的自动子元素排列布局。

属性类型默认值说明
directionFlexDirectionRowRowColumnRowReverseColumnReverse
wrapFlexWrapNoWrapNoWrapWrap
justifyContentJustifyContentStartStartCenterEndSpaceBetweenSpaceAroundSpaceEvenly
alignItemsAlignItemsStretchStartCenterEndStretch
gapVec2{0, 0}子元素间距
paddingVec4{x:0, y:0, z:0, w:0}内边距,格式为 {x: 上, y: 右, z: 下, w: 左}

FlexItem

FlexContainer 内子实体的 Flex 属性。

属性类型默认值说明
flexGrownumber0扩展以填充空间的比例
flexShrinknumber1空间不足时收缩的比例
flexBasisnumber-1扩展/收缩前的基础尺寸(-1 = 自动)
ordernumber0显示顺序覆盖

LayoutGroup

带方向、间距和对齐的网格式布局。

属性类型默认值说明
directionLayoutDirectionHorizontalHorizontal (0)、Vertical (1)
spacingnumber0子元素间距
paddingobject{left:0, top:0, right:0, bottom:0}边距
childAlignmentChildAlignmentStartStartCenterEnd
reverseOrderbooleanfalse反向排列

下一步