如何找到GameObject的子代或通过脚本附加到子GameObject的脚本 [英] How to find child of a GameObject or the script attached to child GameObject via script

查看:89
本文介绍了如何找到GameObject的子代或通过脚本附加到子GameObject的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个愚蠢的问题,但是我将如何通过脚本(脚本附加到gameObject)引用游戏对象的子对象(多维数据集). (相当于类似GetComponent的东西)

I know this is a bit of a stupid question, but how would I reference the child (a cube) of a game object via script(the script is attached to the gameObject). (the equivalent to something like GetComponent)

推荐答案

通过索引查找子GameObject :

您可以使用 GetChild 获取GameObject的第一个孩子.功能.

You can get the first child of a GameObject with the GetChild function.

GameObject originalGameObject = GameObject.Find("MainObj");
GameObject child = originalGameObject.transform.GetChild(0).gameObject;

您可以通过向 GetChild 函数.

You can get other children by providing index number of the child GameObject such as 1,2,3, to the GetChild function.

,如果它是原始游戏对象中的孩子的孩子?我会 必须重复一次,还是只是第n次失败

and if its a child of a child in the original gameobject? would I just have to repeat it or would it just be the nth one down

首先找到一个孩子,然后从参考中找到该孩子的孩子.

You find the child first, then find the child of that child from the reference.

让我们说这是OriginalGameObject/Prefab层次结构:

Let's say this is OriginalGameObject/Prefab hierarchy:

  • OriginalGameObject
    • child1
    • child2
    • child3
      • childOfChild3
      • OriginalGameObject
        • child1
        • child2
        • child3
          • childOfChild3

          如您所见,OriginalGameObjectchild1child2child3的父级. childOfChild3是child3的子代.

          As you can see the OriginalGameObject is the parent of child1, child2 and child3. childOfChild3 is the child of child3.

          假设您要访问childs,而您仅引用了作为父GameObject的OriginalGameObject:

          Let's say you want to access the childs and you only have reference to OriginalGameObject which is the parent GameObject:

          //Instantiate Prefab
          GameObject originalGameObject  = Instantiate(prefab);
          
          //To find `child1` which is the first index(0)
          GameObject child2 = originalGameObject.transform.GetChild(0).gameObject;
          
          //To find `child2` which is the second index(1)
          GameObject child2 = originalGameObject.transform.GetChild(1).gameObject;
          
          //To find `child3` which is the third index(2)
          GameObject child3 = originalGameObject.transform.GetChild(2).gameObject;
          

          索引从0开始,因此像数组一样,实际索引号为index-1.

          The index starts from 0 so the real index number is index-1 just like arrays.

          现在,要获取childOfChild3的引用,而childOfChild3child3的子代,但是您仅引用OriginalGameObject是父GameObject:

          Now, to get the reference of childOfChild3 which is the child of child3 but you only have reference to OriginalGameObject which is the parent GameObject:

          首先,获取child3的引用,然后从中获取childOfChild3.

          First of all, get reference of child3 then get childOfChild3 from it.

          GameObject mychild = originalGameObject.transform.GetChild(2).gameObject;
          GameObject childOfChild3 = mychild.transform.GetChild(0).gameObject;
          

          通过带有循环的索引查找[所有]子GameObject :

          要遍历originalGameObject的所有子对象:

          To loop through all the child of originalGameObject:

          GameObject originalGameObject = Instantiate(prefab);
          for (int i = 0; i < originalGameObject.transform.childCount; i++)
          {
              GameObject child = originalGameObject.transform.GetChild(i).gameObject;
              //Do something with child
          }
          

          您还可以使用transform.FindChild功能按名称查找子级.我不建议您这样做.多个子共享相同的名称时,它似乎很慢并且会发生冲突.这就是为什么您使用GetChild.

          You can also find child by name with the transform.FindChild function. I wouldn't recommend that you can do that. It seems slow and will conflict when multiple child share the-same name. That's why you use GetChild.

          通过名称查找子GameObject :

          GameObject child1 = originalGameObject.transform.FindChild("child1").gameObject;
          GameObject child2 = originalGameObject.transform.FindChild("child2").gameObject;
          GameObject child3 = originalGameObject.transform.FindChild("child3").gameObject;
          

          要找到childOfChild3,可以像使用文件目录一样轻松地使用'/'来实现.您提供parent/child然后提供名称. childOfChild3的父级是child3.因此,我们在FindChild函数中使用childOfChild3/child3.

          To find childOfChild3, you can easily do that with the '/' just like you do with a file directory. You provide the parent/child then name. The parent of childOfChild3 is child3. So, we use, childOfChild3/child3 in FindChild function.

          GameObject childOfChild3 = originalGameObject.transform.FindChild("child3/childOfChild3").gameObject;
          

          查找附加到子GameObject的脚本/组件:

          如果您只需要附加到子GameObject的脚本,则使用 GetComponentInChildren :

          If all you want is the script that is attached to the child GameObject, then use GetComponentInChildren:

          MyScript childScript = originalGameObject.GetComponentInChildren<MyScript>();
          

          如果有多个孩子使用相同的脚本,而您只想获取所有脚本,请使用

          If there are more than one child with the-same script and you just want to get all the scripts attached to them, then use GetComponentsInChildren:

          MyScript[] childScripts = originalGameObject.GetComponentsInChildren<MyScript>();
          for (int i = 0; i < childScripts.Length; i++)
          {
              MyScript myChildScript = childScripts[i];
              //Do something with myChildScript
          }
          

          这篇关于如何找到GameObject的子代或通过脚本附加到子GameObject的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆