ToB企服应用市场:ToB评测及商务社交产业平台

标题: Unity UGUI的VerticalLayoutGroup(垂直布局)组件的介绍及使用 [打印本页]

作者: 大连密封材料    时间: 2023-7-21 11:53
标题: Unity UGUI的VerticalLayoutGroup(垂直布局)组件的介绍及使用
Unity UGUI的VerticalLayoutGroup(垂直布局)组件的介绍及使用

1. 什么是VerticalLayoutGroup组件?

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

VerticalLayoutGroup组件通过以下步骤实现垂直布局:
3. VerticalLayoutGroup组件的常用属性

4. VerticalLayoutGroup组件的常用函数

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. }
复制代码
操作步骤:
例子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. }
复制代码
操作步骤:
例子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. }
复制代码
操作步骤:
例子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. }
复制代码
操作步骤:
例子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. }
复制代码
操作步骤:
注意事项

参考资料

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4