关于查找父物体下子物体可以改成如下
//从父物体下找到对应名称子物体
public static Transform FindChild(GameObject father, string name)
{
Transform[] childList = father.GetComponentsInChildren<Transform>();
Transform child = null;
bool isFinded = false;
foreach (var temp in childList)
{
if (temp.name == name)
{
if (isFinded == true)
{
//查找是否有重名的 //缺点是要把整个列表都遍历一遍
Debug.Log(father.name + " 下存在重名的物体 " + name);
}
else
{
child = temp;
isFinded = true;
}
}
}
return child;
}
public static T FindChild<T>(string fatherName, string name)
{
GameObject father = GameObject.Find(fatherName);
return FindChild(father, name).GetComponent<T>();
}