GameObject.GetComponentsInChildren

Switch to Manual
public Component[] GetComponentsInChildren (Type type, bool includeInactive= false);

Parameters

type 要检索的组件的类型。
includeInactive 在发现结果集内是否应包含非活动 GameObject 上的组件?

Description

返回 GameObject 或其任何子项中类型为 type 的所有组件。

组件搜索是在子对象上以递归方式执行的,因此包含子项的子项,依此类推。


using UnityEngine;

public class GetComponentsInChildrenExample : MonoBehaviour { void Start() { Component[] hingeJoints;

hingeJoints = GetComponentsInChildren(typeof(HingeJoint));

if (hingeJoints != null) { foreach (HingeJoint joint in hingeJoints) joint.useSpring = false; } else { // Try again, looking for inactive GameObjects Component[] hingesInactive = GetComponentsInChildren(typeof(HingeJoint), true);

foreach (HingeJoint joint in hingesInactive) joint.useSpring = false; } } }

public T[] GetComponentsInChildren ();
public T[] GetComponentsInChildren (bool includeInactive);

Parameters

includeInactive 在发现结果集内是否应包含非活动 GameObject?

Returns

T[] 匹配指定类型的所有已发现组件的列表。

Description

通用版本。有关更多详细信息,请参阅通用函数页面。


using UnityEngine;

public class GetComponentsInChildrenExample : MonoBehaviour { void Start() { HingeJoint[] hingeJoints;

hingeJoints = GetComponentsInChildren<HingeJoint>();

if (hingeJoints != null) { foreach (HingeJoint joint in hingeJoints) joint.useSpring = false; } else { // Try again, looking for inactive GameObjects HingeJoint[] hingesInactive = GetComponentsInChildren<HingeJoint>(true);

foreach (HingeJoint joint in hingesInactive) joint.useSpring = false; } } }

public void GetComponentsInChildren (List<T> results);
public void GetComponentsInChildren (bool includeInactive, List<T> results);

Parameters

results 用于接收找到的组件的列表。
includeInactive 在发现结果集内是否应包含非活动 GameObject?

Description

将找到的所有组件均返回到 List results 中。


using UnityEngine;
using System.Collections.Generic;

public class GetComponentsInChildrenExample : MonoBehaviour { void Start() { List<HingeJoint> hingeJoints = new List<HingeJoint>();

GetComponentsInChildren<HingeJoint>(false, hingeJoints);

if (hingeJoints != null) { foreach (HingeJoint joint in hingeJoints) joint.useSpring = false; } else { // Try again, looking for inactive GameObjects List<HingeJoint> hingesInactive = new List<HingeJoint>();

GetComponentsInChildren<HingeJoint>(true, hingesInactive);

foreach (HingeJoint joint in hingesInactive) joint.useSpring = false; } } }