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

时间: 2023-11-14 admin 维修知识

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

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

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

///
/工具: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);