unity沒有提供打開windows對(duì)話框的api,在開發(fā)種也會(huì)遇到選擇系統(tǒng)文件夾或選擇系統(tǒng)文件的需求文章來源地址http://www.zghlxwxcb.cn/news/detail-858191.html
///
/工具:windows系統(tǒng)文件夾/文件選擇窗口//
///
using System;
using System.Runtime.InteropServices;
/// <summary>
/// 工具:windows系統(tǒng)文件夾/文件選擇窗口
/// </summary>
public class OpenFile
{
/// <summary>
/// 選擇文件夾
/// </summary>
public static string ChooseWinFolder()
{
//使用如下:
OpenDialogDir ofn = new OpenDialogDir();
ofn.pszDisplayName = new string(new char[2000]); ; // 存放目錄路徑緩沖區(qū)
ofn.title = "選擇文件夾";// 標(biāo)題
//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);//這個(gè)就是選擇的目錄路徑
}
/// <summary>
/// 選擇應(yīng)用程序文件
/// </summary>
public static string ChooseWinFile()
{
OpenFileName OpenFileName = new OpenFileName();
OpenFileName.structSize = Marshal.SizeOf(OpenFileName);
OpenFileName.filter = "應(yīng)用程序(*.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('/', '\\');//默認(rèn)路徑
OpenFileName.title = "選擇exe文件";
OpenFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
if (WindowDll.GetOpenFileName(OpenFileName))
return OpenFileName.file;
else
return null;
}
/// <summary>
/// 選擇圖片文件
/// </summary>
public static string ChooseImageFile()
{
OpenFileName OpenFileName = new OpenFileName();
OpenFileName.structSize = Marshal.SizeOf(OpenFileName);
OpenFileName.filter = "圖片文件 (*.png;*.jpg;*.jpeg)\0*.png;*.jpg;*.jpeg";
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('/', '\\');//默認(rèn)路徑
OpenFileName.title = "選擇圖片文件";
OpenFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
if (WindowDll.GetOpenFileName(OpenFileName))
return OpenFileName.file;
else
return null;
}
}
/// <summary>
/// windows系統(tǒng)文件選擇窗口
/// </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系統(tǒng)文件夾選擇窗口
/// </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
{
//鏈接指定系統(tǒng)函數(shù) 打開文件對(duì)話框
[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);
}
//鏈接指定系統(tǒng)函數(shù) 另存為對(duì)話框
[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);
}
文章來源:http://www.zghlxwxcb.cn/news/detail-858191.html
到了這里,關(guān)于Unity C# 打開windows對(duì)話框選擇文件夾或選擇文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!