【unity editor】通过拖拽获取文件路径

原文连接 : 点击打开链接




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];
            }
        }
    }
}

你可能感兴趣的:(unity,editor)