unity 打开文件夹获取选择的文件名称和后缀名
时间: 2023-12-14 14:39:32 浏览: 228
你可以使用 `EditorUtility.OpenFilePanel` 方法打开一个文件选择对话框,然后从返回的路径中获取文件名和后缀名。以下是一个示例代码:
```csharp
using UnityEngine;
using UnityEditor;
using System.IO;
public class OpenFileDialog : EditorWindow
{
string filePath = "";
string fileName = "";
string fileExtension = "";
[MenuItem("Tools/Open File Dialog")]
static void Init()
{
OpenFileDialog window = (OpenFileDialog)EditorWindow.GetWindow(typeof(OpenFileDialog));
window.Show();
}
void OnGUI()
{
GUILayout.Label("Selected File Path: " + filePath);
GUILayout.Label("Selected File Name: " + fileName);
GUILayout.Label("Selected File Extension: " + fileExtension);
if (GUILayout.Button("Open File Dialog"))
{
filePath = EditorUtility.OpenFilePanel("Select File", "", "");
if (filePath.Length > 0)
{
fileName = Path.GetFileNameWithoutExtension(filePath);
fileExtension = Path.GetExtension(filePath);
}
}
}
}
```
在 Unity 编辑器中,选择 `Tools/Open File Dialog` 菜单项,将打开一个窗口,在窗口中点击 `Open File Dialog` 按钮,将弹出文件选择对话框。选择一个文件后,窗口中将显示所选文件的路径、文件名和文件扩展名。
阅读全文