您当前的位置: 首页 >  苍狼王unity学院 ui

在层级未知情况下通过递归查找子物体 ,这个主要是用于UI的的层级查找中

苍狼王unity学院 发布时间:2019-07-30 17:41:31 ,浏览量:8

在在层级未知情况下通过递归查找子物体 ,这个主要是用于UI的的层级查找中

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

public class EnemyManager : MonoBehaviour
{
private GameObject t;
private string name_1 = “Cube_05”;
private void Start()
{
t = FindChildByName(this.gameObject,name_1);
print(t.name);
}
///
/// 在不知道层级的情况下,查找指定名字的子物体
///
///
///
public GameObject FindChildByName(GameObject parent, string childName)
{
if (parent.name == childName) //如果要查找的就是这个物体本身
{
return parent;
}
if (parent.transform.childCount < 1) //如果要查找的物体孩子数量为0,则跳出方法,进行下一个判定
{
return null;
}
GameObject obj = null;
for (int i = 0; i < parent.transform.childCount; i++)
{
GameObject go = parent.transform.GetChild(i).gameObject;
obj = FindChildByName(go, childName); //进行递归的调用,递归查找
if (obj != null)
{
break;
}
}
return obj;
}
}

关注
打赏
查看更多评论

苍狼王unity学院

暂无认证

  • 8浏览

    0关注

    252博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录