0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【UNITY C#】自身の子オブジェクトを全てコピーして、任意のオブジェクトの子オブジェクトとしてペーストするテンプレ

Last updated at Posted at 2021-08-13

public Transform SampleTran;//任意のオブジェクトの位置をパブリックで取得
//(インスペクターからドラッグアンドドロップで設定してね)

public void Copy()
{
    // 子の数を数える
    int ChildNum = transform.childCount;

    //forを使って子の数の分だけ繰り返す
    for (int i = 0; i < ChildNum ; i++)
    {
        //子オブジェクトをゲームオブジェクトとして取得
        GameObject Mychild = transform.GetChild(i).gameObject;

        //子オブジェクトのコピーをSampleの子オブジェクトとして生成(位置や大きさは上で設定した任意のオブジェクトに依存)
        GameObject MychildrenClone = Instantiate(Mychild);
        MychildrenClone.transform.SetParent(SampleTran, false); 
    }
}

基本コピペで動く筈
不具合とかあったら教えてクレメンス

追記 多分2D限定
https://mizutanikirin.net/unity-clonegameobject
ここに3D用のアレコレとか書かれてる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?