Unity 打开windows窗口获取选中文件的路径

Unity版本:Unity 2020.3.36f1c1
打包平台:PC版(EXE格式)
using System;
using System.Runtime.InteropServices;

/// <summary>
/// 打开 Window 文件窗口
/// </summary>
public class OpenWinFile
{
    /// <summary>
    /// 数据接收类
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private class FileDialogConfig
    {
        /// <summary>
        /// 设置窗口大小
        /// </summary>
        public int structSize = 0;
        public IntPtr dlgOwner = IntPtr.Zero;
        public IntPtr instance = IntPtr.Zero;

        /// <summary>
        /// 文件筛选
        /// </summary>
        public string filter = null;
        public string customFilter = null;
        public int maxCustFilter = 0;
        public int filterIndex = 0;

        /// <summary>
        /// 文件全名
        /// </summary>
        public string file = null;
        public int maxFile = 0;
        /// <summary>
        /// 文件名
        /// </summary>
        public string fileTitle = null;
        public int maxFileTitle = 0;

        /// <summary>
        /// 指定路径
        /// </summary>
        public string initialDir = null;
        /// <summary>
        /// 窗口名称
        /// </summary>
        public string title = null;

        public int flags = 0;
        public short fileOffset = 0;
        public short fileExtension = 0;

        public string defExt = null;
        public IntPtr custData = IntPtr.Zero;
        public IntPtr hook = IntPtr.Zero;
        public string templateName = null;
        public IntPtr reservedPtr = IntPtr.Zero;
        public int reservedInt = 0;
        public int flagsEx = 0;
    }

    /// <summary>
    /// 系统函数调用类
    /// </summary>
    private class LocalDialog
    {
        //链接指定系统函数       打开文件对话框
        [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        private static extern bool GetOpenFileName([In, Out] FileDialogConfig ofn);

        /// <summary>
        /// 打开文件
        /// </summary>
        public static bool GetOFN([In, Out] FileDialogConfig ofn)
        {
            return GetOpenFileName(ofn);
        }

        //链接指定系统函数        另存为对话框
        [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        private static extern bool GetSaveFileName([In, Out] FileDialogConfig ofn);

        /// <summary>
        /// 保存文件
        /// </summary>
        public static bool GetSFN([In, Out] FileDialogConfig ofn)
        {
            return GetSaveFileName(ofn);
        }
    }

    /// <summary>
    /// 选择文件
    /// </summary>
    /// <returns>选择文件的全名</returns>
    public static string ChooseWinFile()
    {
        FileDialogConfig openFileName = new FileDialogConfig();
        openFileName.structSize = Marshal.SizeOf(openFileName);

        // 这里设置文件类型。可以指定视频,图片等其他文件。*.*默认为所有文件
        openFileName.filter = "Excel文件(*.xlsx)\0*.xlsx";

        // 文件原始路径,格式(D:\3-图片-花.jpg)
        openFileName.file = new string(new char[256]);
        openFileName.maxFile = openFileName.file.Length;

        // 文件名字(带格式后缀),格式(3-图片-花.jpg)
        openFileName.fileTitle = new string(new char[64]);
        openFileName.maxFileTitle = openFileName.fileTitle.Length;

        // 默认路径
        openFileName.initialDir = "D:";
        openFileName.title = "请选择 Excel 文件:";

        // 注意 一下项目不一定要全选 但是0x00000008项不要缺少
        openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

        if (LocalDialog.GetOFN(openFileName))
        {
            return openFileName.file;
        }

        return null;
    }

}


注意:没必要在Unity的 Plugins 文件夹底下引入 Comdlg32.dll 的动态链接库

使用方式:

public void Open()
{
    string fileURL = OpenWinFile.ChooseWinFile();
    Debug.Log(fileURL);
}
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值