unityのオブジェクトをC#で生成、子要素に移動する方法
※Unity実践リファレンスの内容を参考にしています。
<エンジニアのためのUnity実践リファレンス ~ ゲーム開発にすぐに役立つスクリプト入門>
https://www.amazon.co.jp/dp/B00WHEJI8W/ref=cm_sw_em_r_mt_dp_K82SFb5NXTQ27
動的にC#でゲームオブジェクトを生成し、それを特定のゲームオブジェクトの子要素として移動させるためのコードになります
// test.cs
// Start is called before the first frame update
void Start()
{
//C#で使用する変数名にparentAという名前で作り、
//ヒエラルキー上のオブジェクト名をParentGO_Aとして生成
var parentA = new GameObject("ParentGO_A");
var parentB = new GameObject("ParentGO_B");
var childA = new GameObject("childrenGO_A");
var childB = new GameObject("childrenGO_B");
var childBchildA = new GameObject("childrenBchildrenGO_A");
//childAをparentAの子要素にトランスフォーム
//これでchildAのゲームオブジェクトを指定したゲームオブジェクトの子要素に変更
childA.transform.parent = parentA.transform;
childB.transform.parent = parentB.transform;
//特定の要素の子要素にする場合に、2階層下の要素としても1回で変更できる
//この場合は
childBchildA.transform.parent = childB.transform;
}
ちゃんと階層構造を持ったままゲームオブジェクトが生成されました。
この機能を使って特定の条件下で生成したゲームオブジェクトがあれば、それをFind関数を利用して見つけたのちに、子要素へ移動させるということもできそうです。
Author And Source
この問題について(unityのオブジェクトをC#で生成、子要素に移動する方法), 我々は、より多くの情報をここで見つけました https://qiita.com/tat_mae084/items/4500d20bd90990130ccf著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .