Unity 打开Windows文件窗口

准备

  1. Github:ookii-dialogs-winforms,下载后缀为nupkg的文件,修改后缀为rar,解压打开找到Ookii.Dialogs.WinForms.dll
  2. Assets目录下新建css.rsp文件,文件内容为:-r:System.Windows.Forms.dll
  3. API等级调整为.NET Framework
  4. 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();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值