unity 子オブジェクト 取得
- unity 子 取得
- unity 子
- unity 子オブジェクト 取得 すべて
- 子オブジェクト unity
Transform.Find
public Transform Find(string name);
パラメーター
- 名前 – 子供の名前
形容
- 名前で子を検索し、それを返します。
- 名前を持つ子が見つからない場合は、null が返されます。
- name に ‘/’ 文字が含まれている場合、パス名のように階層を走査します。
public void Example() { gun = player.transform.Find("Gun").gameObject; if(gun != null) { .... } }
//自身の子オブジェクトから名前検索で取得(一段階下の子オブジェクトのみ) GameObject child = transform.Find("Child").gameObject;
//孫(子オブジェクトの子オブジェクト)を取得する。 //以下の場合なら自身の子オブジェクトChildの子オブジェクトGrandChildを取得 GameObject grandChild = transform.Find("Child/GrandChild").gameObject;
unity 子要素 取得
Transform.GetChild
public Transform GetChild(int index);
パラメーター
- インデックス – 返す子の変換のインデックス。
形容
- インデックスによって変換子を返します。
public void Example() { meeple = this.gameObject.transform.GetChild(0); grandChild = this.gameObject.transform.GetChild(0).GetChild(0).gameObject; }
//子オブジェクトの順番で取得。最初が0で二番目が1となる。つまり↓は最初の子オブジェクト GameObject child = transform.GetChild(0).gameObject
unity 子オブジェクト
Component.GetComponentInChildren
public Component GetComponentInChildren(Type t);
パラメーター
- t – 取得するコンポーネントの型。
形容
- 深さ優先検索を使用して、GameObject またはその子の Type 型のコンポーネントを返します。
//子オブジェクトの全て(子の子の子のオブジェクトとかも)を取得する void Start { GetChildren(this.gameObject); } void GetChildren(GameObject obj) { Transform children = obj.GetComponentInChildren<Transform>(); //子要素がいなければ終了 if (children.childCount == 0) { return; } foreach(Transform ob in children) { GetChildren(ob.gameObject); } }