unity中查找hierarchy面板对象,包含隐藏对象。

编程入门 行业动态 更新时间:2024-10-21 11:53:01

unity中查找hierarchy面板<a href=https://www.elefans.com/category/jswz/34/1771306.html style=对象,包含隐藏对象。"/>

unity中查找hierarchy面板对象,包含隐藏对象。

在初始化获取hierarchy面板对象的时候使用。
当使用GameObject.Find无法查找被隐藏的物体,在网上查找了一些方法发现都无法在运行模式下运行,根据查找到方法修改编写了俩方法放到脚本中可以直接使用,在运行状态下也可获取隐藏的物体。

  public static Transform TraverseAllObjects(string name){Transform s = null;try{// 获取场景中所有的根物体GameObject[] rootObjects = SceneManager.GetActiveScene().GetRootGameObjects();// 遍历所有根物体和它们的子物体foreach (GameObject rootObject in rootObjects){TraverseHierarchy(rootObject.transform, name ,ref s);}return s;}catch (System.Exception e) { return s; }}private static void TraverseHierarchy(Transform parent,string name ,ref Transform returnObj){try{if (parent.name == name){returnObj = parent;}}catch (System.Exception) { }// 遍历当前父物体下的所有子物体foreach (Transform child in parent){// 处理子物体,这里可以添加你的逻辑try{//  print(child.name+"   "+ name);if (child.name==name){returnObj=child;}          }catch (System.Exception){TraverseHierarchy(child, name,ref returnObj);}// 递归调用遍历子物体的子物体TraverseHierarchy(child, name, ref returnObj);}}

更多推荐

unity中查找hierarchy面板对象,包含隐藏对象。

本文发布于:2023-11-14 14:30:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   面板   unity   hierarchy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!