unity EditorWindow通过拖拽获取文件夹或者文件路径

参考:https://blog.csdn.net/lyh916/article/details/45133101
主要就是Event、DragAndDrop的使用了。

void OnGUI()
        {
            targetGo = (GameObject)EditorGUILayout.ObjectField(targetGo, typeof(GameObject), true);
            subPath = targetGo == null ? subPath : targetGo.name;
            EditorGUILayout.LabelField(string.Format("保存路径output path:{0}", Path.Combine(path, subPath)));
            //获得一个长300的框  
            Rect rect = EditorGUILayout.GetControlRect(GUILayout.Width(300));
            //将上面的框作为文本输入框  
            path = EditorGUI.TextField(rect, path);
            subPath = EditorGUILayout.TextField(subPath);

            //如果鼠标正在拖拽中或拖拽结束时,并且鼠标所在位置在文本输入框内  
            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];
                }
            }
        ……
    }

猜你喜欢

转载自blog.csdn.net/qq_18229381/article/details/80550238