主题
EUIGridLayout
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- EUINodeBase(35 属性 / 12 函数 / 4 事件)
- [EUIGridLayout](10 属性 / 1 函数)
- EUINodeBase(35 属性 / 12 函数 / 4 事件)
继承成员
2 个来源 / 39 属性 / 36 函数 / 10 事件
- 来自 EUINodeBase(35 属性 / 12 函数 / 4 事件)
- 属性:
Visible、Position、Anchor、FlippedX、FlippedY、Size、Rotation、Opacity、TouchBeginAudio、TouchEndAudio、TouchClickAudio、TouchbeginEvent、ClickEvent、TouchendEvent、LongtouchEvent、ShowEvent、HideEvent、TouchEnabled、TouchDragEnabled、SwallowTouchEnabled、TopAdaptMode、TopAdaption、BottomAdaptMode、BottomAdaption、LeftAdaptMode、LeftAdaption、RightAdaptMode、RightAdaption、AnchorXAdaptMode、AnchorXAdaption、AnchorYAdaptMode、AnchorYAdaption、LocalZOrder、LayoutOrder、Scale - 函数:
RemoveFromParent、GetFullPath、TweenPosition、TweenSize、TweenSizeAndPosition、TweenOpacity、Retain、Release、GetWorldPosition、ConvertToWorldPosition、ConvertToLocalPosition、HitTest - 事件:
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、GetDebugId - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
EUIGridLayout 是用于把子节点按网格形式排布的界面布局容器,可配置填充方向、单元尺寸与单元间距、主轴最大单元数、起始角落、排序依据以及水平与垂直对齐方式,并通过自动扩容属性决定容器是否随内容变化调整自身。布局参数发生变化后需要调用 ApplyLayout 让容器重新计算并刷新子节点位置。
适用场景
适合在界面根节点下把多个同级子节点组织成行列整齐、间距一致的网格区域,例如让图标或按钮按固定单元尺寸等距排列。
使用要点
先由 game:GetService("Players").LocalPlayer 取到本地玩家,再通过其 PlayerGui.EuiManager:GetRootNode() 获得界面根节点,然后用 game:GetService("World"):CreateUnit("EUIGridLayout", { Parent = rootNode }) 创建实例。创建后依次设置 CellSize、CellPadding、FillDirection、FillDirectionMaxCells、StartCorner、SortOrder、HorizontalAlignment、VerticalAlignment 与 AutomaticSize,其中枚举属性可取值如 Enums.EUIFillDirection.Horizontal、Enums.EUIGridStartCorner.TopLeft。最后调用 ApplyLayout() 让布局生效并刷新子节点位置。
注意事项
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 | EUIFillDirection | Enums.EUIFillDirection.Horizontal | 指定网格单元依次填充的方向,取值来自 Enums.EUIFillDirection 枚举。 |
AutoLayout | Bool | true | 控制 EUIGridLayout 是否启用自动布局。开启后,容器内的子节点会按照本实例配置的网格规则自动排列。 |
CellSize | Vector2 | [100, 100] | EUIGridLayout 的 CellSize 属性,类型为 Vector2,用于描述网格布局中每个单元的尺寸,单位为像素。 |
CellPadding | Vector2 | [0, 0] | EUIGridLayout 的 CellPadding 属性,类型为 Vector2,用于描述网格布局中单元之间的间距,单位为像素。 |
FillDirectionMaxCells | Int | 0 | 限制填充方向上单行或单列最多容纳的单元数量,取整数。 |
StartCorner | EUIGridStartCorner | Enums.EUIGridStartCorner.TopLeft | 指定网格填充的起始角落,取值来自 Enums.EUIGridStartCorner 枚举。 |
SortOrder | EUISortOrderType | Enums.EUISortOrderType.LayoutOrder | 指定网格内子节点排列时采用的排序依据,取值来自 Enums.EUISortOrderType 枚举。 |
HorizontalAlignment | EUIHorizontalAlignment | Enums.EUIHorizontalAlignment.Left | 设置网格整体在水平方向上的对齐方式,取值来自 Enums.EUIHorizontalAlignment 枚举。 |
VerticalAlignment | EUIVerticalAlignment | Enums.EUIVerticalAlignment.Top | 设置网格整体在垂直方向上的对齐方式,取值来自 Enums.EUIVerticalAlignment 枚举。 |
AutomaticSize | Bool | false | 控制 EUIGridLayout 是否根据子节点数量自动扩展容器尺寸。 |
关联类型
- EUINodeBase
- EUIFillDirection
- EUIGridStartCorner
- EUIHorizontalAlignment
- EUISortOrderType
- EUIVerticalAlignment
- Vector2
函数 (1)
ApplyLayout
签名:ApplyLayout() -> void
按照 EUIGridLayout 实例当前配置的布局属性(单元尺寸、间距、填充方向、对齐方式等)重新排列子节点,使布局立即生效。
返回值 void
示例代码
重新计算网格布局
lua
-- @runtime client
local function refreshGrid(grid)
if grid == nil then return end
grid:ApplyLayout()
end