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); } }} }
测试结果,全被销毁了:
以上就是我总结的常用的三种查找子物体的方法。
夏天来了,热死宝宝了!
- unity深度查找某个子物体和遍历所有子物体方法
- Unity中遍历所有子物体
- 遍历所有子物体的方法
- 【新手学习笔记】我总结的unity遍历所有子物体的两种简单方法
- unity3d 遍历检测某个子物体
- unity3d 遍历检测某个子物体
- Unity3D检测查找某个子物体
- unity获取物体下的所有子物体
- Unity 获取所有子物体,不包括孙物体
- Unity通过父物体找到所有子物体
- Unity根据名称递归查找一个物体的子物体
- 遍历物体所有子级(递归)
- 【Unity3D自学记录】遍历父物体下所有子物体
- 用递归查找所有子物体
- unity 删除子物体
- 查找子物体
- unity 实例化物体并成为某个物体的子物体
- 在Unity中获得一个物体的所有子物体包括孙物体。
- hive的查询注意事项以及优化总结 .
- 解决微信浏览器返回上一页面强制刷新的问题
- 获取select标签选中的值
- Android自定义通知样式适配不同系统样式
- 根据端口找配置文件、关服务
- unity深度查找某个子物体和遍历所有子物体方法
- 【LeetCode】56. Merge Intervals
- Java邮件开发简单实例
- 【Unity&事件】事件监听响应的几种方法分析研究
- 我推荐阅读的微信公众号-IT类
- 向前光照和延后光照渲染器问题和原理
- npm 安装配置等
- 特长生模拟 Array(水)
- #游戏unity#AR中奇妙的虚拟按钮