2014-10-29 59 views
1

因为我知道得到最顶端的父母是transform.root。如果我想要得到最底层的孩子,并且我不知道孩子名字的情况,我该怎么办?我已经尝试得到最底部的孩子

for (int i=0; i<theParent.childCount; i++) 
{ 
int bottommost=theParent.childCount-1; 
Debug.Log(theParent.GetChild(bottommost).name); 
} 

但是,不是我期望的结果,我只是得到第一个孩子,但我想要最底层的一个。我指的是层次结构中最底层的孩子。是否有任何提示可以让最底层的孩子?谢谢。

回答

0

首先,您需要定义什么是底部的孩子,因为孩子的深度也可以取决于兄弟姐妹。

例如:

    • child_1
    • child_2
    • child_3

但随后每个孩子可以有自己的孩子

  • Child_1
    • Child_1A
    • Child_1B
  • Child_2
    • Child_2A
    • Child_2B
    • Child_2C
    • Child_2D
    • Child_2E

所以在儿童的孩子的这种情况下,人是处于同一水平。

也许您在编辑器中看到的最底部? 在这种情况下,可能会添加一些内容以便搜索它...

0

创建父级变换下所有子级的列表。

List<GameObject> list = new List<GameObject>(); 

foreach(Transform t in transform) 
{ 
    list.Add(t.gameObject); 

    if(t.childCount > 0) 
    foreach(Transform c in t) 
     list.Add(c); 
} 

的最后一个元素是最后一个子项:

GameObject lastChild = list[list.Count-1]; 
2
lastChild = transform.GetChild(transform.childCount - 1);