Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Editor拡張でドラッグ&ドロップした時のファイルパスを取得する
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class DragAndDropTest : EditorWindow
{
// Add menu item to the Window menu
[MenuItem("Window/DragAndDrop")]
static void Init () {
// Get existing open window or if none, make a new one:
EditorWindow.GetWindow<DragAndDropTest>(false, "DragAndDrop");
}
GUIContent btnCntent = new GUIContent();
private SerializedObject m_Object;
private SerializedProperty m_FilePath;
private string[] m_paths = new string[0];
// Implement your own editor GUI here.
void OnGUI () {
#region Horizontal
GUILayout.BeginHorizontal(GUILayout.Width(this.position.width - 20));
btnCntent.text = "新しく開き直す";
btnCntent.tooltip = "このウインドウを開きなおす";
if (GUILayout.Button(btnCntent, GUI.skin.button, GUILayout.Height(20)))
{
this.Close();
Init();
}
GUILayout.EndHorizontal();
#endregion Horizontal
var evt = Event.current;
var dropArea = GUILayoutUtility.GetRect(0.0f, 50.0f, GUILayout.ExpandWidth(true));
GUI.Box(dropArea, "Drag & Drop");
switch (evt.type)
{
case EventType.DragUpdated:
case EventType.DragPerform:
if (!dropArea.Contains(evt.mousePosition)) break;
//マウスの形状
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (evt.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
m_paths = DragAndDrop.paths;
foreach (string path in DragAndDrop.paths)
{
//GetFullPath(path);
}
foreach (var draggedObject in DragAndDrop.objectReferences)
{
Debug.Log(draggedObject);
}
DragAndDrop.activeControlID = 0;
}
Event.current.Use();
break;
}
foreach (string path in m_paths)
{
EditorGUILayout.TextField("DragAndDrop.path", path);
EditorGUILayout.TextField("FullPath", GetFullPath(path));
EditorGUILayout.Space();
}
}
private bool IsMyAssetsPath(string path)
{
if (path.IndexOf("Assets/") == 0)
{
return true;
}
return false;
}
private string GetFullPath(string path)
{
if (IsMyAssetsPath(path))
{
path = path.Substring("Assets/".Length);
path = Application.dataPath + "/" + path;
}
else if (Path.GetFileName(path) == path)
{
path = Application.dataPath + "/../" + path;
path = Path.GetFullPath(path);
}
return path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment