Unity C# 打开windows对话框选择文件夹或选择文件

unity没有提供打开windows对话框的api,在开发种也会遇到选择系统文件夹或选择系统文件的需求

csharp 复制代码
///
/工具:windows系统文件夹/文件选择窗口//
///
using System;
using System.Runtime.InteropServices;
public class OpenFile
{
    /// <summary>
    /// 选择文件夹
    /// </summary>
    public static string ChooseWinFolder()
    {
        //使用如下:
        OpenDialogDir ofn = new OpenDialogDir();
        ofn.pszDisplayName = new string(new char[2000]); ;     // 存放目录路径缓冲区  
        ofn.title = "选择文件夹";// 标题  
        //ofn.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框  
        IntPtr pidlPtr = WindowDll.SHBrowseForFolder(ofn);

        char[] charArray = new char[2000];
        for (int i = 0; i < 2000; i++)
            charArray[i] = '\0';

        WindowDll.SHGetPathFromIDList(pidlPtr, charArray);
        string fullDirPath = new String(charArray);
        return fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));

        //fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
        //Debug.Log(fullDirPath);//这个就是选择的目录路径
    }
    /// <summary>
    /// 选择文件
    /// </summary>
    public static string ChooseWinFile()
    {
        OpenFileName OpenFileName = new OpenFileName();
        OpenFileName.structSize = Marshal.SizeOf(OpenFileName);
        OpenFileName.filter = "应用程序(*.exe)\0*.exe";
        OpenFileName.file = new string(new char[1024]);
        OpenFileName.maxFile = OpenFileName.file.Length;
        OpenFileName.fileTitle = new string(new char[64]);
        OpenFileName.maxFileTitle = OpenFileName.fileTitle.Length;
        //OpenFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
        OpenFileName.title = "选择exe文件";
        OpenFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
        if (WindowDll.GetOpenFileName(OpenFileName))
            return OpenFileName.file;
        else
            return null;
    }
}

/// <summary>
/// windows系统文件选择窗口
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OpenFileName
{
    public int structSize;
    public IntPtr dlgOwner;
    public IntPtr instance;
    public String filter;
    public String customFilter;
    public int maxCustFilter;
    public int filterIndex;
    public String file;
    public int maxFile;
    public String fileTitle;
    public int maxFileTitle;
    public String initialDir;
    public String title;
    public int flags;
    public short fileOffset;
    public short fileExtension;
    public String defExt;
    public IntPtr custData;
    public IntPtr hook;
    public String templateName;
    public IntPtr reservedPtr;
    public int reservedInt;
    public int flagsEx;
}

/// <summary>
/// windows系统文件夹选择窗口
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OpenDialogDir
{
    public IntPtr hwndOwner;
    public IntPtr pidlRoot;
    public String pszDisplayName;
    public String title;
    public UInt32 ulFlags;
    public IntPtr lpfno;
    public IntPtr lParam;
    public int iImage;
}
public class WindowDll
{
    //链接指定系统函数       打开文件对话框
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
    public static bool GetOFN([In, Out] OpenFileName ofn)
    {
        return GetOpenFileName(ofn);
    }

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

    [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern IntPtr SHBrowseForFolder([In, Out] OpenDialogDir ofn);

    [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
}
相关推荐
yngsqq33 分钟前
cad c# 二次开发 ——动态加载dll 文件制作(loada netloadx)
c#
zh路西法1 小时前
【C++决策和状态管理】从状态模式,有限状态机,行为树到决策树(二):从FSM开始的2D游戏角色操控底层源码编写
c++·游戏·unity·设计模式·状态模式
吾与谁归in4 小时前
【C#联合halcon实现绘制ROI功能】
c#·halcon·roi
Amd7945 小时前
在不同操作系统上安装 PostgreSQL
linux·windows·macos·postgresql·操作系统·数据库管理·安装指南
ling1s5 小时前
C#核心(18)面向对象多态vob
java·开发语言·c#
橘子遇见BUG5 小时前
Unity Shader学习日记 part 3 线性代数--矩阵变换
学习·线性代数·unity·矩阵·图形渲染
月巴月巴白勺合鸟月半6 小时前
一个C#开发的APP
c#·web
我曾经是个程序员7 小时前
C#File文件基础操作大全
开发语言·c#
三天不学习7 小时前
C# 中的记录类型简介 【代码之美系列】
后端·c#·微软技术·record·记录类型