赞
踩
HorizontalLayoutGroup是Unity UGUI中的一种布局组件,用于在水平方向上对子物体进行排列和布局。它可以根据一定的规则自动调整子物体的位置和大小,使它们在水平方向上均匀分布。
HorizontalLayoutGroup组件通过以下步骤实现水平布局:
Spacing
:子物体之间的间距。ChildForceExpandWidth
:是否强制子物体扩展宽度以填充整个水平布局。ChildForceExpandHeight
:是否强制子物体扩展高度以填充整个水平布局。ChildControlWidth
:是否控制子物体的宽度。ChildControlHeight
:是否控制子物体的高度。CalculateLayoutInputHorizontal()
:计算水平布局的输入。CalculateLayoutInputVertical()
:计算垂直布局的输入。SetLayoutHorizontal()
:设置水平布局。SetLayoutVertical()
:设置垂直布局。using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public HorizontalLayoutGroup layoutGroup; public GameObject childPrefab; void Start() { for (int i = 0; i < 3; i++) { GameObject child = Instantiate(childPrefab, layoutGroup.transform); child.GetComponent<Text>().text = "Child " + (i + 1); } } }
操作步骤:
using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public HorizontalLayoutGroup layoutGroup; public GameObject childPrefab; void Start() { layoutGroup.spacing = 20f; for (int i = 0; i < 3; i++) { GameObject child = Instantiate(childPrefab, layoutGroup.transform); child.GetComponent<Text>().text = "Child " + (i + 1); } } }
操作步骤:
using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public HorizontalLayoutGroup layoutGroup; public GameObject childPrefab; void Start() { layoutGroup.childForceExpandWidth = true; for (int i = 0; i < 3; i++) { GameObject child = Instantiate(childPrefab, layoutGroup.transform); child.GetComponent<Text>().text = "Child " + (i + 1); } } }
操作步骤:
using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public HorizontalLayoutGroup layoutGroup; public GameObject childPrefab; void Start() { layoutGroup.childControlWidth = false; layoutGroup.childControlHeight = false; for (int i = 0; i < 3; i++) { GameObject child = Instantiate(childPrefab, layoutGroup.transform); child.GetComponent<Text>().text = "Child " + (i + 1); } } }
操作步骤:
using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public HorizontalLayoutGroup layoutGroup; public GameObject childPrefab; void Start() { layoutGroup.childAlignment = TextAnchor.MiddleCenter; for (int i = 0; i < 3; i++) { GameObject child = Instantiate(childPrefab, layoutGroup.transform); child.GetComponent<Text>().text = "Child " + (i + 1); } } }
操作步骤:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。