將腳本放在Assets內的Editor文件夾里。
TestWindow.cs
using UnityEngine;
using UnityEditor;
public class TestWindow:EditorWindow{
[MenuItem("Tools/TestWindow")]
static void createTestWindow() {
var window=EditorWindow.GetWindow<TestWindow>(false,"TestWindow");
window.minSize=new Vector2(400,400);
window.Show();
}
private void OnGUI() {
if(mouseOverWindow==this){//鼠標位於當前窗口
if(Event.current.type==EventType.DragUpdated){//拖入窗口未松開鼠標
DragAndDrop.visualMode=DragAndDropVisualMode.Generic;//改變鼠標外觀
}else if(Event.current.type==EventType.DragExited){//拖入窗口並松開鼠標
Focus();//獲取焦點,使unity置頂(在其他窗口的前面)
//Rect rect=EditorGUILayout.GetControlRect();
//rect.Contains(Event.current.mousePosition);//可以使用鼠標位置判斷進入指定區域
if(DragAndDrop.paths!=null){
int len=DragAndDrop.paths.Length;
for(int i=0;i<len;i++){
Debug.Log(DragAndDrop.paths[i]);//輸出拖入的文件或文件夾路徑
}
}
}
}
}
}