This commit is contained in:
MuHua-123
2025-03-07 19:20:55 +08:00
parent c8f1873939
commit 5e819d5257
98 changed files with 389 additions and 180 deletions
@@ -0,0 +1,89 @@
using System;
using System.Runtime.InteropServices;
namespace MuHua {
public static class FileTool {
#region
public static bool OpenFile(string title, out string path, params string[] type) {
FileDialog fd = new FileDialog();
fd.structSize = Marshal.SizeOf(fd);
fd.filter = FileType(type);
fd.file = new string(new char[256]);
fd.maxFile = fd.file.Length;
fd.fileTitle = new string(new char[64]);
fd.maxFileTitle = fd.fileTitle.Length;
fd.initialDir = "C:/";
fd.title = title;
fd.defExt = type[0];
fd.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
bool result = GetOpenFileName(fd);
path = fd.file;
return result;
}
public static string FileType(params string[] array) {
string type = "";
foreach (var item in array) {
type += "文件(*." + item + ")\0*." + item + "\0";
}
return type;
}
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] FileDialog fileDialog);
#endregion
#region
[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);
public static string BrowseForFolder(string title = "Path") {
BrowseFolder temp = new BrowseFolder();
temp.pszDisplayName = new string(new char[2048]);
temp.lpszTitle = title;
temp.ulFlags = 0x00000040;
IntPtr a = SHBrowseForFolder(temp);
char[] path = new char[2048];
for (int i = 0; i < 2048; i++) { path[i] = '\0'; }
SHGetPathFromIDList(a, path);
string res = new string(path);
return res.Substring(0, res.IndexOf('\0'));
}
#endregion
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FileDialog {
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public string filter = null;//筛选文件类型
public string customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public string file = null;
public int maxFile = 0;
public string fileTitle = null;
public int maxFileTitle = 0;
public string initialDir = null;//默认路径
public string title = null;
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public string defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public string templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class BrowseFolder {
public IntPtr hwndOwner = IntPtr.Zero;
public IntPtr pidlRoot = IntPtr.Zero;
public String pszDisplayName = null;
public String lpszTitle = null;
public UInt32 ulFlags = 0;
public IntPtr lpfn = IntPtr.Zero;
public IntPtr lParam = IntPtr.Zero;
public int iImage = 0;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fd18523b9fa12c14fb26cb8e7fbe6713
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,37 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace MuHua {
/// <summary>json解析与保存</summary>
public static class JsonTool {
/// <summary> 内部包装类 </summary>
private class Pack<T> { public T data; }
/// <summary> 把对象转换为Json字符串 </summary>
/// <param name="obj">对象</param>
public static string ToJson<T>(T obj) {
if (obj == null) return "null";
if (obj.GetType().GetInterface("IList") != null) {
Pack<T> pack = new Pack<T>();
pack.data = obj;
string json = JsonUtility.ToJson(pack);
return json.Substring(8, json.Length - 9);
}
return JsonUtility.ToJson(obj);
}
/// <summary> 解析Json </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="json">Json字符串</param>
public static T FromJson<T>(string json) {
if (json == "null" && typeof(T).IsClass) return default(T);
if (typeof(T).GetInterface("IList") != null) {
json = "{\"data\":{data}}".Replace("{data}", json);
Pack<T> Pack = JsonUtility.FromJson<Pack<T>>(json);
return Pack.data;
}
return JsonUtility.FromJson<T>(json);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0a0d672eab72d3942b6646e769595b33
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,120 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public static class RayTool {
public static RaycastHit hitInfo;
public static readonly LayerMask DefaultLayerMask = ~(1 << 0) | 1 << 0;
/// <summary> 鼠标坐标转世界坐标 </summary>
public static bool GetMouseToWorldPosition(out Vector3 position) {
return GetScreenToWorldPosition(Input.mousePosition, out position);
}
/// <summary> 鼠标坐标转世界坐标 </summary>
public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position) {
return GetScreenToWorldPosition(camera, Input.mousePosition, out position);
}
/// <summary> 鼠标坐标转世界坐标 </summary>
public static bool GetMouseToWorldPosition(out Vector3 position, LayerMask planeLayerMask) {
return GetScreenToWorldPosition(Input.mousePosition, out position, planeLayerMask);
}
/// <summary> 鼠标坐标转世界坐标 </summary>
public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position, LayerMask planeLayerMask) {
return GetScreenToWorldPosition(camera, Input.mousePosition, out position, planeLayerMask);
}
/// <summary> 屏幕坐标转世界坐标 </summary>
public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position) {
return GetScreenToWorldPosition(screen, out position, DefaultLayerMask);
}
/// <summary> 屏幕坐标转世界坐标 </summary>
public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position) {
return GetScreenToWorldPosition(camera, screen, out position, DefaultLayerMask);
}
/// <summary> 屏幕坐标转世界坐标 </summary>
public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position, LayerMask planeLayerMask) {
return GetScreenToWorldPosition(Camera.main, screen, out position, planeLayerMask);
}
/// <summary> 屏幕坐标转世界坐标 </summary>
public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position, LayerMask planeLayerMask) {
Ray ray = camera.ScreenPointToRay(screen);
Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
position = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
if (hitInfo.transform != null) { position = hitInfo.point; }
return hitInfo.transform != null;
}
/// <summary> 从鼠标坐标获取对象 </summary>
public static bool GetMouseToWorldObject<T>(out T value) where T : Object {
return GetScreenToWorldObject(Input.mousePosition, out value);
}
/// <summary> 从鼠标坐标获取对象 </summary>
public static bool GetMouseToWorldObject<T>(Camera camera, out T value) where T : Object {
return GetScreenToWorldObject(camera, Input.mousePosition, out value);
}
/// <summary> 从鼠标坐标获取对象 </summary>
public static bool GetMouseToWorldObject<T>(out T value, LayerMask planeLayerMask) where T : Object {
return GetScreenToWorldObject(Input.mousePosition, out value, planeLayerMask);
}
/// <summary> 从鼠标坐标获取对象 </summary>
public static bool GetMouseToWorldObject<T>(Camera camera, out T value, LayerMask planeLayerMask) where T : Object {
return GetScreenToWorldObject(camera, Input.mousePosition, out value, planeLayerMask);
}
/// <summary> 从屏幕坐标获取对象 </summary>
public static bool GetScreenToWorldObject<T>(Vector3 screen, out T value) where T : Object {
return GetScreenToWorldObject(screen, out value, DefaultLayerMask);
}
/// <summary> 从屏幕坐标获取对象 </summary>
public static bool GetScreenToWorldObject<T>(Camera camera, Vector3 screen, out T value) where T : Object {
return GetScreenToWorldObject(camera, screen, out value, DefaultLayerMask);
}
/// <summary> 从屏幕坐标获取对象 </summary>
public static bool GetScreenToWorldObject<T>(Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object {
return GetScreenToWorldObject(Camera.main, screen, out value, planeLayerMask);
}
/// <summary> 从屏幕坐标获取对象 </summary>
public static bool GetScreenToWorldObject<T>(Camera camera, Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object {
Ray ray = camera.ScreenPointToRay(screen);
Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
value = hitInfo.transform?.GetComponent<T>();
return value != null;
}
/// <summary> 从鼠标坐标获取碰撞信息 </summary>
public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo) {
return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo);
}
/// <summary> 从鼠标坐标获取碰撞信息 </summary>
public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo) {
return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo);
}
/// <summary> 从鼠标坐标获取碰撞信息 </summary>
public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo, LayerMask planeLayerMask) {
return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo, planeLayerMask);
}
/// <summary> 从鼠标坐标获取碰撞信息 </summary>
public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo, LayerMask planeLayerMask) {
return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo, planeLayerMask);
}
/// <summary> 从屏幕坐标获取碰撞信息 </summary>
public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo) {
return GetScreenToWorldHitInfo(screen, out hitInfo, DefaultLayerMask);
}
/// <summary> 从屏幕坐标获取碰撞信息 </summary>
public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo) {
return GetScreenToWorldHitInfo(camera, screen, out hitInfo, DefaultLayerMask);
}
/// <summary> 从屏幕坐标获取碰撞信息 </summary>
public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) {
return GetScreenToWorldHitInfo(Camera.main, screen, out hitInfo, planeLayerMask);
}
/// <summary> 从屏幕坐标获取碰撞信息 </summary>
public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) {
Ray ray = camera.ScreenPointToRay(screen);
return Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 735bce90ef65acd4c91d821d00bfbcf2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,113 @@
using System;
using System.IO;
using System.Text;
using UnityEngine;
namespace MuHua
{
public static class SaveTool
{
/// <summary>默认扩展名</summary>
public const string EXTENSION = "Json";
/// <summary>各平台本地保存路径</summary>
public static string PATH
{
#if UNITY_IOS
get { return Application.persistentDataPath;}
#elif UNITY_ANDROID
get { return Application.persistentDataPath;}
#else
get { return Application.streamingAssetsPath; }
#endif
}
#region
/// <summary>保存字符串到本地文件夹</summary>
/// <param name="directory">文件夹</param>
/// <param name="fileName">文件名</param>
/// <param name="saveString">保存内容</param>
public static void SaveText(string directory, string fileName, string saveString)
{
string filePath = directory + fileName;
if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); }
File.WriteAllText(filePath, saveString);
}
/// <summary>读取文件返回字符串</summary>
/// <param name="filePath">文件路径</param>
/// <returns>读取内容</returns>
public static string LoadText(string filePath)
{
if (File.Exists(filePath)) { return File.ReadAllText(filePath); }
else { return null; }
}
#endregion
#region
/// <summary>保存编码字符串到本地文件夹</summary>
/// <param name="directory">文件夹</param>
/// <param name="fileName">文件名</param>
/// <param name="saveString">保存内容</param>
public static void SaveEncodingString(string directory, string fileName, string saveString, Encoding encodeType)
{
string filePath = directory + fileName;
if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); }
byte[] byteArray = encodeType.GetBytes(saveString);
string base64 = Convert.ToBase64String(byteArray);
File.WriteAllText(filePath, base64);
}
/// <summary>读取编码文件返回字符串</summary>
/// <param name="filePath">文件路径</param>
/// <returns>读取内容</returns>
public static string LoadEncodingString(string filePath, Encoding encodeType)
{
if (File.Exists(filePath))
{
string base64 = File.ReadAllText(filePath);
byte[] byteArray = Convert.FromBase64String(base64);
return encodeType.GetString(byteArray);
}
else { return null; }
}
#endregion
#region Json的保存与加载
/// <summary>保存Object为Json文件</summary>
/// <param name="directory">文件夹</param>
/// <param name="fileName">文件名</param>
/// <param name="saveObject">保存数据类</param>
public static void SaveObjectToJson<TSaveObject>(string directory, string fileName, TSaveObject saveObject)
{
SaveText(directory, fileName, JsonTool.ToJson(saveObject));
}
/// <summary>加载Class</summary>
/// <typeparam name="TSaveObject">读取的类型</typeparam>
/// <param name="filePath">文件路径</param>
/// <returns>读取数据类</returns>
public static TSaveObject LoadJsonToObject<TSaveObject>(string filePath)
{
string json = LoadText(filePath);
if (json != null) { return JsonTool.FromJson<TSaveObject>(json); }
else { return default(TSaveObject); }
}
#endregion
#region Encoding的保存与加载
/// <summary>编码保存Object</summary>
/// <param name="directory">文件夹</param>
/// <param name="fileName">文件名</param>
/// <param name="saveObject">保存数据类</param>
/// <param name="encodeType">编码类型</param>
public static void SaveEncodingObject<TSaveObject>(string directory, string fileName, TSaveObject saveObject, Encoding encodeType)
{
SaveEncodingString(directory, fileName, JsonTool.ToJson(saveObject), encodeType);
}
/// <summary>加载编码Object</summary>
/// <typeparam name="TSaveObject">读取的类型</typeparam>
/// <param name="filePath">文件路径</param>
/// <param name="encodeType">编码类型</param>
/// <returns>读取数据类</returns>
public static TSaveObject LoadEncodingObject<TSaveObject>(string filePath, Encoding encodeType)
{
string json = LoadEncodingString(filePath, encodeType);
if (json != null) { return JsonTool.FromJson<TSaveObject>(json); }
else { return default(TSaveObject); }
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bc926ac96d06fc54da01608d8821318d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: