コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】UnityEditor.GameObjectUtility.DuplicateGameObject でゲームオブジェクトを複製する方法

概要

using UnityEditor;

internal static class Example
{
    [MenuItem( "Tools/Example" )]
    private static void Run()
    {
        var gameObject = Selection.activeGameObject;
        var clone      = GameObjectUtility.DuplicateGameObject( gameObject );

        Selection.activeGameObject = clone;
    }
}

Unity 2023.1.0f1 から UnityEditor.GameObjectUtility.DuplicateGameObject
ゲームオブジェクトを複製できるようになった。

using UnityEditor;

internal static class Example
{
    [MenuItem( "Tools/Example" )]
    private static void Run()
    {
        var gameObjects = Selection.gameObjects;
        var clones      = GameObjectUtility.DuplicateGameObjects( gameObjects );

        Selection.objects = clones;
    }
}

複数のゲームオブジェクトを複製できる
GameObjectUtility.DuplicateGameObjects も追加された。

参考サイト様