unity EditorWindow拖入文件或文件夾


將腳本放在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]);//輸出拖入的文件或文件夾路徑
					}
				}
			}
		}
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM