赞
踩
GridLayoutGroup是Unity UGUI中的一种布局组件,用于在UI界面中创建网格布局。它可以根据指定的行数、列数和间距自动排列子物体,使它们按照网格的形式排列。
GridLayoutGroup组件会根据指定的行数和列数,将子物体按照从左到右、从上到下的顺序排列。它还可以设置间距,控制子物体之间的间隔。当子物体的数量超过网格的容量时,GridLayoutGroup会自动创建新的行或列来容纳多余的子物体。
using UnityEngine; using UnityEngine.UI; public class GridLayoutExample : MonoBehaviour { public GridLayoutGroup gridLayout; public GameObject prefab; void Start() { gridLayout.constraint = GridLayoutGroup.Constraint.FixedColumnCount; gridLayout.constraintCount = 2; for (int i = 0; i < 4; i++) { Instantiate(prefab, transform); } } }
操作步骤:
注意事项:
using UnityEngine; using UnityEngine.UI; public class GridLayoutExample : MonoBehaviour { public GridLayoutGroup gridLayout; public GameObject prefab; void Start() { gridLayout.constraint = GridLayoutGroup.Constraint.FixedColumnCount; gridLayout.constraintCount = 3; gridLayout.spacing = new Vector2(10, 10); for (int i = 0; i < 9; i++) { Instantiate(prefab, transform); } } }
操作步骤:
注意事项:
using UnityEngine; using UnityEngine.UI; public class GridLayoutExample : MonoBehaviour { public GridLayoutGroup gridLayout; public GameObject prefab; void Start() { gridLayout.constraint = GridLayoutGroup.Constraint.Flexible; gridLayout.spacing = new Vector2(10, 10); for (int i = 0; i < 10; i++) { Instantiate(prefab, transform); } } }
操作步骤:
注意事项:
using UnityEngine; using UnityEngine.UI; public class GridLayoutExample : MonoBehaviour { public GridLayoutGroup gridLayout; public GameObject prefab; void Start() { gridLayout.constraint = GridLayoutGroup.Constraint.FixedRowCount; gridLayout.constraintCount = 2; for (int i = 0; i < 4; i++) { Instantiate(prefab, transform); } Invoke("ChangeConstraintCount", 3f); } void ChangeConstraintCount() { gridLayout.constraintCount = 3; } }
操作步骤:
注意事项:
using UnityEngine; using UnityEngine.UI; public class GridLayoutExample : MonoBehaviour { public GridLayoutGroup gridLayout; public GameObject prefab; void Start() { gridLayout.constraint = GridLayoutGroup.Constraint.FixedColumnCount; gridLayout.constraintCount = 2; gridLayout.spacing = new Vector2(10, 10); for (int i = 0; i < 4; i++) { Instantiate(prefab, transform); } Invoke("ChangeSpacing", 3f); } void ChangeSpacing() { gridLayout.spacing = new Vector2(20, 20); } }
操作步骤:
注意事项:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。