HorizontalLayoutGroup 和 Vertical Layout Group

新建

  • 在物体的Inspector面板中添加组件(AddComponent->Horizontal Layout Group或者Vertical Layout Group)

演示

介绍

  • Horizontal Layout Group水平布局组组件将其子布局元素并排放置在一起。

  • Vertical Layout Group垂直布局组组件将子布局元素纵向放置。

  • Padding:布局组边缘内的填充。

  • Spacing:布局元素之间的间距。

  • Child Alignment:用于子布局元素的对齐方式(如果这些元素未填满可用空间)。

详细下拉选项见GridLayoutGroup中的Child Alignment

  • Control Child Size:布局组是否控制其子布局元素的宽度和高度。

  • Use Child Scale:在为元素调整大小和进行布局时,布局组是否考虑其子布局元素的缩放。Width 和 Height 对应于每个子布局元素的 Rect Transform 组件中的 Scale > X 和 Scale > Y 值。

  • Child Force Expand:是否要强制子布局元素扩展以填充额外的可用空间

代码示例

using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
    public HorizontalLayoutGroup testGroup;
    void Start()
    {
        testGroup.spacing = 20f;//设置间距
        testGroup.childForceExpandWidth = true;//设置子物体扩展宽度
        testGroup.childAlignment = TextAnchor.MiddleCenter;//设置布局规则
        testGroup.childControlHeight = false;//设置是否控制子物体的高度
        testGroup.childControlWidth = false;//设置是否控制子物体的宽度
    }
}

posted @   请明月  阅读(74)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示