Last active
June 18, 2021 02:43
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//自身の子オブジェクトから名前検索で取得(一段階下の子オブジェクトのみ) | |
GameObject child = transform.Find("Child").gameObject; | |
//子オブジェクトの順番で取得。最初が0で二番目が1となる。つまり↓は最初の子オブジェクト | |
GameObject child = transform.GetChild(0).gameObject | |
//孫(子オブジェクトの子オブジェクト)を取得する。 | |
//以下の場合なら自身の子オブジェクトChildの子オブジェクトGrandChildを取得 | |
GameObject grandChild = transform.Find("Child/GrandChild").gameObject; | |
//子オブジェクトの全て(子の子の子のオブジェクトとかも)を取得する | |
void Start { | |
GetChildren(this.gameObject); | |
} | |
void GetChildren(GameObject obj) { | |
Transform children = obj.GetComponentInChildren < Transform > (); | |
//子要素がいなければ終了 | |
if (children.childCount == 0) { | |
return; | |
} | |
foreach(Transform ob in children) { | |
//ここに何かしらの処理 | |
//例 ボーンについてる武器を取得する | |
//if (ob.name == "Right Hand") | |
// { | |
// rightHandWeapon = ob.transform.GetChild(0).gameObject; | |
// } | |
GetChildren(ob.gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment