Skip to content

EUIGridLayout

概览

字段
KindUnit · 单位 Unit
Realmcommon

继承关系

  • Unit(4 属性 / 25 函数 / 6 事件)
    • EUINodeBase(34 属性 / 8 函数 / 4 事件)
      • [EUIGridLayout](10 属性 / 1 函数)

继承成员

2 个来源 / 38 属性 / 33 函数 / 10 事件

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类型默认值说明
FillDirectionInt0 (Horizontal)网格的填充方向。
AutoLayoutBooltrue是否启用自动布局。
CellSizeVector2[100, 100]网格布局中每个单元格的尺寸,以像素为单位。
CellPaddingVector2[0, 0]网格布局中每个单元格之间的间距,以像素为单位。
FillDirectionMaxCellsInt0在填充方向上每行或每列的最大单元数。
StartCornerInt0 (TopLeft)网格布局的起始角落。
SortOrderInt0 (LayoutOrder)子节点的排序依据。
HorizontalAlignmentInt0 (Left)子节点在水平方向上的对齐方式。
VerticalAlignmentInt0 (Top)子节点在垂直方向上的对齐方式。
AutomaticSizeBoolfalse是否根据子节点内容自动调整容器尺寸。

关联类型

函数 (1)

ApplyLayout

签名:ApplyLayout() -> void

立即根据当前属性设置重新计算并应用网格布局。

返回值 void

示例代码

重新计算网格布局

lua
-- @runtime client
local function refreshGrid(grid)
    if grid == nil then return end
    grid:ApplyLayout()
end