測試-關於Unity獲取子層級內容的幾種接口(Transform FindChild, Component GetComponentInChildren,...)


測試常用的層級內組件查找接口,但一些需求還是需要擴展

比如按照名稱批量查找節點等

 

1.Transform - Transform Find(string name)

可以直接根據名稱搜索到子層級節點和孫節點等,支持非激活的節點,但不能返回數組

不支持搜索自身

var bTransform = transform.Find("a/b");
Debug.Log(bTransform);

 更正只支持對路徑搜索,不支持子節點孫節點的字符匹配(測試unity5.6.2)

 

 

2.Transform - Transform FindChild(string name)

可以搜索到子節點,但不支持孫節點,曾孫節點等。支持非激活的節點

不支持搜索自身,出場率很低,基本用不到

var aTaransform = transform.FindChild("a");
Debug.Log(aTransform);

 

 

 

3.Component - T GetComponentInChildren<T>(bool includeInactive)

支持子節點,孫節點的搜索。

如果自身存在這個組件,可返回自身

 

如果是非激活的對象,第二個參數includeInactive設為true即可搜索到

但缺點是只能搜索組件,不能按照名稱搜索

用GetComponentsInChildren可以搜索多個,返回數組

 

GetComponentInParent規則同此條


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM