版本: Unity 6 (6000.0)
语言英语
  • C#

Transform.childCount

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每个建议更改,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册
public int childCount;

描述

父级 Transform 拥有的子级数量。

注意:父级不包含在计数中。
注意:不活动的 GameObject 会包含在计数中。

using UnityEngine;

public class ExampleClass : MonoBehaviour { // generate a group of connected GameObjects void Awake() { GameObject go = new GameObject("top");

Random.InitState(System.Environment.TickCount);

// add 3, 4 or 5 "middle" children that report to "top" for (int i = 0; i < Random.Range(3, 6); i++) { GameObject go2 = new GameObject("middle" + i.ToString()); go2.transform.parent = go.transform;

// add between 1 to 8 "bottom" children that report to the above "middle" for (int j = 0; j < Random.Range(1, 8); j++) { GameObject go3 = new GameObject("bottom" + j); go3.transform.parent = go2.transform; } } }

void Start() { // how many children does top have? GameObject go = GameObject.Find("top"); Debug.Log(go.name + " has " + go.transform.childCount + " children");

// pick a random middle group and pick a member of its children go = GameObject.Find("middle" + Random.Range(0, go.transform.childCount)); Debug.Log(go.name + " has " + go.transform.childCount + " children"); } }