Unity学习笔记:递归层级未知查找子物体

  单独定义一个工具类。功能就是在层级未知的情况下查找子物体,方便在其他脚本中调用 减少重复代码的书写。

   创建一个类命名为TransformHelper删除掉继承关系。使用静态static创建方法。

 public class TransFormHelper
{
             //(父物体变换组件,子物体名称)
	public static Transform GetChild(Transform TF,string childName)
	{
		Transform childTF = TF.Find(childName);//查找名字为childName的子物体
		if (childTF!=null)
		{
			return childTF;
		}
		if (TF.childCount == 0)
		{
			return null; 
		}
		for (int i = 0; i 

  调用

Transform tf =TransFormHelper.GetChild(this.transform,"Cube (3)");
			if (tf)
			{
				Debug.Log("找到了"); 
				Debug.Log(tt.name);
			}
			else
			{
				Debug.Log("未找到");
			}

 

你可能感兴趣的:(Unity学习笔记:递归层级未知查找子物体)