unity中遍历Transform的子物体

1、遍历Transform直接子transform

 private void Start()
 {
      var Equipment = building.transform.FindChild("building/building/Equipment");
        foreach (Transform tran in Equipment)
        {
            Debug.LogError(tran);
        }
  }        

2、递归遍历该GameObject的所有子GameObject

 public void Awake()
    {    
       Recursive(gameObject);
    }

//递归遍历该GameObject的所有子GameObject
 private static void Recursive(GameObject parenGameObject)
    {
        foreach (Transform child in parenGameObject.transform)
        {

            Recursive(child.gameObject);
            Debug.LogError(child);
        }
    }    

 

posted @ 2018-04-29 16:32  U3DEngineer  阅读(3680)  评论(0编辑  收藏  举报