准备
- Github:ookii-dialogs-winforms,下载后缀为nupkg的文件,修改后缀为rar,解压打开找到Ookii.Dialogs.WinForms.dll
- Assets目录下新建css.rsp文件,文件内容为:-r:System.Windows.Forms.dll
- API等级调整为.NET Framework
- Unity版本2021.3.6f1c1
使用
using Ookii.Dialogs.WinForms;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System;
using UnityEngine;
public class Dialog
{
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
public void OpenWindowFolder()
{
var openFileDialog = new VistaOpenFileDialog();
openFileDialog.Multiselect = false;//是否多选
openFileDialog.Title = "Open Folder";//窗口名称
openFileDialog.InitialDirectory = "D:\\";//首次打开的窗口路径
openFileDialog.RestoreDirectory = false;//重定向
openFileDialog.FilterIndex = 1;//过滤索引
openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";//选择的文件
try
{
if (openFileDialog.ShowDialog(new WindowFolder(GetActiveWindow())) == DialogResult.OK)
{
Debug.Log(openFileDialog.FileName);//单个文件路径
for (int i = 0; i < openFileDialog.FileNames.Length; i++)//多个文件路径
{
Debug.Log("路径" + openFileDialog.FileNames[i]);
}
}
}
catch (Exception e)
{
Debug.Log(e.StackTrace);
Debug.Log(e.Source);
Debug.Log(e.Message);
}
}
}
using System;
using System.Windows.Forms;
public class WindowFolder : IWin32Window
{
IntPtr handle;
public WindowFolder(IntPtr handle)
{
this.handle = handle;
}
public IntPtr Handle
{
get { return handle; }
}
}
测试
if (GUILayout.Button("test"))
{
var dialog = new Dialog();
dialog.OpenWindowFolder();
}
文章来源地址https://www.toymoban.com/news/detail-663279.html
文章来源:https://www.toymoban.com/news/detail-663279.html
到了这里,关于Unity 打开Windows文件窗口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!