【unity editor】通過拖拽獲取檔案路徑

NO IMAGE
原文連線 : 點選開啟連結




using UnityEngine;
using System.Collections;
using UnityEditor;
public class TestDrag : EditorWindow {
private string path;
Rect rect;
[MenuItem ("Window/TestDrag")]
private static void Init()
{
EditorWindow.GetWindow(typeof(TestDrag));
}
void OnGUI()
{
EditorGUILayout.LabelField("路徑");
rect = EditorGUILayout.GetControlRect(GUILayout.Width(400));
path = EditorGUI.TextField(rect, path);
if((Event .current .type == EventType .dragUpdated ||
Event.current .type == EventType.DragExited )&&
rect .Contains (Event .current .mousePosition))
{
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
if(DragAndDrop .paths != null && DragAndDrop .paths .Length > 0)
{
path = DragAndDrop.paths[0];
}
}
}
}