GridLayoutGroup
新建
- 在物体的Inspector面板中添加组件(AddComponent->Grid Layout Group)
演示

介绍
Grid Layout Group网格布局组组件将其子布局元素放在网格中。

Padding:设置子对象之间的间距,如左移、右移等。Cell Size:指定每个单元格的大小。你可以设置宽度和高度,也可以将其中一个值设置为 0,以使 Grid Layout Group 自动计算大小。Spacing:布局元素之间的间距。Start Corner:指定网格的起始角落,可以是左上角、右上角、左下角或右下角。
![]()
UpperLeft:左上角
UpperRight:右上角
LowerLeft:左下角
LowerRight:右下角
Start Axis:沿哪个主轴放置元素。Horizontal 将在填充整行后才开始新行。Vertical 将在填充整列后才开始新列。
![]()
Horizontal:水平
Vertical:垂直
Child Alignment:用于布局元素的对齐方式(如果这些元素未填满可用空间)。
![]()
UpperLeft = 0, //上左
UpperCenter:上中
UpperRight:上右
MiddleLeft:中左
MiddleCenter:中中
MiddleRight:中右
LowerLeft:下左
LowerCenter:下中
LowerRight:下右
Constraint:将网格约束为固定数量的行或列以便支持自动布局系统。
![]()
Flexible:不限制行数和列数
FixedColumnCount:约束指定数量的列数
FixedRowCount:约束指定数量的行数
代码示例
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
public GridLayoutGroup testGroup;
void Start()
{
testGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;//设置约束条件为指定列数
testGroup.constraintCount = 2;//设置为2
testGroup.spacing = new Vector2(10, 10);//设置间距
}
}





浙公网安备 33010602011771号