Unity中獲取物體的子物體


如果想獲取一級子節點

1  foreach (Transform child in this.transform)
2         {
3             Debug.Log(child.name);
4         }
View Code

如果想獲取所有子節點

1  foreach (Transform child in gameObject.GetComponentsInChildren<Transform>())
2         {
3             Debug.Log(child.name);
4         }
View Code

這樣會包括所有帶有transform屬性的子節點。

在unityAPI中也有介紹

1 public class ExampleClass : MonoBehaviour {
2     void Example() {
3         foreach (Transform child in transform) {
4             child.position += Vector3.up * 10.0F;
5         }
6     }
7 }
View Code

 

歡迎廣大unity興趣愛好者加群學習165628892 (進群備注:博客)  隨時提出問題解決問題!

樹欲靜而風不止,子欲養而親不待。

2016年12月8日15:12:38


免責聲明!

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



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