GameObject.GetComponentsInChildren 获取子物体组件列表

function GetComponentsInChildren (type : Type, includeInactive : bool = false) : Component[]

Description描述

Returns all components of Type type in the GameObject or any of its children.
Only active components are returned.

得到游戏物体或者其所有子物体上的所有类型为type的组件。
只返回活动的组件。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public HingeJoint[] hingeJoints;
	public void Awake() {
		hingeJoints = gameObject.GetComponentsInChildren<HingeJoint>();
		foreach (HingeJoint joint in hingeJoints) {
			joint.useSpring = false;
		}
	}
}
// 使这个游戏物体及其子物体上的所有HingeJoints上的spring不可用.
var hingeJoints : HingeJoint[];
hingeJoints = gameObject.GetComponentsInChildren(HingeJoint);
for (var joint : HingeJoint in hingeJoints) {
	joint.useSpring = false;
}
Page last updated: 2011-1-16