type | 要检索的组件的类型。 |
includeInactive | 在发现结果集内是否应包含非活动 GameObject 上的组件? |
返回 GameObject 或其任何子项中类型为 type
的所有组件。
Unity searches for components recursively on child GameObjects. This means that it also includes all the child GameObjects of the target GameObject, and all subsequent child GameObjects.
Note: If the type you request is a derivative of MonoBehaviour and the associated script can not be loaded then this function will return `null` for that component.
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; } } }
includeInactive | 在发现结果集内是否应包含非活动 GameObject? |
T[] 匹配指定类型的所有已发现组件的列表。
通用版本。有关更多详细信息,请参阅通用函数页面。
Note: If the type you request is a derivative of MonoBehaviour and the associated script can not be loaded then this function will return `null` for that component.
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; } } }
results | 用于接收找到的组件的列表。 |
includeInactive | 在发现结果集内是否应包含非活动 GameObject? |
将找到的所有组件均返回到 List results
中。
Note: If the type you request is a derivative of MonoBehaviour and the associated script can not be loaded then this function will return `null` for that component.
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; } } }