1. 程式人生 > >Unity學習筆記003.遞迴查詢子物體/獲取子物體元件

Unity學習筆記003.遞迴查詢子物體/獲取子物體元件

public static Transform FindChild(Transform parent,string name)
{
	Transform child = null;
    child = parent.Find(name);
    if (child != null)
    	return child;
    Transform grandchild = null;
    for(int i = 0; i < parent.childCount; i++)
    {
		grandchild = FindChild(parent.GetChild(i)
, name); if (grandchild != null) return grandchild; } return null; } public static T FindChild<T>(Transform parent, string name) where T : Component { Transform child = null; child = FindChild(parent, name); if (child != null) return child.GetComponent<T>(); return null; }