GetComponentsInChildren<T>和GetComponentsInChildren<T>(bool)用法

GetComponentsInChildren<T>

一、能获取到自己的情况

Transform[] trans = GetComponentsInChildren<Transform>();
因为root对象以及其6个子对象都有Transform组件,所以获取到的size是7,包含了root自身节点。

二、不能获取到自身的情况

TestComponent[] trans = GetComponentsInChildren<TestComponent>();
此时是在root下面的子对象上挂在了TestComponent脚本(标识作用),这时没有获取到root自身节点

三、获取到孩子的孩子对象的情况

TestComponent[] trans = GetComponentsInChildren<TestComponent>();
此时获取到的结点包含了儿子节点、孙子节点和曾孙子节点,(看数组的顺序,感觉有点像深度优先搜索的遍历方式)。

GetComponentsInChildren<T>(bool)

GetComponentsInChildren<Transform>(true);//游戏对象下的子物体激活的没激活的都会被拿到,包括游戏对象本身
GetComponentsInChildren<Transform>(false);//游戏对象下的子物体激活的会被拿到,包括游戏对象本身;没激活的不会被拿到
另外,()不写的活默认为false.

posted @ 2021-07-07 17:59  奕心1999  阅读(270)  评论(0)    收藏  举报