using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public HingeJoint[] hingeJoints;
public void Awake() {
hingeJoints = GetComponentsInChildren<HingeJoint>();
foreach (HingeJoint joint in hingeJoints) {
joint.useSpring = false;
}
}
}
Component.GetComponentsInChildren 获取子物体组件列表
function GetComponentsInChildren (t : Type, includeInactive : bool = false) : Component[]
Description描述
Returns all components of Type type in the GameObject or any of its children.
在GameObject或任何它的子物体,返回全部Type类型组件
For C# there is a generic version available.
对于C#有一个通用的版本。
Only active components are returned.
仅返回激活的组件
上面是官方的文档说明。
用在实际开发过程中,
GetComponentsInChildren可以得到的是子辈或者孙辈的列表。<pre name="code" class="csharp">比如:
<img src="https://img-blog.csdn.net/20150310120129598?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGhrdW4wMTIw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
在Inventory上面挂载的脚本执行
<span style="font-family:Menlo;"><span style="color:#009695;">private</span><span style="color:#444444;"> </span><span style="color:#444444;">InventoryItemUI</span><span style="color:#444444;">[] </span><span style="color:#444444;">listInventoryItems</span><span style="color:#444444;">;</span>
<span style="color:#444444;"> </span><span style="color:#009695;">private</span><span style="color:#444444;"> </span><span style="color:#444444;">UIScrollView</span><span style="color:#444444;"> </span><span style="color:#444444;">view</span><span style="color:#444444;">;</span>
<span style="color:#444444;"> </span>
<span style="color:#444444;"> </span><span style="color:#999988;"><em>//</em></span><span style="color:#999988;"><em> </em></span><span style="color:#999988;"><em>Use</em></span><span style="color:#999988;"><em> </em></span><span style="color:#999988;"><em>this</em></span><span style="color:#999988;"><em> </em></span><span style="color:#999988;"><em>for</em></span><span style="color:#999988;"><em> </em></span><span style="color:#999988;"><em>initialization</em></span>
<span style="color:#444444;"> </span><span style="color:#3363a4;">void</span><span style="color:#444444;"> </span><span style="color:#444444;">Awake</span><span style="color:#444444;">(){</span>
<span style="color:#999988;"><em>//</em></span><span style="color:#999988;"><em> </em></span><span style="color:#999988;"><em>view</em></span><span style="color:#999988;"><em> = </em></span><span style="color:#999988;"><em>transform</em></span><span style="color:#999988;"><em>.</em></span><span style="color:#999988;"><em>Find</em></span><span style="color:#999988;"><em> ("</em></span><span style="color:#999988;"><em>ScrollView</em></span><span style="color:#999988;"><em>").</em></span><span style="color:#999988;"><em>GetComponent</em></span><span style="color:#999988;"><em><</em></span><span style="color:#999988;"><em>UIScrollView</em></span><span style="color:#999988;"><em>> ();</em></span>
<span style="color:#444444;"> </span><span style="color:#444444;">listInventoryItems</span><span style="color:#444444;"> = </span><span style="color:#444444;">GetComponentsInChildren</span><span style="color:#444444;"><</span><span style="color:#444444;">InventoryItemUI</span><span style="color:#444444;">> ();</span>
<span style="color:#444444;"> }</span></span>
<span style="font-family:Menlo;color:#444444;">可以直接得到package-item上面挂的所有InventoryItemUI脚本,而不是scrollview上面的脚本。</span>