From 0fbadf3730e8510e0511bd9e8c2186c7e004b47e Mon Sep 17 00:00:00 2001 From: MuHua-123 <136542559+MuHua-123@users.noreply.github.com> Date: Tue, 4 Mar 2025 16:19:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 2 +- Packages/Tools/Runtime.meta | 8 ++ Packages/Tools/Runtime/Global.meta | 8 ++ Packages/Tools/Runtime/Global/Assets.cs | 60 +++++++++ Packages/Tools/Runtime/Global/Assets.cs.meta | 11 ++ Packages/Tools/Runtime/Global/Handle.cs | 26 ++++ Packages/Tools/Runtime/Global/Handle.cs.meta | 11 ++ Packages/Tools/Runtime/MuHua.asmdef | 3 + Packages/Tools/Runtime/MuHua.asmdef.meta | 7 + Packages/Tools/Runtime/Single.meta | 8 ++ Packages/Tools/Runtime/Single/Single.cs | 16 +++ Packages/Tools/Runtime/Single/Single.cs.meta | 11 ++ .../Tools/Runtime/Single/SingleBehaviour.cs | 30 +++++ .../Runtime/Single/SingleBehaviour.cs.meta | 11 ++ Packages/Tools/Runtime/Tools.meta | 8 ++ Packages/Tools/Runtime/Tools/FileTool.cs | 89 +++++++++++++ Packages/Tools/Runtime/Tools/FileTool.cs.meta | 11 ++ Packages/Tools/Runtime/Tools/JsonTool.cs | 37 ++++++ Packages/Tools/Runtime/Tools/JsonTool.cs.meta | 11 ++ Packages/Tools/Runtime/Tools/RayTool.cs | 120 ++++++++++++++++++ Packages/Tools/Runtime/Tools/RayTool.cs.meta | 11 ++ Packages/Tools/Runtime/Tools/SaveTool.cs | 113 +++++++++++++++++ Packages/Tools/Runtime/Tools/SaveTool.cs.meta | 11 ++ Packages/Tools/package.json | 11 ++ Packages/Tools/package.json.meta | 7 + Packages/packages-lock.json | 6 + 26 files changed, 646 insertions(+), 1 deletion(-) create mode 100644 Packages/Tools/Runtime.meta create mode 100644 Packages/Tools/Runtime/Global.meta create mode 100644 Packages/Tools/Runtime/Global/Assets.cs create mode 100644 Packages/Tools/Runtime/Global/Assets.cs.meta create mode 100644 Packages/Tools/Runtime/Global/Handle.cs create mode 100644 Packages/Tools/Runtime/Global/Handle.cs.meta create mode 100644 Packages/Tools/Runtime/MuHua.asmdef create mode 100644 Packages/Tools/Runtime/MuHua.asmdef.meta create mode 100644 Packages/Tools/Runtime/Single.meta create mode 100644 Packages/Tools/Runtime/Single/Single.cs create mode 100644 Packages/Tools/Runtime/Single/Single.cs.meta create mode 100644 Packages/Tools/Runtime/Single/SingleBehaviour.cs create mode 100644 Packages/Tools/Runtime/Single/SingleBehaviour.cs.meta create mode 100644 Packages/Tools/Runtime/Tools.meta create mode 100644 Packages/Tools/Runtime/Tools/FileTool.cs create mode 100644 Packages/Tools/Runtime/Tools/FileTool.cs.meta create mode 100644 Packages/Tools/Runtime/Tools/JsonTool.cs create mode 100644 Packages/Tools/Runtime/Tools/JsonTool.cs.meta create mode 100644 Packages/Tools/Runtime/Tools/RayTool.cs create mode 100644 Packages/Tools/Runtime/Tools/RayTool.cs.meta create mode 100644 Packages/Tools/Runtime/Tools/SaveTool.cs create mode 100644 Packages/Tools/Runtime/Tools/SaveTool.cs.meta create mode 100644 Packages/Tools/package.json create mode 100644 Packages/Tools/package.json.meta diff --git a/.editorconfig b/.editorconfig index 9b10c4b..b54d560 100644 --- a/.editorconfig +++ b/.editorconfig @@ -16,7 +16,7 @@ charset = utf-8 indent_style = tab indent_size = 4 tab_width = 4 -  + # if (...) { #     ... diff --git a/Packages/Tools/Runtime.meta b/Packages/Tools/Runtime.meta new file mode 100644 index 0000000..5bc1644 --- /dev/null +++ b/Packages/Tools/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a3a9f0035e541db4b84669311dd168a1 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Global.meta b/Packages/Tools/Runtime/Global.meta new file mode 100644 index 0000000..306e9ef --- /dev/null +++ b/Packages/Tools/Runtime/Global.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c2293d2090267954bb2e45486f1a6fc9 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Global/Assets.cs b/Packages/Tools/Runtime/Global/Assets.cs new file mode 100644 index 0000000..de50e89 --- /dev/null +++ b/Packages/Tools/Runtime/Global/Assets.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace MuHua { + /// + /// 资源模块 + /// + public class Assets : Single> { + protected List datas = new List(); + + /// 更改事件 + public virtual event Action> OnChange; + /// 数据列表 + public virtual List Datas => datas; + /// 数据计数 + public virtual int Count => Datas.Count; + /// 数据操作 Datas[index]; + + /// 添加数据 + public virtual void Add(Data data) { Datas.Add(data); OnChange?.Invoke(this); } + /// 添加数据 + public virtual void Add(IList data) { Datas.AddRange(data); OnChange?.Invoke(this); } + /// 删除数据 + public virtual void Remove(Data data) { Datas.Remove(data); OnChange?.Invoke(this); } + /// 清除数据 + public virtual void Clear() { Datas.Clear(); OnChange?.Invoke(this); } + + /// 保存数据 + public virtual void Save() { throw new NotImplementedException(); } + /// 加载数据 + public virtual void Load() { throw new NotImplementedException(); } + + /// 循环列表 + public virtual void ForEach(Action action) => Datas.ForEach(action); + } + /// + /// 资源模块工具 + /// + public static class AssetsTool { + /// 头尾循环标准化索引 + public static Data LoopIndex(this Assets assets, int index) { + return assets[LoopIndex(index, assets.Count)]; + } + /// 头尾循环标准化索引 + public static Data LoopIndex(this List list, int index) { + return list[LoopIndex(index, list.Count)]; + } + /// 头尾循环标准化索引 + public static Data LoopIndex(this Data[] array, int index) { + return array[LoopIndex(index, array.Length)]; + } + /// 头尾循环标准化索引 + public static int LoopIndex(int index, int maxIndex) { + return index % maxIndex; + } + } +} \ No newline at end of file diff --git a/Packages/Tools/Runtime/Global/Assets.cs.meta b/Packages/Tools/Runtime/Global/Assets.cs.meta new file mode 100644 index 0000000..001217e --- /dev/null +++ b/Packages/Tools/Runtime/Global/Assets.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f240e58226b9d1440b23f97be93c4f00 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Global/Handle.cs b/Packages/Tools/Runtime/Global/Handle.cs new file mode 100644 index 0000000..0f64a43 --- /dev/null +++ b/Packages/Tools/Runtime/Global/Handle.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace MuHua { + /// + /// 事件处理器 + /// + public class Handle : Single> { + /// 数据 + protected Data value; + + /// 当前数据 + public virtual Data Current => value; + /// 当前数据是否有效 + public virtual bool IsValid => Current != null; + + /// 改变当前数据 Event + public virtual event Action OnChange; + /// 改变当前数据 + public virtual void Change() => OnChange?.Invoke(value); + /// 改变当前数据 + public virtual void Change(Data value) { this.value = value; Change(); } + } +} \ No newline at end of file diff --git a/Packages/Tools/Runtime/Global/Handle.cs.meta b/Packages/Tools/Runtime/Global/Handle.cs.meta new file mode 100644 index 0000000..93d39ec --- /dev/null +++ b/Packages/Tools/Runtime/Global/Handle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7e27b368a7807c74987bd3cb5287968a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/MuHua.asmdef b/Packages/Tools/Runtime/MuHua.asmdef new file mode 100644 index 0000000..7481b01 --- /dev/null +++ b/Packages/Tools/Runtime/MuHua.asmdef @@ -0,0 +1,3 @@ +{ + "name": "MuHua" +} diff --git a/Packages/Tools/Runtime/MuHua.asmdef.meta b/Packages/Tools/Runtime/MuHua.asmdef.meta new file mode 100644 index 0000000..798e98c --- /dev/null +++ b/Packages/Tools/Runtime/MuHua.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: b88cc9b1727cc3a45a42964e99e119c1 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Single.meta b/Packages/Tools/Runtime/Single.meta new file mode 100644 index 0000000..d311388 --- /dev/null +++ b/Packages/Tools/Runtime/Single.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cf2bc427dc33d6249adc8ab937d37331 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Single/Single.cs b/Packages/Tools/Runtime/Single/Single.cs new file mode 100644 index 0000000..74de0cc --- /dev/null +++ b/Packages/Tools/Runtime/Single/Single.cs @@ -0,0 +1,16 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace MuHua { + /// + /// 单例基类 + /// + public class Single where T : Single, new() { + /// 模块单例 + public static T I => Instantiate(); + + protected static T instance; + protected static T Instantiate() => instance == null ? instance = new T() : instance; + } +} \ No newline at end of file diff --git a/Packages/Tools/Runtime/Single/Single.cs.meta b/Packages/Tools/Runtime/Single/Single.cs.meta new file mode 100644 index 0000000..876a8e8 --- /dev/null +++ b/Packages/Tools/Runtime/Single/Single.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 864d5767e91be034eb47a0561aba685a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Single/SingleBehaviour.cs b/Packages/Tools/Runtime/Single/SingleBehaviour.cs new file mode 100644 index 0000000..6dde7b0 --- /dev/null +++ b/Packages/Tools/Runtime/Single/SingleBehaviour.cs @@ -0,0 +1,30 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace MuHua { + /// + /// 单例行为 + /// + public abstract class SingleBehaviour : MonoBehaviour where T : SingleBehaviour { + /// 模块单例 + public static T I => instance; + /// 模块单例 + protected static T instance; + /// 初始化 + protected abstract void Awake(); + + /// 替换,并且设置切换场景不销毁 + protected virtual void Replace(bool isDontDestroy = true) { + if (instance != null) { Destroy(instance.gameObject); } + instance = (T)this; + if (isDontDestroy) { DontDestroyOnLoad(gameObject); } + } + /// 不替换,并且设置切换场景不销毁 + protected virtual void NoReplace(bool isDontDestroy = true) { + if (isDontDestroy) { DontDestroyOnLoad(gameObject); } + if (instance == null) { instance = (T)this; } + else { Destroy(gameObject); } + } + } +} \ No newline at end of file diff --git a/Packages/Tools/Runtime/Single/SingleBehaviour.cs.meta b/Packages/Tools/Runtime/Single/SingleBehaviour.cs.meta new file mode 100644 index 0000000..8dff5ba --- /dev/null +++ b/Packages/Tools/Runtime/Single/SingleBehaviour.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 103d1480bcf841b40858282d95c83e99 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Tools.meta b/Packages/Tools/Runtime/Tools.meta new file mode 100644 index 0000000..6270963 --- /dev/null +++ b/Packages/Tools/Runtime/Tools.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dc33220895840b14eb2d7030498033b0 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Tools/FileTool.cs b/Packages/Tools/Runtime/Tools/FileTool.cs new file mode 100644 index 0000000..e336ddf --- /dev/null +++ b/Packages/Tools/Runtime/Tools/FileTool.cs @@ -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; + } +} \ No newline at end of file diff --git a/Packages/Tools/Runtime/Tools/FileTool.cs.meta b/Packages/Tools/Runtime/Tools/FileTool.cs.meta new file mode 100644 index 0000000..aa93231 --- /dev/null +++ b/Packages/Tools/Runtime/Tools/FileTool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fd18523b9fa12c14fb26cb8e7fbe6713 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Tools/JsonTool.cs b/Packages/Tools/Runtime/Tools/JsonTool.cs new file mode 100644 index 0000000..e79fcd7 --- /dev/null +++ b/Packages/Tools/Runtime/Tools/JsonTool.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using UnityEngine; + +namespace MuHua { + /// json解析与保存 + public static class JsonTool { + /// 内部包装类 + private class Pack { public T data; } + /// 把对象转换为Json字符串 + /// 对象 + public static string ToJson(T obj) { + if (obj == null) return "null"; + if (obj.GetType().GetInterface("IList") != null) { + Pack pack = new Pack(); + pack.data = obj; + string json = JsonUtility.ToJson(pack); + return json.Substring(8, json.Length - 9); + } + return JsonUtility.ToJson(obj); + } + /// 解析Json + /// 类型 + /// Json字符串 + public static T FromJson(string json) { + if (json == "null" && typeof(T).IsClass) return default(T); + if (typeof(T).GetInterface("IList") != null) { + json = "{\"data\":{data}}".Replace("{data}", json); + Pack Pack = JsonUtility.FromJson>(json); + return Pack.data; + } + return JsonUtility.FromJson(json); + } + } +} \ No newline at end of file diff --git a/Packages/Tools/Runtime/Tools/JsonTool.cs.meta b/Packages/Tools/Runtime/Tools/JsonTool.cs.meta new file mode 100644 index 0000000..69d7171 --- /dev/null +++ b/Packages/Tools/Runtime/Tools/JsonTool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0a0d672eab72d3942b6646e769595b33 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Tools/RayTool.cs b/Packages/Tools/Runtime/Tools/RayTool.cs new file mode 100644 index 0000000..c10f012 --- /dev/null +++ b/Packages/Tools/Runtime/Tools/RayTool.cs @@ -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; + + /// 鼠标坐标转世界坐标 + public static bool GetMouseToWorldPosition(out Vector3 position) { + return GetScreenToWorldPosition(Input.mousePosition, out position); + } + /// 鼠标坐标转世界坐标 + public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position) { + return GetScreenToWorldPosition(camera, Input.mousePosition, out position); + } + /// 鼠标坐标转世界坐标 + public static bool GetMouseToWorldPosition(out Vector3 position, LayerMask planeLayerMask) { + return GetScreenToWorldPosition(Input.mousePosition, out position, planeLayerMask); + } + /// 鼠标坐标转世界坐标 + public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position, LayerMask planeLayerMask) { + return GetScreenToWorldPosition(camera, Input.mousePosition, out position, planeLayerMask); + } + + /// 屏幕坐标转世界坐标 + public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position) { + return GetScreenToWorldPosition(screen, out position, DefaultLayerMask); + } + /// 屏幕坐标转世界坐标 + public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position) { + return GetScreenToWorldPosition(camera, screen, out position, DefaultLayerMask); + } + /// 屏幕坐标转世界坐标 + public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position, LayerMask planeLayerMask) { + return GetScreenToWorldPosition(Camera.main, screen, out position, planeLayerMask); + } + /// 屏幕坐标转世界坐标 + 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; + } + + /// 从鼠标坐标获取对象 + public static bool GetMouseToWorldObject(out T value) where T : Object { + return GetScreenToWorldObject(Input.mousePosition, out value); + } + /// 从鼠标坐标获取对象 + public static bool GetMouseToWorldObject(Camera camera, out T value) where T : Object { + return GetScreenToWorldObject(camera, Input.mousePosition, out value); + } + /// 从鼠标坐标获取对象 + public static bool GetMouseToWorldObject(out T value, LayerMask planeLayerMask) where T : Object { + return GetScreenToWorldObject(Input.mousePosition, out value, planeLayerMask); + } + /// 从鼠标坐标获取对象 + public static bool GetMouseToWorldObject(Camera camera, out T value, LayerMask planeLayerMask) where T : Object { + return GetScreenToWorldObject(camera, Input.mousePosition, out value, planeLayerMask); + } + + /// 从屏幕坐标获取对象 + public static bool GetScreenToWorldObject(Vector3 screen, out T value) where T : Object { + return GetScreenToWorldObject(screen, out value, DefaultLayerMask); + } + /// 从屏幕坐标获取对象 + public static bool GetScreenToWorldObject(Camera camera, Vector3 screen, out T value) where T : Object { + return GetScreenToWorldObject(camera, screen, out value, DefaultLayerMask); + } + /// 从屏幕坐标获取对象 + public static bool GetScreenToWorldObject(Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object { + return GetScreenToWorldObject(Camera.main, screen, out value, planeLayerMask); + } + /// 从屏幕坐标获取对象 + public static bool GetScreenToWorldObject(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(); + return value != null; + } + + /// 从鼠标坐标获取碰撞信息 + public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo) { + return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo); + } + /// 从鼠标坐标获取碰撞信息 + public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo) { + return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo); + } + /// 从鼠标坐标获取碰撞信息 + public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo, LayerMask planeLayerMask) { + return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo, planeLayerMask); + } + /// 从鼠标坐标获取碰撞信息 + public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo, LayerMask planeLayerMask) { + return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo, planeLayerMask); + } + + /// 从屏幕坐标获取碰撞信息 + public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo) { + return GetScreenToWorldHitInfo(screen, out hitInfo, DefaultLayerMask); + } + /// 从屏幕坐标获取碰撞信息 + public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo) { + return GetScreenToWorldHitInfo(camera, screen, out hitInfo, DefaultLayerMask); + } + /// 从屏幕坐标获取碰撞信息 + public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) { + return GetScreenToWorldHitInfo(Camera.main, screen, out hitInfo, planeLayerMask); + } + /// 从屏幕坐标获取碰撞信息 + 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); + } + } +} \ No newline at end of file diff --git a/Packages/Tools/Runtime/Tools/RayTool.cs.meta b/Packages/Tools/Runtime/Tools/RayTool.cs.meta new file mode 100644 index 0000000..7c48873 --- /dev/null +++ b/Packages/Tools/Runtime/Tools/RayTool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 735bce90ef65acd4c91d821d00bfbcf2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/Runtime/Tools/SaveTool.cs b/Packages/Tools/Runtime/Tools/SaveTool.cs new file mode 100644 index 0000000..aa9308b --- /dev/null +++ b/Packages/Tools/Runtime/Tools/SaveTool.cs @@ -0,0 +1,113 @@ +using System; +using System.IO; +using System.Text; +using UnityEngine; + +namespace MuHua +{ + public static class SaveTool + { + /// 默认扩展名 + public const string EXTENSION = "Json"; + /// 各平台本地保存路径 + 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 字符串保存 + /// 保存字符串到本地文件夹 + /// 文件夹 + /// 文件名 + /// 保存内容 + 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); + } + /// 读取文件返回字符串 + /// 文件路径 + /// 读取内容 + public static string LoadText(string filePath) + { + if (File.Exists(filePath)) { return File.ReadAllText(filePath); } + else { return null; } + } + #endregion + #region 编码字符串保存 + /// 保存编码字符串到本地文件夹 + /// 文件夹 + /// 文件名 + /// 保存内容 + 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); + } + /// 读取编码文件返回字符串 + /// 文件路径 + /// 读取内容 + 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的保存与加载 + /// 保存Object为Json文件 + /// 文件夹 + /// 文件名 + /// 保存数据类 + public static void SaveObjectToJson(string directory, string fileName, TSaveObject saveObject) + { + SaveText(directory, fileName, JsonTool.ToJson(saveObject)); + } + /// 加载Class + /// 读取的类型 + /// 文件路径 + /// 读取数据类 + public static TSaveObject LoadJsonToObject(string filePath) + { + string json = LoadText(filePath); + if (json != null) { return JsonTool.FromJson(json); } + else { return default(TSaveObject); } + } + #endregion + #region 对象Encoding的保存与加载 + /// 编码保存Object + /// 文件夹 + /// 文件名 + /// 保存数据类 + /// 编码类型 + public static void SaveEncodingObject(string directory, string fileName, TSaveObject saveObject, Encoding encodeType) + { + SaveEncodingString(directory, fileName, JsonTool.ToJson(saveObject), encodeType); + } + /// 加载编码Object + /// 读取的类型 + /// 文件路径 + /// 编码类型 + /// 读取数据类 + public static TSaveObject LoadEncodingObject(string filePath, Encoding encodeType) + { + string json = LoadEncodingString(filePath, encodeType); + if (json != null) { return JsonTool.FromJson(json); } + else { return default(TSaveObject); } + } + #endregion + } +} \ No newline at end of file diff --git a/Packages/Tools/Runtime/Tools/SaveTool.cs.meta b/Packages/Tools/Runtime/Tools/SaveTool.cs.meta new file mode 100644 index 0000000..be151c9 --- /dev/null +++ b/Packages/Tools/Runtime/Tools/SaveTool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bc926ac96d06fc54da01608d8821318d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Tools/package.json b/Packages/Tools/package.json new file mode 100644 index 0000000..7308030 --- /dev/null +++ b/Packages/Tools/package.json @@ -0,0 +1,11 @@ +{ + "name": "muhua-tools", + "version": "1.0.0", + "displayName": "MuHua Tools", + "description": "\u57fa\u7840\u5de5\u5177\u5305", + "author": { + "name": "MuHua", + "email": "muhua233@qq.com" + }, + "type": "tool" +} \ No newline at end of file diff --git a/Packages/Tools/package.json.meta b/Packages/Tools/package.json.meta new file mode 100644 index 0000000..4c0bd88 --- /dev/null +++ b/Packages/Tools/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 803db43165f26c84f96ee139c8fda321 +PackageManifestImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index b1b9471..24fc1c1 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -233,6 +233,12 @@ "source": "embedded", "dependencies": {} }, + "muhua-tools": { + "version": "file:Tools", + "depth": 0, + "source": "embedded", + "dependencies": {} + }, "muhua-ui-tool": { "version": "file:UITool", "depth": 0,