赞
踩
VerticalLayoutGroup是Unity UGUI中的一种布局组件,用于在垂直方向上自动排列子对象。它可以根据子对象的大小和布局设置,自动调整子对象的位置和大小,实现垂直布局效果。
VerticalLayoutGroup组件通过以下步骤实现垂直布局:
using UnityEngine; using UnityEngine.UI; public class VerticalLayoutExample : MonoBehaviour { public VerticalLayoutGroup verticalLayoutGroup; private void Start() { verticalLayoutGroup.spacing = 10f; verticalLayoutGroup.childForceExpandWidth = false; verticalLayoutGroup.childForceExpandHeight = false; verticalLayoutGroup.childControlHeight = true; verticalLayoutGroup.childAlignment = TextAnchor.UpperCenter; } }
操作步骤:
using UnityEngine; using UnityEngine.UI; public class AddChildExample : MonoBehaviour { public VerticalLayoutGroup verticalLayoutGroup; public GameObject childPrefab; private void Start() { for (int i = 0; i < 5; i++) { GameObject child = Instantiate(childPrefab, verticalLayoutGroup.transform); child.GetComponentInChildren<Text>().text = "Child " + (i + 1); } } }
操作步骤:
using UnityEngine;
using UnityEngine.UI;
public class RemoveChildExample : MonoBehaviour
{
public VerticalLayoutGroup verticalLayoutGroup;
private void Start()
{
for (int i = 0; i < verticalLayoutGroup.transform.childCount; i++)
{
Destroy(verticalLayoutGroup.transform.GetChild(i).gameObject);
}
}
}
操作步骤:
using UnityEngine; using UnityEngine.UI; public class ResizeChildExample : MonoBehaviour { public VerticalLayoutGroup verticalLayoutGroup; private void Start() { for (int i = 0; i < verticalLayoutGroup.transform.childCount; i++) { RectTransform childRectTransform = verticalLayoutGroup.transform.GetChild(i).GetComponent<RectTransform>(); childRectTransform.sizeDelta = new Vector2(childRectTransform.sizeDelta.x, 100f); } } }
操作步骤:
using UnityEngine;
using UnityEngine.UI;
public class AlignChildExample : MonoBehaviour
{
public VerticalLayoutGroup verticalLayoutGroup;
private void Start()
{
verticalLayoutGroup.childAlignment = TextAnchor.MiddleCenter;
}
}
操作步骤:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。