GameObject.GetComponentsInParent

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

Parameters

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

Description

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

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


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

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


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 中。


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; } } }