主题
EUIGridLayout
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- EUINodeBase(34 属性 / 8 函数 / 4 事件)
- [EUIGridLayout](10 属性 / 1 函数)
- EUINodeBase(34 属性 / 8 函数 / 4 事件)
继承成员
2 个来源 / 38 属性 / 33 函数 / 10 事件
- 来自 EUINodeBase(34 属性 / 8 函数 / 4 事件)
- 属性:
Visible、Position、Anchor、FlippedX、FlippedY、Size、Rotation、Opacity、TouchBeginAudio、TouchEndAudio、TouchClickAudio、TouchbeginEvent、ClickEvent、TouchendEvent、LongtouchEvent、ShowEvent、HideEvent、TouchEnabled、SwallowTouchEnabled、TopAdaptMode、TopAdaption、BottomAdaptMode、BottomAdaption、LeftAdaptMode、LeftAdaption、RightAdaptMode、RightAdaption、AnchorXAdaptMode、AnchorXAdaption、AnchorYAdaptMode、AnchorYAdaption、LocalZOrder、LayoutOrder、Scale - 函数:
RemoveFromParent、GetFullPath、TweenPosition、TweenSize、TweenSizeAndPosition、TweenOpacity、Retain、Release - 事件:
OnTouchBegan、OnTouchMoved、OnTouchEnded、OnClicked
- 属性:
- 来自 Unit(4 属性 / 25 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
EUIGridLayout 是 UI 网格布局容器,负责将子节点按网格排列。它支持设置单元格尺寸、间距、填充方向、对齐方式等属性,并通过 ApplyLayout 方法刷新布局。
适用场景
在背包、商店等需要整齐排列多个同类 UI 元素的界面中,使用 EUIGridLayout 自动管理子节点的位置和排序。
使用要点
通过 World:CreateUnit 创建实例,将 Parent 设为玩家根节点,然后设置 CellSize、CellPadding、FillDirection 等属性,最后调用 ApplyLayout 使布局生效。子节点通过 LayoutOrder 控制排列顺序。
注意事项
EUIGridLayout 是 UI 网格布局容器,通过 World:CreateUnit 创建,并把 Parent 设置为当前玩家 PlayerGui.EuiManager:GetRootNode()(不能自行直接构造,也不能通过 GetService 获取 EUIManager)。设置布局属性后需调用 ApplyLayout 刷新子节点位置;子节点通过 LayoutOrder 控制排序。
代码示例
创建并配置网格布局
lua
-- @runtime client
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local rootNode = localPlayer.PlayerGui.EuiManager:GetRootNode()
local World = game:GetService("World")
local grid = World:CreateUnit("EUIGridLayout", {
Parent = rootNode,
CellSize = Vector2.New(96, 96),
CellPadding = Vector2.New(8, 8),
FillDirectionMaxCells = 4,
AutoLayout = true,
})
grid:ApplyLayout()属性 (10)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
FillDirection | Int | 0 (Horizontal) | 网格的填充方向。 |
AutoLayout | Bool | true | 是否启用自动布局。 |
CellSize | Vector2 | [100, 100] | 网格布局中每个单元格的尺寸,以像素为单位。 |
CellPadding | Vector2 | [0, 0] | 网格布局中每个单元格之间的间距,以像素为单位。 |
FillDirectionMaxCells | Int | 0 | 在填充方向上每行或每列的最大单元数。 |
StartCorner | Int | 0 (TopLeft) | 网格布局的起始角落。 |
SortOrder | Int | 0 (LayoutOrder) | 子节点的排序依据。 |
HorizontalAlignment | Int | 0 (Left) | 子节点在水平方向上的对齐方式。 |
VerticalAlignment | Int | 0 (Top) | 子节点在垂直方向上的对齐方式。 |
AutomaticSize | Bool | false | 是否根据子节点内容自动调整容器尺寸。 |
关联类型
函数 (1)
ApplyLayout
签名:ApplyLayout() -> void
立即根据当前属性设置重新计算并应用网格布局。
返回值 void
示例代码
重新计算网格布局
lua
-- @runtime client
local function refreshGrid(grid)
if grid == nil then return end
grid:ApplyLayout()
end