本文档详细说明 Unity GameObject Layout Group 插件的使用方法。该插件在 Unity 内置 Layout Group 基础上扩展,支持 Grid 网格布局、Sector 扇形布局、Tween 动画入场 以及 Sprite/Rect 双模式。
视频:Unity 自研布局插件!Grid+Sector+Tween一键搞定_哔哩哔哩_bilibili
常见问题 FAQ
GameObject Layout Group 是一款为 Unity 开发的布局组件扩展插件,在原生 Horizontal/Vertical/Grid Layout Group 基础上新增两大核心能力:
代码块
UnityGameObjectLayoutGroup/
├── Editor/ # 编辑器显示
│ ├── GameObjectLayoutGroupEditor.cs
├── Runtime/
│ ├── GameObjectLayoutGroup.cs
│ └── GameObjectLayoutGroup.Rect.cs
│ └── GameObjectLayoutGroup.Sprite
│ └── GameObjectLayoutGroup.Tween
Gitee : https://gitee.com/NuoYanRuoShui/UnityGameObjectLayoutGroup.git
Github:网页链接
以下创建一个扇形展开的卡牌手牌效果
Step 1:在 Hierarchy 中创建空物体,命名为 CardHand
Step 2:为 CardHand 添加 Sector Layout Group 组件
代码块
GameObject "CardHand" → Add Component → Sector Layout Group
Step 3:设置扇形参数
Step 4:为子物体添加入场动画
Step 5:创建子卡牌预制体并添加为子物体
代码块
// 代码方式动态添加子物体
GameObject cardPrefab = Resources.Load<GameObject>("Card");
for (int i = 0; i < 5; i++)
{
GameObject card = Instantiate(cardPrefab, cardHand.transform);
}
在 Inspector 面板右侧找到 Play 按钮,点击可预览 Tween 动画效果,无需进入 Play Mode。
在不启用Sector模式下默认为网格模式,此模式下的使用与Unity自身的组件相同
子物体尺寸控制
⚠️ 注意:启用 Control Child Size 后,Cell Size 参数变为可编辑状态,可精确控制每个格子的尺寸。
运行时控制
案例 1:装备栏(3行4列)
代码块
Axis: Horizontal
Start Corner: Upper Left
Start Axis: Horizontal
Cell Size: (80, 80)
Spacing: (5, 5)
Control Child Size: Width ✅, Height ✅
案例 2:技能栏(单行动态扩展)
代码块
Axis: Horizontal
Start Corner: Upper Center
Start Axis: Horizontal
Cell Size: (64, 64)
Spacing: (8, 0)
案例 3:商品列表(多行自适应)
代码块
Axis: Vertical
Start Corner: Upper Left
Start Axis: Horizontal
Cell Size: (200, 250)
Spacing: (15, 15)
Eanble Sector 设置为True
在游戏 UI 中,经常需要将物体排列成弧形,例如:
原生 Unity Layout Group 无法实现这种效果,Sector Layout Group 正是为此而生。
代码块
子物体位置计算公式:
angle_i = (i / (childCount - 1)) * TotalAngle - (TotalAngle / 2)
x = Radius * cos(angle_i)
y = Radius * sin(angle_i)
// 如果启用了 Control Child Rotation:
rotation_i = atan2(y, x) // 子物体朝向圆心
案例 1:卡牌手牌
代码块
Enable Sector: ✅
Axis: Horizontal
Angle: 120
Radius: 180
Cell Size: (120, 180)
Spacing: (8, 0)
Start Corner: Upper Center
Control Child Rotation: ✅
Child Alignment: Middle Center
Enable Tween: ✅
Tween Ease: Out Back
Duration: 0.3
Delay Between: 0.1
Start Scale: (0, 0, 0)
Start Point: (0, 300)
案例 2:技能图标环绕
代码块
Enable Sector: ✅
Axis: Vertical
Angle: 180
Radius: 120
Cell Size: (64, 64)
Spacing: (15, 15)
Start Corner: Upper Center
Control Child Rotation: ✅
Child Alignment: Middle Center
案例 3:头像环绕展示
代码块
Enable Sector: ✅
Axis: Vertical
Angle: 360
Radius: 200
Cell Size: (80, 80)
Start Corner: Upper Center
Control Child Rotation: ❌
Child Alignment: Middle Center
Enable Tween: ✅
Tween Ease: Out Elastic
Duration: 0.5
Start Scale: (0.5, 0.5, 0)
基于DoTween动画系统。它为子物体的入场效果提供了一整套可配置的补间动画,无需编写额外代码。
运动参数(Movement)
缩放参数(Scale)
旋转参数(Rotation)
路径参数(Path Arc)
常用推荐
完整缓动曲线列表
运动类(Movement)
专用类
代码块
Linear: _______________ (匀速)
Out Back: ___________/ (冲出后回弹)
Out Bounce: __________/ /\ (弹跳)
Out Elastic:__________/~~~~ (橡皮筋)
In Out Quad: /"""""""\ (平滑)
示例 1:卡牌扇形入场
代码块
Enable Tween: ✅
Duration: 0.3
Delay Between: 0.1
Tween Ease: Out Back
Start Point: (0, 500)
Start Scale: (0.1, 0.1, 0)
Start Angle: -10
Rotate Ease: Out Back
Path Arc Height: 30
示例 2:技能图标淡入缩放
代码块
Enable Tween: ✅
Duration: 0.4
Delay Between: 0.05
Tween Ease: Out Quad
Start Point: (0, 0)
Start Scale: (0, 0, 0)
Path Arc Height: 0
示例 3:道具弹跳效果
代码块
Enable Tween: ✅
Duration: 0.5
Delay Between: 0.15
Tween Ease: Out Bounce
Start Point: (0, -200)
Start Scale: (1, 1, 1)
适用场景:子物体为普通 GameObject,带有 Sprite Renderer(2D游戏、3D场景中的Sprite)。
开启方式:在组件面板中勾选 Use Sprite Extension
特点:
检查项:
// 强制刷新
layoutGroup.UpdateLayout();
检查项:
原因:默认 Control Child Size 是关闭的,此时 Cell Size 不可编辑。
解决:勾选 Control Child Size - Width 和 Height
原因:Start Corner 和 Angle 配合不当。
解决:确认 Start Corner 设置在扇形的对称轴方向。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删