/// <summary> /// 自身と子要素をすべて取得し、Listで返す /// </summary> /// <param name="obj">親のオブジェクト</param> public static List <GameObject> GetAll(this GameObject obj) { var allChildren = new List <GameObject>(); allChildren.Add(obj); GetAllChildren.GetChildren(obj, ref allChildren); return(allChildren); }
/// <summary> /// 子要素を取得してリストに追加 /// </summary> public static void GetChildren(GameObject obj, ref List <GameObject> allChildren) { Transform children = obj.GetComponentInChildren <Transform>(); // 子要素がいなければ終了 if (children.childCount == 0) { return; } foreach (Transform ob in children) { allChildren.Add(ob.gameObject); GetAllChildren.GetChildren(ob.gameObject, ref allChildren); } }