当前位置:   article > 正文

Unity 打开Windows文件窗口_ookii.dialogs.winforms

ookii.dialogs.winforms

准备

  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);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
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; }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

测试

if (GUILayout.Button("test"))
{
    var dialog = new Dialog();
    dialog.OpenWindowFolder();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/783231
推荐阅读
相关标签
  

闽ICP备14008679号