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

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

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

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

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

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

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

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3  
 4 public class findchild : MonoBehaviour {
 5  
 6     // Use this for initialization
 7     void Start () {
 8     
 9     }
10     
11     // Update is called once per frame
12     void Update () {
13         if (Input.GetMouseButtonDown(1))
14         {
15             //查找物体方法
16             GameObject go = GameObject.Find("Cube");
17             //查找子物体,并且将得到的物体转换成gameobject
18           GameObject objname= go.transform.FindChild("Sphere/Cylinder/Plane").gameObject;
19  
20           Debug.Log("得到最终子物体的名字是:"+ objname.name);
21         }
22     }
23 }
复制代码

然后是执行结果:Plane
在这里插入图片描述

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

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

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

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

该方法核心代码:

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3  
 4 public class findchild : MonoBehaviour {
 5  
 6     // Use this for initialization
 7     void Start () {
 8     
 9     }
10     
11     // Update is called once per frame
12     void Update () {
13         if (Input.GetMouseButtonDown(1))
14         {
15             //  //查找物体方法
16            GameObject go = GameObject.Find("Cube");
17             //  //查找子物体,并且将得到的物体转换成gameobject
18             //GameObject objname= go.transform.FindChild("Sphere/Cylinder/Plane").gameObject;
19  
20             //Debug.Log("得到最终子物体的名字是:"+ objname.name);
21              
22  
23             GetTransform(go.transform, "Cylinder");
24             
25         }
26     }
27  
28     Transform GetTransform(Transform check, string name)
29     {
30         Transform forreturn = null;
31  
32         foreach (Transform t in check.GetComponentsInChildren<Transform>())
33         {
34             if (t.name == name)
35             {
36                 Debug.Log("得到最终子物体的名字是:" + t.name);
37                 forreturn = t;
38                 return t;
39                 
40             }        
41            
42         }
43         return forreturn;
44     }
45 }
复制代码

测试结果:Cylinder
在这里插入图片描述


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

注意:所有子物体都是同级关系,在同一层里。如图:
在这里插入图片描述

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class findchild : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void 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);
            }

        }
    }


}

测试结果,全被销毁了:

在这里插入图片描述

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

转自https://www.cnblogs.com/macky/p/9335093.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值