Unity打开Windows的文件浏览器
这里分别贴了一个保存和打开的功能,大家各取所需吧.话不多说,直接上代码!!!
/// <summary>
/// 打开文件浏览器选取文件
/// </summary>
/// <returns></returns>
public static void OpenProject()
{
OpenFileDlg pth = new OpenFileDlg();
pth.structSize = Marshal.SizeOf(pth);
pth.filter = "All files (*.*)|*.*";
pth.file = new string(new char[256]);
pth.maxFile = pth.file.Length;
pth.fileTitle = new string(new char[64]);
pth.maxFileTitle = pth.fileTitle.Length;
pth.initialDir = UnityEngine.Application.dataPath.Replace("/", "\\") + "\\Resources"; //默认路径
pth.title = "打开项目";
pth.defExt = "dat";
pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (OpenFileDialog.GetOpenFileName(pth))
{
string filepath = pth.file; //选择的文件路径;
Debug.Log(filepath);//这里Dbug的其实就是打开的你刚才选中的那个文件的路径
}
}
/// <summary>
/// 打开文件浏览器保存文件
/// </summary>
public static void SaveProject()
{
SaveFileDlg pth = new SaveFileDlg();
pth.structSize = Marshal.SizeOf(pth);
pth.filter = "All files (*.*)|*.*";
pth.file = new string(new char[256]);
pth.maxFile = pth.file.Length;
pth.fileTitle = new string(new char[64]);
pth.maxFileTitle = pth.fileTitle.Length;
pth.initialDir = UnityEngine.Application.dataPath; //默认路径
pth.title = "保存项目";
pth.defExt = "xlsx";
pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (SaveFileDialog.GetSaveFileName(pth))
{
string filepath = pth.file; //选择的文件路径;
}
}
先这些吧,有空再写。
书接上回,忘记一些代码