using UnityEngine; using System.Runtime.InteropServices; using System; /// /// 文件控制脚本 /// public class ChinarFileController : MonoBehaviour { /// /// 打开项目 /// public static string OpenProject() { string originalDir = Environment.CurrentDirectory; string filepath = string.Empty; OpenFileDlg pth = new OpenFileDlg(); pth.structSize = Marshal.SizeOf(pth); // 修正过滤器格式(移除末尾多余的竖线) //pth.filter = @"All Files (*.*)|*.jpg"; 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 = Application.dataPath.Replace("/", "\\") + "\\Resources"; pth.title = "打开项目"; pth.defExt = "dat"; // 简化Flags设置(保留关键选项) pth.flags = 0x00080000 | 0x00001000 | 0x00000800; // OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST if (OpenFileDialog.GetOpenFileName(pth)) { filepath = pth.file; } Environment.CurrentDirectory = originalDir; return filepath; } /// /// 保存文件项目 /// public static string SaveProject(string ext) { string filepath = string.Empty; 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 = Application.dataPath; //默认路径 pth.title = "保存项目"; pth.defExt = ext; pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; if (SaveFileDialog.GetSaveFileName(pth)) { filepath = pth.file; //选择的文件路径; } return filepath; } }