本文总结一下关于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 }

然后是执行结果:

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

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

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

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

该方法核心代码:

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

 1 Transform GetTransform(Transform check, string name)
 2     {
 3         Transform forreturn = null;
 4
 5         foreach (Transform t in check.GetComponentsInChildren<Transform>())
 6         {
 7             if (t.name == name)
 8             {
 9                 Debug.Log("得到最终子物体的名字是:" + t.name);
10                 forreturn = t;
11                 return t;
12
13             }
14
15         }
16         return forreturn;
17     }

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

修改后的代码:

 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 }

测试结果:

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

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

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

核心方法:

 1 List<Transform> lst = new List<Transform>();
 2             foreach (Transform child in transform)
 3             {
 4                 lst.Add(child);
 5                 Debug.Log(child.gameObject.name);
 6             }
 7             for (int i = 0; i < lst.Count; i++)
 8             {
 9                 Destroy(lst[i].gameObject);
10             }

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

 1 using UnityEngine;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4
 5 public class findchild : MonoBehaviour {
 6
 7     // Use this for initialization
 8     void Start () {
 9
10     }
11
12     // Update is called once per frame
13     void Update () {
14         if (Input.GetMouseButtonDown(1))
15         {
16             //  //查找物体方法
17            GameObject go = GameObject.Find("Cube");
18             List<Transform> lst = new List<Transform>();
19             foreach (Transform child in go.transform)
20             {
21                 lst.Add(child);
22                 Debug.Log(child.gameObject.name);
23             }
24             for (int i = 0; i < lst.Count; i++)
25             {
26                 Debug.Log("销毁的物体是:"+ lst[i].gameObject);
27                 Destroy(lst[i].gameObject);
28             }
29
30         }
31     }
32
33
34 }

测试结果,全被销毁了:

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

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

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

  1. 【Unity】获取当前物体下的所有子物体

    使用Transform.GetComponentsInChildren<>()的方法来获取指定物体下的所有子物体,并复制给数组,然后进行遍历 activePanel为指定物体,string ...

  2. unity——删除父物体下面的所有子物体

    1.直接使用for循环,遍历子物体,使用依次Destory直接销毁子物体: Destroy(parent.GetChild(i).gameObject); /// <summary>/// ...

  3. unity获取物体下的所有子物体

    1      通过标签来寻找 3 4 5 6 7 8 9 void ForeachObjs()     {         //GameObject objs[] = GameObject       ...

  4. unity3d 如何遍历一个物体下的所有子物体 (包括active为false的子物体)

    一. Gameobject.Find("游戏对象名")  //根据对象名直接获取游戏对象 这个方法可以找到指定的对象,但是一些缺陷. 缺陷1:如果场景中有重名,此方法找到的是Hie ...

  5. 在Unity中获得一个物体的所有子物体包括孙物体。

    看API时想到了这个问题,如何获得一个物体的所有子物体,在Unity的API有这个函数 Component.GetComponentsInChildren 获取子物体组件列表. 首先是在Unity软件 ...

  6. Unity3d 移除某个GameObject的所有子物体

    在开发游戏的时候,经验会遇到这样的需求:移除某个物体下面的所有子物体,比如排行榜列表的刷新,清空某个列表等.虽然Unity3d提供了一些现成的API可以操作,但是要正确移除一个物体下的所有子物体,还需 ...

  7. 【Unity 知识点】Unity 如何查找被隐藏的游戏对象

    1.第一种方式比较繁琐,就是通过foreach语句. foreach (Transform button in child.transform)              {              ...

  8. Unity中获取一个物体下所有的子物体的方法

    Unity中获取一个物体下所有的子物体的方法 方法1(获取全部子物体,无论子物体SetActive是否为true): using System.Collections; using System.Co ...

  9. Unity点击物体后,移动到物体所在位置

    Unity点击物体后,移动到物体所在位置 方法一:OnMouse检测(需要Collider组件) 脚本挂在被点击的物体上 using System.Collections.Generic; using ...

最新文章

  1. java 顺序栈_java用顺序栈实现数制转换 求完整程序!!
  2. Fuzzy C Means 算法及其 Python 实现——写得很清楚,见原文
  3. HTML 5 Web 音频
  4. SQL Server 数据库设计
  5. 移动的验证码安全问题告诉移动网站后......,1860意指一般人不会这样做.
  6. 递归查询mysql数据库设计
  7. boost::graph模块实现拉马努金图的周长和直径的测试程序
  8. 系统学习redis之二——redis集群搭建
  9. 摄像头拍照功能是怎样实现的?自己动手做一个!
  10. 成为前端开发人员的步骤
  11. android mediarecorder 输出到流_音视频的采集、编码、封包成 mp4 输出
  12. 奇安信专家:近八成软件存开源漏洞 供应链需全生命周期安全防护
  13. 软中断网卡处理Linux高性能外部设备处理机制SMP
  14. 【机器学习】--谱聚类从初始到应用
  15. 【游戏开发指路】Unity学习路线,三万字大纲(面试题大纲 | 知识图谱 | Unity游戏开发工程师)
  16. hublider配置php环境(wamp、phpstudy,php工具箱...)
  17. 网站漏洞修复之UEditor漏洞 任意文件上传漏洞
  18. 计算机公式SUBSTITUTE,全了,SUBSTITUTE函数常用套路集合!
  19. placement new理解
  20. Uncaught ReferenceError: xxx is not defined 解决办法

热门文章

  1. 支持向量机SVM(Support Vector Machines)介绍
  2. FPGA设计的常用思想与技巧(转)
  3. 熔池 沉积_用于3D打印的AI(第3部分):异常熔池分类的纠缠变分自动编码器
  4. 机器学习初学者_绝对初学者的机器学习
  5. Linux进程调度策略分析
  6. C语言高级编程:指针变量p指向的地址与p自身的地址
  7. Gartner Magic Quadrant for Enterprise Network Firewall (2018,2017,2016,2015,2014,2013,2011,2010)
  8. 空间谱专题06:宽带信号处理思路
  9. 奥尼尔的话剧《榆树下的欲望》
  10. 女人口口相传的快乐和语气