5212人加入学习
(17人评价)
游戏开发中的设计模式(Unity 5.6)
价格 ¥ 280.00
该课程属于 Unity - A计划(永久有效期) 请加入后再学习

关于查找父物体下子物体可以改成如下

//从父物体下找到对应名称子物体
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>();
}

 

[展开全文]