1
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】【エディタ拡張】複数のオブジェクトをまとめて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上で複数のゲームオブジェクトをプレハブ化する方法](https://doruby.jp/users/motsuka/entries/UnityEditor%E4%B8%8A%E3%81%A7%E8%A4%87%E6%95%B0%E3%81%AE%E3%82%B2%E3%83%BC%E3%83%A0%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%82%92%E3%83%97%E3%83%AC%E3%83%8F%E3%83%96%E5%8C%96%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95)
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?