GameObject.GetComponentsInParent

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

Parameters

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

Description

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

The search for components is carried out recursively on parent objects, so it includes parents of parents, and so on.
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 GetComponentsInParentExample : MonoBehaviour { void Start() { Component[] hingeJoints;

hingeJoints = GetComponentsInParent(typeof(HingeJoint));

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

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

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

Parameters

includeInactive在发现的集内是否应包含非活动组件?

Description

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

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 GetComponentsInParentExample : MonoBehaviour { void Start() { HingeJoint[] hingeJoints;

hingeJoints = GetComponentsInParent<HingeJoint>();

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

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

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

Parameters

includeInactive在发现的集内是否应包含非活动组件?
results容纳找到的组件的列表。

Description

查找 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 GetComponentsInParentExample : MonoBehaviour { void Start() { List<HingeJoint> hingeJoints = new List<HingeJoint>();

GetComponentsInParent<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>();

GetComponentsInParent<HingeJoint>(true, hingesInactive);

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