This commit is contained in:
MuHua-123
2025-11-17 11:56:36 +08:00
parent 7bc98d9fe6
commit 3f4d3220b1
2 changed files with 54 additions and 20 deletions
@@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using UnityEngine; using UnityEngine;
namespace MuHua { namespace MuHua {
@@ -26,5 +27,11 @@ namespace MuHua {
public static FileName Create(string name, string directory, string extensions) { public static FileName Create(string name, string directory, string extensions) {
return new FileName() { name = name, directory = directory, extensions = extensions }; return new FileName() { name = name, directory = directory, extensions = extensions };
} }
public static FileName Parse(string path) {
string name = Path.GetFileNameWithoutExtension(path);
string directory = Path.GetDirectoryName(path);
string extensions = Path.GetExtension(path);
return new FileName() { name = name, directory = directory, extensions = extensions };
}
} }
} }
+47 -20
View File
@@ -9,39 +9,56 @@ namespace MuHua {
#if UNITY_STANDALONE_WIN #if UNITY_STANDALONE_WIN
#region [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static bool OpenFile(string title, out string path, params string[] type) { public static extern bool GetOpenFileName([In, Out] FileDialog fileDialog);
[DllImport("Shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
private static extern IntPtr SHBrowseForFolder([In, Out] BrowseFolder browseFolder);
[DllImport("Shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool SHGetPathFromIDList([In] IntPtr dlist, [In] char[] path);
[DllImport("Comdlg32.dll", CharSet = CharSet.Auto, SetLastError = true, ThrowOnUnmappableChar = true)]
public static extern bool GetSaveFileName([In, Out] FileDialog fileDialog);
/// <summary> 获取文件路径 </summary>
public static bool OpenFileType(string title, out string path, params string[] type) {
return OpenFile(title, out path, FileTypes(type));
}
/// <summary> 获取文件路径 </summary>
public static bool OpenFile(string title, out string path, string type) {
FileDialog fd = new FileDialog(); FileDialog fd = new FileDialog();
fd.structSize = Marshal.SizeOf(fd); fd.structSize = Marshal.SizeOf(fd);
fd.filter = FileType(type); fd.filter = type;
fd.file = new string(new char[256]); fd.file = new string(new char[256]);
fd.maxFile = fd.file.Length; fd.maxFile = fd.file.Length;
fd.fileTitle = new string(new char[64]); fd.fileTitle = new string(new char[64]);
fd.maxFileTitle = fd.fileTitle.Length; fd.maxFileTitle = fd.fileTitle.Length;
fd.initialDir = "C:/"; fd.initialDir = "C:/";
fd.title = title; fd.title = title;
fd.defExt = type[0];
fd.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; fd.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
bool result = GetOpenFileName(fd); bool result = GetOpenFileName(fd);
path = fd.file; path = fd.file;
return result; return result;
} }
public static string FileType(params string[] array) { /// <summary> 获取保存路径 </summary>
string type = ""; public static bool SaveFileType(string title, out string path, params string[] type) {
foreach (var item in array) { return SaveFile(title, out path, FileTypes(type));
type += "文件(*." + item + ")\0*." + item + "\0";
}
return type;
} }
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] /// <summary> 获取保存路径 </summary>
public static extern bool GetOpenFileName([In, Out] FileDialog fileDialog); public static bool SaveFile(string title, out string path, string type) {
#endregion FileDialog fd = new FileDialog();
fd.structSize = Marshal.SizeOf(fd);
#region fd.filter = type;
[DllImport("Shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] fd.file = new string(new char[256]);
private static extern IntPtr SHBrowseForFolder([In, Out] BrowseFolder browseFolder); fd.maxFile = fd.file.Length;
[DllImport("Shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] fd.fileTitle = new string(new char[64]);
public static extern bool SHGetPathFromIDList([In] IntPtr dlist, [In] char[] path); fd.maxFileTitle = fd.fileTitle.Length;
fd.initialDir = "C:/";//默认路径
fd.title = title;
fd.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
bool result = GetSaveFileName(fd);
path = fd.file;
return result;
}
/// <summary> 获取文件夹路径 </summary>
public static string BrowseForFolder(string title = "Path") { public static string BrowseForFolder(string title = "Path") {
BrowseFolder temp = new BrowseFolder(); BrowseFolder temp = new BrowseFolder();
temp.pszDisplayName = new string(new char[2048]); temp.pszDisplayName = new string(new char[2048]);
@@ -54,10 +71,20 @@ namespace MuHua {
string res = new string(path); string res = new string(path);
return res.Substring(0, res.IndexOf('\0')); return res.Substring(0, res.IndexOf('\0'));
} }
#endregion
#endif #endif
/// <summary> 文件类型 </summary>
public static string FileTypes(params string[] array) {
string type = "";
foreach (var item in array) { type += FileType(item); }
return type;
}
/// <summary> 文件类型 </summary>
public static string FileType(string expand) {
return $"(*.{expand})\0*.{expand}\0";
}
} }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FileDialog { public class FileDialog {