unity GetComponentsInChildren

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值