LoginSignup
1
0

More than 3 years have passed since last update.

【Unity】【エディタ拡張】複数のオブジェクトをまとめてPrefab化する

Posted at

Unity標準機能で出来ると思ったら出来なかった

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

/// <summary>
/// 複数のオブジェクトをまとめてprefab化するエディタ拡張
/// </summary>
public class MultiPrefabCreator : EditorWindow {

    // 出力先
    private string PATH_FOLDER = "Assets/Resources/Prefab/Temp/";

    private HashSet<GameObject> m_dropList = new HashSet<GameObject>();

    //メニューに項目追加
    [MenuItem("拡張/MultiPrefabCreator")]
    static void open()
    {
        EditorWindow.GetWindow<MultiPrefabCreator>("MultiPrefabCreator");
    }

    void OnGUI()
    {
        EditorGUILayout.LabelField("MultiPrefabCreator");
        var dropArea = GUILayoutUtility.GetRect(0.0f, 50.0f, GUILayout.ExpandWidth(true));
        GUI.Box(dropArea, "ここにPrefab化したいものをまとめてをドラッグ&ドロップ");

        var evt = Event.current;
        switch (evt.type)
        {
            case EventType.DragPerform:
                if (!dropArea.Contains(evt.mousePosition))
                {
                    break;
                }
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                DragAndDrop.AcceptDrag();
                break;

            case EventType.DragExited:
                foreach (GameObject go in DragAndDrop.objectReferences)
                {
                    Debug.Log(go);
                    m_dropList.Add(go);
                }

                CreatePrefabs();
                Event.current.Use();
                break;
        }
    }


    /// <summary>
    /// Prefab化
    /// </summary>
    void CreatePrefabs()
    {
        foreach (GameObject go in m_dropList)
        {
            if (null == m_dropList)
            {
                return;
            }
            string prefab = PATH_FOLDER + go.name + ".prefab";
            PrefabUtility.SaveAsPrefabAsset(go,prefab);

        }
        m_dropList.Clear();
    }
}


Event.currentで処理中のEventが取れるので、

EventType.DragPerform: ドラッグアンドドロップ イベント中

カーソルの表示を変える

EventType.DragExited: ドラッグアンドドロップイベント終了

Prefab作り始める


参考

UnityEditor上で複数のゲームオブジェクトをプレハブ化する方法

1
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
1
0