VirtualFramework/Assets/Scripts/Tools/ChinarFileController.cs
2025-03-06 13:28:21 +08:00

66 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System.Runtime.InteropServices;
using System;
/// <summary>
/// 文件控制脚本
/// </summary>
public class ChinarFileController : MonoBehaviour
{
/// <summary>
/// 打开项目
/// </summary>
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;
}
/// <summary>
/// 保存文件项目
/// </summary>
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;
}
}