今天正好碰见要写一个查找所有UISprite,包括active=fales的情况
同理也可以找出所有的GameObject啦~~~
贴上代码
UISprite[] chidItem = parentObj.transform.GetComponentsInChildren<UISprite>(true);
Debug.Log("查出所有子项 : " + chidItem.Length + " 个" );
UISprite[] chidItem2 = parentObj.transform.GetComponentsInChildren<UISprite>();
Debug.Log("查出所有子项 : " + chidItem2.Length + " 个" + );
我输出的 Log:
查出所有子项 : 90 个
UnityEngine.Debug:Log(Object)
查出所有子项 : 24 个
UnityEngine.Debug:Log(Object)
这两个有什么区别呢....
我们来看看API:
public Component[] GetComponentsInChildren(Type t,bool includeInactive = false);
Should Components on inactive GameObjects be included in the found set? includeInactive decides which children of the GameObject will be searched. The GameObject that you call GetComponentsInChildren on is always searched regardless.
来自有道的翻译是这样的 :
非活动的游戏对象的组件是否应该包含在已找到的集合中?includeInactive决定对游戏对象的哪个子对象进行搜索。
无论如何,您调用GetComponentsInChildren的GameObject总是被搜索。