コガネブログ

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

【Unity】プレハブを EditorWindow から Hierarchy や Project ビューにドラッグ&ドロップするサンプル

ソースコード

using UnityEditor;
using UnityEngine;

public class Example : EditorWindow
{
    [MenuItem( "Window/Example" )]
    private static void Open()
    {
        GetWindow<Example>();
    }

    private void OnGUI()
    {
        if ( Event.current.type != EventType.MouseDrag ) return;

        var prefab = AssetDatabase.LoadAssetAtPath<GameObject>( "Assets/Cube.prefab" );

        DragAndDrop.PrepareStartDrag();
        DragAndDrop.objectReferences = new Object[] { prefab };
        DragAndDrop.StartDrag( "Dragging" );

        Event.current.Use();
    }
}