翼度科技»论坛 编程开发 .net 查看内容

Unity UGUI的VerticalLayoutGroup(垂直布局)组件的介绍及使用

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
Unity UGUI的VerticalLayoutGroup(垂直布局)组件的介绍及使用

1. 什么是VerticalLayoutGroup组件?

VerticalLayoutGroup是Unity UGUI中的一种布局组件,用于在垂直方向上自动排列子对象。它可以根据子对象的大小和布局设置,自动调整子对象的位置和大小,实现垂直布局效果。
2. VerticalLayoutGroup组件的工作原理

VerticalLayoutGroup组件通过以下步骤实现垂直布局:

  • 获取所有子对象的RectTransform组件。
  • 根据子对象的大小和布局设置,计算子对象的位置和大小。
  • 调整子对象的位置和大小,使其按照垂直方向上的布局要求进行排列。
3. VerticalLayoutGroup组件的常用属性


  • Padding:内边距,控制子对象与VerticalLayoutGroup之间的间距。
  • Spacing:子对象之间的间距。
  • Child Force Expand:子对象是否自动扩展以填充VerticalLayoutGroup的宽度。
  • Child Control Height:子对象是否自动调整高度以适应VerticalLayoutGroup的高度。
  • Child Alignment:子对象的对齐方式。
4. VerticalLayoutGroup组件的常用函数


  • CalculateLayoutInputHorizontal():计算垂直布局的水平方向上的布局要求。
  • CalculateLayoutInputVertical():计算垂直布局的垂直方向上的布局要求。
  • SetLayoutHorizontal():设置垂直布局的水平方向上的布局。
  • SetLayoutVertical():设置垂直布局的垂直方向上的布局。
5. 例子代码

例子1:创建一个垂直布局的面板
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class VerticalLayoutExample : MonoBehaviour
  4. {
  5.     public VerticalLayoutGroup verticalLayoutGroup;
  6.     private void Start()
  7.     {
  8.         verticalLayoutGroup.spacing = 10f;
  9.         verticalLayoutGroup.childForceExpandWidth = false;
  10.         verticalLayoutGroup.childForceExpandHeight = false;
  11.         verticalLayoutGroup.childControlHeight = true;
  12.         verticalLayoutGroup.childAlignment = TextAnchor.UpperCenter;
  13.     }
  14. }
复制代码
操作步骤:

  • 创建一个空物体,并将VerticalLayoutGroup组件添加到该物体上。
  • 将需要进行垂直布局的子对象添加到VerticalLayoutGroup物体下。
  • 将VerticalLayoutExample脚本添加到VerticalLayoutGroup物体上。
  • 在Inspector面板中,将VerticalLayoutGroup的spacing设置为10,childForceExpandWidth和childForceExpandHeight设置为false,childControlHeight设置为true,childAlignment设置为UpperCenter。
例子2:动态添加子对象
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class AddChildExample : MonoBehaviour
  4. {
  5.     public VerticalLayoutGroup verticalLayoutGroup;
  6.     public GameObject childPrefab;
  7.     private void Start()
  8.     {
  9.         for (int i = 0; i < 5; i++)
  10.         {
  11.             GameObject child = Instantiate(childPrefab, verticalLayoutGroup.transform);
  12.             child.GetComponentInChildren<Text>().text = "Child " + (i + 1);
  13.         }
  14.     }
  15. }
复制代码
操作步骤:

  • 创建一个空物体,并将VerticalLayoutGroup组件添加到该物体上。
  • 创建一个子对象的预制体,并将其赋值给AddChildExample脚本的childPrefab变量。
  • 将AddChildExample脚本添加到空物体上。
  • 在Inspector面板中,将VerticalLayoutGroup的spacing和其他布局设置进行适当调整。
例子3:动态删除子对象
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class RemoveChildExample : MonoBehaviour
  4. {
  5.     public VerticalLayoutGroup verticalLayoutGroup;
  6.     private void Start()
  7.     {
  8.         for (int i = 0; i < verticalLayoutGroup.transform.childCount; i++)
  9.         {
  10.             Destroy(verticalLayoutGroup.transform.GetChild(i).gameObject);
  11.         }
  12.     }
  13. }
复制代码
操作步骤:

  • 创建一个空物体,并将VerticalLayoutGroup组件添加到该物体上。
  • 将RemoveChildExample脚本添加到空物体上。
  • 在Inspector面板中,将VerticalLayoutGroup的spacing和其他布局设置进行适当调整。
  • 运行游戏,所有子对象将被删除。
例子4:动态调整子对象的大小
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class ResizeChildExample : MonoBehaviour
  4. {
  5.     public VerticalLayoutGroup verticalLayoutGroup;
  6.     private void Start()
  7.     {
  8.         for (int i = 0; i < verticalLayoutGroup.transform.childCount; i++)
  9.         {
  10.             RectTransform childRectTransform = verticalLayoutGroup.transform.GetChild(i).GetComponent<RectTransform>();
  11.             childRectTransform.sizeDelta = new Vector2(childRectTransform.sizeDelta.x, 100f);
  12.         }
  13.     }
  14. }
复制代码
操作步骤:

  • 创建一个空物体,并将VerticalLayoutGroup组件添加到该物体上。
  • 将ResizeChildExample脚本添加到空物体上。
  • 在Inspector面板中,将VerticalLayoutGroup的spacing和其他布局设置进行适当调整。
  • 运行游戏,所有子对象的高度将被调整为100。
例子5:动态调整子对象的对齐方式
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class AlignChildExample : MonoBehaviour
  4. {
  5.     public VerticalLayoutGroup verticalLayoutGroup;
  6.     private void Start()
  7.     {
  8.         verticalLayoutGroup.childAlignment = TextAnchor.MiddleCenter;
  9.     }
  10. }
复制代码
操作步骤:

  • 创建一个空物体,并将VerticalLayoutGroup组件添加到该物体上。
  • 将AlignChildExample脚本添加到空物体上。
  • 在Inspector面板中,将VerticalLayoutGroup的spacing和其他布局设置进行适当调整。
  • 运行游戏,所有子对象的对齐方式将被调整为MiddleCenter。
注意事项


  • VerticalLayoutGroup组件只能用于垂直布局,如果需要水平布局,请使用HorizontalLayoutGroup组件。
  • 在使用VerticalLayoutGroup组件时,确保子对象的RectTransform组件的锚点和位置设置正确,以便正确计算子对象的位置和大小。
参考资料

            本文作者:                            Blank                            
        本文链接:        
        版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
        声援博主:如果您觉得文章对您有帮助,可以点击文章右下角            【                推荐】                    一下。您的鼓励是博主的最大动力!

来源:https://www.cnblogs.com/alianblank/archive/2023/07/21/17570842.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具