unity深度查找某个子物体和遍历所有子物体方法

来源:互联网 发布:java jxl.jar 下载 编辑:程序博客网 时间:2024/05/18 09:19

本文总结一下关于unity的查找子物体的方法


首先说明一下这里将讲三种查找子物体方法

查找固定路径的某一个子物体的方法、通过名字深度查找某个子物体的方法查找父物体下所有子物体的方法。


第一:查找固定路径的某一个子物体的方法

对于已知的路径可以直接用go.transform.FindChild方法来查找。

例如:在这样一个层级路径下,我们要找到最后那个plane物体。



而下面是查找的具体方法:

using UnityEngine;using System.Collections;public class findchild : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {        if (Input.GetMouseButtonDown(1))        {            //查找物体方法            GameObject go = GameObject.Find("Cube");            //查找子物体,并且将得到的物体转换成gameobject          GameObject objname= go.transform.FindChild("Sphere/Cylinder/Plane").gameObject;          Debug.Log("得到最终子物体的名字是:"+ objname.name);        }}}

然后是执行结果:




==-------------------------------------------------------------------------------------------------------------


第二:通过名字深度查找某个子物体的方法

注意:要使用这个方法必须要满足两个条件:第一必须有你要查找的子物体的名字,第二必须要从一个父物体上开始查起


下面代码中,check代表从这个父物体开始查起,name为你要查找的目标子物体的名称。如return GetTransform(transform,"bone12");


该方法核心代码:


 Transform GetTransform(Transform check, string name)    {        Transform forreturn = null;        foreach (Transform t in check.GetComponentsInChildren<Transform>())        {            if (t.name == name)            {                Debug.Log("得到最终子物体的名字是:" + t.name);                forreturn = t;                return t;                            }                           }        return forreturn;    }

再看完整的测试代码:还用上个的例子的,例如这次要查到Cylinder这个物体:

修改后的代码:

using UnityEngine;using System.Collections;public class findchild : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {        if (Input.GetMouseButtonDown(1))        {            //  //查找物体方法           GameObject go = GameObject.Find("Cube");            //  //查找子物体,并且将得到的物体转换成gameobject            //GameObject objname= go.transform.FindChild("Sphere/Cylinder/Plane").gameObject;            //Debug.Log("得到最终子物体的名字是:"+ objname.name);                         GetTransform(go.transform, "Cylinder");                    }}    Transform GetTransform(Transform check, string name)    {        Transform forreturn = null;        foreach (Transform t in check.GetComponentsInChildren<Transform>())        {            if (t.name == name)            {                Debug.Log("得到最终子物体的名字是:" + t.name);                forreturn = t;                return t;                            }                           }        return forreturn;    }}

测试结果:


-----------------------------------------------------------------------------------------------------

第三:接下来我们将获取一个父物体下的所有子物体,然后销毁其下所有子物体

注意:所有子物体都是同级关系,在同一层里。如图:



核心方法:

 List<Transform> lst = new List<Transform>();            foreach (Transform child in transform)            {                lst.Add(child);                Debug.Log(child.gameObject.name);            }            for (int i = 0; i < lst.Count; i++)            {                Destroy(lst[i].gameObject);            }


上面的transform就是该父物体的transform。具体案例代码:

using UnityEngine;using System.Collections;using System.Collections.Generic;public class findchild : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {        if (Input.GetMouseButtonDown(1))        {            //  //查找物体方法           GameObject go = GameObject.Find("Cube");            List<Transform> lst = new List<Transform>();            foreach (Transform child in go.transform)            {                lst.Add(child);                Debug.Log(child.gameObject.name);            }            for (int i = 0; i < lst.Count; i++)            {                Debug.Log("销毁的物体是:"+ lst[i].gameObject);                Destroy(lst[i].gameObject);            }        }}   }

测试结果,全被销毁了:



以上就是我总结的常用的三种查找子物体的方法。

夏天来了,热死宝宝了!