前言
需求是這樣:從資產面板中拖拽一個文件,比如表格的xlsx吧,到某個屬性面板的框框中。然後獲取這個文件的路徑。大概如下圖所示:
之前在一個網站中看到類似的教程,後來沒翻到在哪裏了。總之實現過程如下:
正文
1、思路
首先肯定要繪製一個文本框,然後獲得他的Rect;
然後如果鼠標在拖拽中,那麼判定是否拖到這個文本框裏面了。
如果拖進來的時候有選中文件,並且文件符合選中條件,那麼就調用API獲取路徑進行填寫。
2、代碼
[CustomEditor(typeof(OrginExcelData), true)]
[CanEditMultipleObjects]
public class BaseExcelDataEditor : Editor
{
SerializedProperty Path;
private void OnEnable()
{
Path = serializedObject.FindProperty("ExcelPath");
}
public override void OnInspectorGUI()
{
//獲得一個長500的框
mExcelPathRect = EditorGUILayout.GetControlRect(GUILayout.Width(500));
EditorGUI.TextField(mExcelPathRect, curPath);
//如果鼠標正在拖拽中或拖拽結束時,並且鼠標所在位置在文本輸入框內
if ((Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragExited)&& mExcelPathRect.Contains(Event.current.mousePosition))
{
//改變鼠標的外表
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
{
string retPath = DragAndDrop.paths[0];
curPath = retPath;
}
}
Path.stringValue = curPath;
serializedObject.ApplyModifiedProperties();
}
}