From 7ae4ddac091372351b0fa76f6e9d9c3219a07fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Mon, 23 Mar 2026 19:27:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E6=95=B4=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComponentExtensions.cs | 7 + .../GameObjectExtensions.cs | 119 ++++++- Runtime/ABase/Helper/CameraHelper.cs | 40 --- Runtime/ABase/Helper/CameraHelper.cs.meta | 3 - Runtime/ABase/Helper/FileHelper.cs | 321 ------------------ Runtime/ABase/Helper/FileHelper.cs.meta | 12 - Runtime/ABase/Helper/GameHelper.cs | 9 - Runtime/ABase/Helper/GameObjectHelper.cs | 205 ----------- Runtime/ABase/Helper/GameObjectHelper.cs.meta | 3 - Runtime/ABase/Helper/MathHelper.cs | 130 ------- Runtime/ABase/Helper/MathHelper.cs.meta | 3 - Runtime/ABase/Helper/UnityRendererHelper.cs | 29 ++ Runtime/ABase/Utility/Utility.File.cs | 315 ++++++++++++++++- Runtime/ABase/Utility/Utility.Math.cs | 119 +++++++ .../ABase/Utility/Utility.Unity.Gameplay.cs | 4 + 15 files changed, 590 insertions(+), 729 deletions(-) delete mode 100644 Runtime/ABase/Helper/CameraHelper.cs delete mode 100644 Runtime/ABase/Helper/CameraHelper.cs.meta delete mode 100644 Runtime/ABase/Helper/FileHelper.cs delete mode 100644 Runtime/ABase/Helper/FileHelper.cs.meta delete mode 100644 Runtime/ABase/Helper/GameObjectHelper.cs delete mode 100644 Runtime/ABase/Helper/GameObjectHelper.cs.meta delete mode 100644 Runtime/ABase/Helper/MathHelper.cs delete mode 100644 Runtime/ABase/Helper/MathHelper.cs.meta diff --git a/Runtime/ABase/Extension/UnityEngine.GameObject/ComponentExtensions.cs b/Runtime/ABase/Extension/UnityEngine.GameObject/ComponentExtensions.cs index 4e1abef..b312341 100644 --- a/Runtime/ABase/Extension/UnityEngine.GameObject/ComponentExtensions.cs +++ b/Runtime/ABase/Extension/UnityEngine.GameObject/ComponentExtensions.cs @@ -5,8 +5,15 @@ namespace UnityEngine /// /// . /// + [UnityEngine.Scripting.Preserve] public static class ComponentExtensions { + [UnityEngine.Scripting.Preserve] + public static void DestroyComponent(this Component component) + { + component.SafeDestroySelf(); + } + /// /// 从目标组件中获取一个组件,如果是组件类型不存在,则添加 /// diff --git a/Runtime/ABase/Extension/UnityEngine.GameObject/GameObjectExtensions.cs b/Runtime/ABase/Extension/UnityEngine.GameObject/GameObjectExtensions.cs index 597af4b..0d0e41a 100644 --- a/Runtime/ABase/Extension/UnityEngine.GameObject/GameObjectExtensions.cs +++ b/Runtime/ABase/Extension/UnityEngine.GameObject/GameObjectExtensions.cs @@ -1,8 +1,10 @@ using System; -using System.Collections.Generic; +using UnityEngine.Rendering; +using UnityEngine.SceneManagement; namespace UnityEngine { + [UnityEngine.Scripting.Preserve] public static class GameObjectExtensions { public static void SafeDestroySelf( @@ -107,5 +109,120 @@ namespace UnityEngine } } + [UnityEngine.Scripting.Preserve] + public static void RemoveChildren(this GameObject gameObject) + { + for (var i = gameObject.transform.childCount - 1; i >= 0; i--) + { + gameObject.transform.GetChild(i).gameObject.DestroyObject(); + } + } + + [UnityEngine.Scripting.Preserve] + public static void DestroyObject(this GameObject gameObject) + { + gameObject.SafeDestroySelf(); + } + + [UnityEngine.Scripting.Preserve] + public static void Destroy(this GameObject gameObject) + { + gameObject.DestroyObject(); + } + + [UnityEngine.Scripting.Preserve] + public static GameObject FindChildGamObjectByName(string nodeName, string sceneName = null) + { + Scene scene; + if (string.IsNullOrWhiteSpace(sceneName)) + { + scene = SceneManager.GetActiveScene(); + } + else + { + scene = SceneManager.GetSceneByName(sceneName); + if (!scene.isLoaded) + { + return null; + } + } + + var rootObjects = scene.GetRootGameObjects(); + foreach (var rootObject in rootObjects) + { + var result = rootObject.FindChildGamObjectByName(nodeName); + if (result.IsNotNull()) + { + return result; + } + } + + return null; + } + + [UnityEngine.Scripting.Preserve] + public static GameObject FindChildGamObjectByName(this GameObject gameObject, string name) + { + var transform = gameObject.transform.FindChildName(name); + if (transform.IsNotNull()) + { + return transform.gameObject; + } + + return null; + } + + [UnityEngine.Scripting.Preserve] + public static GameObject Create(this Transform parent, string name) + { + Debug.Assert(!ReferenceEquals(parent, null), nameof(parent) + " == null"); + var gameObject = new GameObject(name); + gameObject.transform.SetParent(parent); + return gameObject; + } + + [UnityEngine.Scripting.Preserve] + public static GameObject Create(this GameObject parent, string name) + { + Debug.Assert(!ReferenceEquals(parent, null), nameof(parent) + " == null"); + return parent.transform.Create(name); + } + + [UnityEngine.Scripting.Preserve] + public static void ResetTransform(this GameObject gameObject) + { + gameObject.transform.localScale = Vector3.one; + gameObject.transform.localPosition = Vector3.zero; + gameObject.transform.localRotation = Quaternion.identity; + } + + [UnityEngine.Scripting.Preserve] + public static void SetSortingGroupLayer(this GameObject gameObject, string sortingLayer) + { + SortingGroup[] sortingGroups = gameObject.GetComponentsInChildren(); + foreach (SortingGroup sortingGroup in sortingGroups) + { + sortingGroup.sortingLayerName = sortingLayer; + } + } + + [UnityEngine.Scripting.Preserve] + public static void SetLayer(this GameObject gameObject, int layer, bool children = true) + { + if (gameObject.layer != layer) + { + gameObject.layer = layer; + } + + if (children) + { + Transform[] transforms = gameObject.GetComponentsInChildren(); + foreach (var transform in transforms) + { + transform.gameObject.layer = layer; + } + } + } + } } diff --git a/Runtime/ABase/Helper/CameraHelper.cs b/Runtime/ABase/Helper/CameraHelper.cs deleted file mode 100644 index a015e33..0000000 --- a/Runtime/ABase/Helper/CameraHelper.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using UnityEngine; -using UnityEngine.SceneManagement; - -namespace AlicizaX -{ - /// - /// 相机帮助类 - /// - [UnityEngine.Scripting.Preserve] - public static class CameraHelper - { - /// - /// 获取相机快照 - /// - /// 相机 - /// 缩放比 - public static Texture2D GetCaptureScreenshot(Camera main, float scale = 0.5f) - { - Rect rect = new Rect(0, 0, Screen.width * scale, Screen.height * scale); - string name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); - RenderTexture renderTexture = RenderTexture.GetTemporary((int)rect.width, (int)rect.height, 0); - renderTexture.name = SceneManager.GetActiveScene().name + "_" + renderTexture.width + "_" + renderTexture.height + "_" + name; - main.targetTexture = renderTexture; - main.Render(); - - RenderTexture.active = renderTexture; - Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false) - { - name = renderTexture.name - }; - screenShot.ReadPixels(rect, 0, 0); - screenShot.Apply(); - main.targetTexture = null; - RenderTexture.active = null; - RenderTexture.ReleaseTemporary(renderTexture); - return screenShot; - } - } -} diff --git a/Runtime/ABase/Helper/CameraHelper.cs.meta b/Runtime/ABase/Helper/CameraHelper.cs.meta deleted file mode 100644 index 47ce360..0000000 --- a/Runtime/ABase/Helper/CameraHelper.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: d6f0c95931944448a8c8f1790d20059a -timeCreated: 1663323744 \ No newline at end of file diff --git a/Runtime/ABase/Helper/FileHelper.cs b/Runtime/ABase/Helper/FileHelper.cs deleted file mode 100644 index 53b1bea..0000000 --- a/Runtime/ABase/Helper/FileHelper.cs +++ /dev/null @@ -1,321 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using UnityEngine; - -namespace AlicizaX -{ - /// - /// 文件帮助类 - /// - [UnityEngine.Scripting.Preserve] - public static class FileHelper - { - /// - /// 获取目录下的所有文件 - /// - /// 文件存放路径列表对象 - /// 目标目录 - [UnityEngine.Scripting.Preserve] - public static void GetAllFiles(List files, string dir) - { - if (!Directory.Exists(dir)) - { - return; - } - - string[] strings = Directory.GetFiles(dir); - foreach (string item in strings) - { - files.Add(item); - } - - string[] subDirs = Directory.GetDirectories(dir); - foreach (string subDir in subDirs) - { - GetAllFiles(files, subDir); - } - } - - /// - /// 清理目录 - /// - /// 目标路径 - [UnityEngine.Scripting.Preserve] - public static void CleanDirectory(string dir) - { - if (!Directory.Exists(dir)) - { - return; - } - - foreach (string subDir in Directory.GetDirectories(dir)) - { - Directory.Delete(subDir, true); - } - - foreach (string subFile in Directory.GetFiles(dir)) - { - File.Delete(subFile); - } - } - - /// - /// 目录复制 - /// - /// 源路径 - /// 目标路径 - /// - [UnityEngine.Scripting.Preserve] - public static void CopyDirectory(string srcDir, string targetDir) - { - DirectoryInfo source = new DirectoryInfo(srcDir); - DirectoryInfo target = new DirectoryInfo(targetDir); - - if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase)) - { - throw new Exception("父目录不能拷贝到子目录!"); - } - - if (!source.Exists) - { - return; - } - - if (!target.Exists) - { - target.Create(); - } - - FileInfo[] files = source.GetFiles(); - - for (int i = 0; i < files.Length; i++) - { - File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true); - } - - DirectoryInfo[] dirs = source.GetDirectories(); - - for (int j = 0; j < dirs.Length; j++) - { - CopyDirectory(dirs[j].FullName, Path.Combine(target.FullName, dirs[j].Name)); - } - } - - /// - /// 复制文件到目标目录 - /// - /// 源路径 - /// 目标路径 - /// 是否覆盖 - [UnityEngine.Scripting.Preserve] - public static void Copy(string sourceFileName, string destFileName, bool overwrite = false) - { - if (!File.Exists(sourceFileName)) - { - return; - } - - File.Copy(sourceFileName, destFileName, overwrite); - } - - /// - /// 删除文件 - /// - /// 文件路径 - [UnityEngine.Scripting.Preserve] - public static void Delete(string path) - { - File.Delete(path); - } - - /// - /// 判断文件是否存在 - /// - /// 文件路径 - /// - [UnityEngine.Scripting.Preserve] - public static bool IsExists(string path) - { -#if ENABLE_GAME_FRAME_X_READ_ASSETS - if (IsAndroidReadOnlyPath(path, out var readPath)) - { - return BlankReadAssets.BlankReadAssets.IsFileExists(readPath); - } -#endif - return File.Exists(path); - } - - [UnityEngine.Scripting.Preserve] - private static bool IsAndroidReadOnlyPath(string path, out string readPath) - { - if (Application.platform == RuntimePlatform.Android) - { - if (Utility.Path.NormalizePath(path).Contains(Utility.Path.AppResPath)) - { - readPath = path.Substring(Utility.Path.AppResPath.Length); - return true; - } - } - - readPath = null; - return false; - } - - /// - /// 移动文件到目标目录 - /// - /// 文件源路径 - /// 目标路径 - [UnityEngine.Scripting.Preserve] - public static void Move(string sourceFileName, string destFileName) - { - if (!File.Exists(sourceFileName)) - { - return; - } - - Copy(sourceFileName, destFileName, true); - Delete(sourceFileName); - } - - /// - /// 读取指定路径的文件内容 - /// - /// 文件路径 - /// - [UnityEngine.Scripting.Preserve] - public static byte[] ReadAllBytes(string path) - { -#if ENABLE_GAME_FRAME_X_READ_ASSETS - if (IsAndroidReadOnlyPath((path), out var readPath)) - { - return BlankReadAssets.BlankReadAssets.Read(readPath); - } -#endif - - return File.ReadAllBytes(path); - } - - /// - /// 读取指定路径的文件内容 - /// - /// 文件路径 - /// 编码 - /// - [UnityEngine.Scripting.Preserve] - public static string ReadAllText(string path, Encoding encoding) - { - return File.ReadAllText(path, encoding); - } - - /// - /// 读取指定路径的文件内容 - /// - /// 文件路径 - /// - [UnityEngine.Scripting.Preserve] - public static string ReadAllText(string path) - { - return File.ReadAllText(path, Encoding.UTF8); - } - - /// - /// 读取指定路径的文件内容 - /// - /// 文件路径 - /// 编码 - /// - [UnityEngine.Scripting.Preserve] - public static string[] ReadAllLines(string path, Encoding encoding) - { - return File.ReadAllLines(path, encoding); - } - - /// - /// 读取指定路径的文件内容 - /// - /// 文件路径 - /// - [UnityEngine.Scripting.Preserve] - public static string[] ReadAllLines(string path) - { - return File.ReadAllLines(path, Encoding.UTF8); - } - - /// - /// 写入指定路径的文件内容 - /// - /// 文件路径 - /// 写入内容 - /// - [UnityEngine.Scripting.Preserve] - public static void ReadAllLines(string path, byte[] buffer) - { - File.WriteAllBytes(path, buffer); - } - - /// - /// 写入指定路径的文件内容 - /// - /// 文件路径 - /// 写入的内容 - /// 编码 - /// - [UnityEngine.Scripting.Preserve] - public static void WriteAllLines(string path, string[] lines, Encoding encoding) - { - File.WriteAllLines(path, lines, encoding); - } - - /// - /// 写入指定路径的文件内容 - /// - /// 文件路径 - /// 写入的内容 - /// - [UnityEngine.Scripting.Preserve] - public static void WriteAllLines(string path, string[] lines) - { - File.WriteAllLines(path, lines, Encoding.UTF8); - } - - /// - /// 写入指定路径的文件内容 - /// - /// 文件路径 - /// 写入的内容 - /// 编码 - /// - [UnityEngine.Scripting.Preserve] - public static void WriteAllText(string path, string content, Encoding encoding) - { - File.WriteAllText(path, content, encoding); - } - - /// - /// 写入指定路径的文件内容,UTF-8 - /// - /// 文件路径 - /// 写入的内容 - /// - [UnityEngine.Scripting.Preserve] - public static void WriteAllText(string path, string content) - { - File.WriteAllText(path, content, Encoding.UTF8); - } - - /// - /// 写入指定路径的文件内容 - /// - /// 文件路径 - /// 写入的内容 - /// - [UnityEngine.Scripting.Preserve] - public static void WriteAllBytes(string path, byte[] buffer) - { - File.WriteAllBytes(path, buffer); - } - } -} diff --git a/Runtime/ABase/Helper/FileHelper.cs.meta b/Runtime/ABase/Helper/FileHelper.cs.meta deleted file mode 100644 index 06807d4..0000000 --- a/Runtime/ABase/Helper/FileHelper.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: 74cc512af7309484fa066b16175c6331 -timeCreated: 1474943113 -licenseType: Pro -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Runtime/ABase/Helper/GameHelper.cs b/Runtime/ABase/Helper/GameHelper.cs index afae8ff..0787f17 100644 --- a/Runtime/ABase/Helper/GameHelper.cs +++ b/Runtime/ABase/Helper/GameHelper.cs @@ -77,15 +77,6 @@ namespace UnityEngine /// public static string GetGuid() => System.Guid.NewGuid().ToString("N"); - /// - /// Change Cursor States. - /// - public static void ShowCursor(bool locked, bool visible) - { - Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; - Cursor.visible = visible; - } - /// /// Change the GameObject layer including all children. /// diff --git a/Runtime/ABase/Helper/GameObjectHelper.cs b/Runtime/ABase/Helper/GameObjectHelper.cs deleted file mode 100644 index 4a4eec0..0000000 --- a/Runtime/ABase/Helper/GameObjectHelper.cs +++ /dev/null @@ -1,205 +0,0 @@ -using UnityEngine; -using UnityEngine.Rendering; -using UnityEngine.SceneManagement; - -namespace UnityEngine -{ - /// - /// 游戏对象帮助类 - /// - public static class GameObjectHelper - { - /// - /// 销毁子物体 - /// - /// - [UnityEngine.Scripting.Preserve] - public static void RemoveChildren(GameObject go) - { - for (var i = go.transform.childCount - 1; i >= 0; i--) - { - Destroy(go.transform.GetChild(i).gameObject); - } - } - - /// - /// 销毁游戏物体 - /// - /// - [UnityEngine.Scripting.Preserve] - public static void DestroyObject(this GameObject gameObject) - { - if (!ReferenceEquals(gameObject, null)) - { - if (Application.isEditor && !Application.isPlaying) - { - Object.DestroyImmediate(gameObject); - return; - } - - Object.Destroy(gameObject); - } - } - - /// - /// 销毁游戏物体 - /// - /// - public static void Destroy(GameObject gameObject) - { - gameObject.DestroyObject(); - } - - /// - /// 销毁游戏组件 - /// - /// - [UnityEngine.Scripting.Preserve] - public static void DestroyComponent(UnityEngine.Component component) - { - if (!ReferenceEquals(component, null)) - { - if (Application.isEditor && !Application.isPlaying) - { - Object.DestroyImmediate(component); - return; - } - - Object.Destroy(component); - } - } - - /// - /// 在指定场景中查找特定名称的节点。 - /// - /// 场景名称。 - /// 节点名称。 - /// 找到的节点的GameObject实例,如果没有找到返回null。 - public static GameObject FindChildGamObjectByName(string nodeName, string sceneName = null) - { - UnityEngine.SceneManagement.Scene scene; - if (sceneName.IsNullOrWhiteSpace()) - { - scene = SceneManager.GetActiveScene(); - } - else - { - scene = SceneManager.GetSceneByName(sceneName); - if (!scene.isLoaded) - { - return null; - } - } - - var rootObjects = scene.GetRootGameObjects(); - foreach (var rootObject in rootObjects) - { - var result = FindChildGamObjectByName(rootObject, nodeName); - if (result.IsNotNull()) - { - return result; - } - } - - return null; - } - - /// - /// 根据游戏对象名称查询子对象 - /// - /// - /// - /// - [UnityEngine.Scripting.Preserve] - public static GameObject FindChildGamObjectByName(GameObject gameObject, string name) - { - var transform = gameObject.transform.FindChildName(name); - if (transform.IsNotNull()) - { - return transform.gameObject; - } - - return null; - } - - /// - /// 创建游戏对象 - /// - /// - /// - /// - [UnityEngine.Scripting.Preserve] - public static GameObject Create(Transform parent, string name) - { - Debug.Assert(!ReferenceEquals(parent, null), nameof(parent) + " == null"); - var gameObject = new GameObject(name); - gameObject.transform.SetParent(parent); - return gameObject; - } - - /// - /// 创建游戏对象 - /// - /// - /// - /// - [UnityEngine.Scripting.Preserve] - public static GameObject Create(GameObject parent, string name) - { - Debug.Assert(!ReferenceEquals(parent, null), nameof(parent) + " == null"); - return Create(parent.transform, name); - } - - /// - /// 重置游戏对象的变换数据 - /// - /// - /// - [UnityEngine.Scripting.Preserve] - public static void ResetTransform(GameObject gameObject) - { - gameObject.transform.localScale = Vector3.one; - gameObject.transform.localPosition = Vector3.zero; - gameObject.transform.localRotation = Quaternion.identity; - } - - /// - /// 设置对象的显示排序层 - /// - /// 游戏对象 - /// 显示层 - [UnityEngine.Scripting.Preserve] - public static void SetSortingGroupLayer(GameObject gameObject, string sortingLayer) - { - SortingGroup[] sortingGroups = gameObject.GetComponentsInChildren(); - foreach (SortingGroup sg in sortingGroups) - { - sg.sortingLayerName = sortingLayer; - } - } - - /// - /// 设置对象的层 - /// - /// 游戏对象 - /// 层 - /// 是否设置子物体 - [UnityEngine.Scripting.Preserve] - public static void SetLayer(GameObject gameObject, int layer, bool children = true) - { - if (gameObject.layer != layer) - { - gameObject.layer = layer; - } - - if (children) - { - Transform[] transforms = gameObject.GetComponentsInChildren(); - foreach (var sg in transforms) - { - sg.gameObject.layer = layer; - } - } - } - } -} diff --git a/Runtime/ABase/Helper/GameObjectHelper.cs.meta b/Runtime/ABase/Helper/GameObjectHelper.cs.meta deleted file mode 100644 index df7572b..0000000 --- a/Runtime/ABase/Helper/GameObjectHelper.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 998123e39155448eb8a773436d34eac8 -timeCreated: 1666446475 \ No newline at end of file diff --git a/Runtime/ABase/Helper/MathHelper.cs b/Runtime/ABase/Helper/MathHelper.cs deleted file mode 100644 index 7b0dd64..0000000 --- a/Runtime/ABase/Helper/MathHelper.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System; -using UnityEngine; - -namespace AlicizaX -{ - /// - /// 数学帮助类 - /// - public static class MathHelper - { - /// - /// 检查两个矩形是否相交 - /// - /// - /// - /// - public static bool CheckIntersect(RectInt src, RectInt target) - { - int minX = Math.Max(src.x, target.x); - int minY = Math.Max(src.y, target.y); - int maxX = Math.Min(src.x + src.width, target.x + target.width); - int maxY = Math.Min(src.y + src.height, target.y + target.height); - if (minX >= maxX || minY >= maxY) - { - return false; - } - - return true; - } - - /// - /// 检查两个矩形是否相交 - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) - { - int minX = Math.Max(x1, x2); - int minY = Math.Max(y1, y2); - int maxX = Math.Min(x1 + w1, x2 + w2); - int maxY = Math.Min(y1 + h1, y2 + h2); - if (minX >= maxX || minY >= maxY) - { - return false; - } - - return true; - } - - /// - /// 检查两个矩形是否相交,并返回相交的区域 - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - private static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, out RectInt rect) - { - rect = default; - int minX = Math.Max(x1, x2); - int minY = Math.Max(y1, y2); - int maxX = Math.Min(x1 + w1, x2 + w2); - int maxY = Math.Min(y1 + h1, y2 + h2); - if (minX >= maxX || minY >= maxY) - { - return false; - } - - rect.x = minX; - rect.y = minY; - rect.width = Math.Abs(maxX - minX); - rect.height = Math.Abs(maxY - minY); - return true; - } - - /// - /// 检查两个矩形相交的点 - /// - /// A 坐标X - /// A 坐标Y - /// A 宽度 - /// A 高度 - /// B 坐标X - /// B 坐标Y - /// B 宽度 - /// B 高度 - /// 交叉点列表 - /// 返回是否相交 - public static bool CheckIntersectPoints(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, int[] intersectPoints) - { - Vector2Int dPt = new Vector2Int(); - - if (false == CheckIntersect(x1, y1, w1, h1, x2, y2, w2, h2, out var rectInt)) - { - return false; - } - - for (var i = 0; i < w1; i++) - { - for (var n = 0; n < h1; n++) - { - if (intersectPoints[i * h1 + n] == 1) - { - dPt.x = x1 + i; - dPt.y = y1 + n; - if (rectInt.Contains(dPt)) - { - intersectPoints[i * h1 + n] = 0; - } - } - } - } - - return true; - } - } -} diff --git a/Runtime/ABase/Helper/MathHelper.cs.meta b/Runtime/ABase/Helper/MathHelper.cs.meta deleted file mode 100644 index 1b416c4..0000000 --- a/Runtime/ABase/Helper/MathHelper.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: c10ee3753d16449f96d506192b4652c6 -timeCreated: 1670988225 \ No newline at end of file diff --git a/Runtime/ABase/Helper/UnityRendererHelper.cs b/Runtime/ABase/Helper/UnityRendererHelper.cs index 55b8c95..194ff2e 100644 --- a/Runtime/ABase/Helper/UnityRendererHelper.cs +++ b/Runtime/ABase/Helper/UnityRendererHelper.cs @@ -1,4 +1,6 @@ +using System; using UnityEngine; +using UnityEngine.SceneManagement; namespace AlicizaX { @@ -30,5 +32,32 @@ namespace AlicizaX Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera); return GeometryUtility.TestPlanesAABB(planes, renderer.bounds); } + + /// + /// 获取相机快照 + /// + /// 相机 + /// 缩放比 + public static Texture2D GetCaptureScreenshot(Camera main, float scale = 0.5f) + { + Rect rect = new Rect(0, 0, Screen.width * scale, Screen.height * scale); + string name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); + RenderTexture renderTexture = RenderTexture.GetTemporary((int)rect.width, (int)rect.height, 0); + renderTexture.name = SceneManager.GetActiveScene().name + "_" + renderTexture.width + "_" + renderTexture.height + "_" + name; + main.targetTexture = renderTexture; + main.Render(); + + RenderTexture.active = renderTexture; + Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false) + { + name = renderTexture.name + }; + screenShot.ReadPixels(rect, 0, 0); + screenShot.Apply(); + main.targetTexture = null; + RenderTexture.active = null; + RenderTexture.ReleaseTemporary(renderTexture); + return screenShot; + } } } diff --git a/Runtime/ABase/Utility/Utility.File.cs b/Runtime/ABase/Utility/Utility.File.cs index beb8ef6..cf49887 100644 --- a/Runtime/ABase/Utility/Utility.File.cs +++ b/Runtime/ABase/Utility/Utility.File.cs @@ -1,4 +1,10 @@ -namespace AlicizaX +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using UnityEngine; + +namespace AlicizaX { public static partial class Utility { @@ -7,7 +13,7 @@ /// public static class File { - private static readonly string[] UnitList = new[] {"B", "KB", "MB", "GB", "TB", "PB"}; + private static readonly string[] UnitList = new[] { "B", "KB", "MB", "GB", "TB", "PB" }; /// /// 获取字节大小 @@ -42,6 +48,311 @@ return length < 1024 * 1024 * 1024 ? $"{(length / 1024f / 1024f):F2} MB" : $"{(length / 1024f / 1024f / 1024f):F2} GB"; } + /// + /// 获取目录下的所有文件 + /// + /// 文件存放路径列表对象 + /// 目标目录 + [UnityEngine.Scripting.Preserve] + public static void GetAllFiles(List files, string dir) + { + if (!Directory.Exists(dir)) + { + return; + } + + string[] strings = Directory.GetFiles(dir); + foreach (string item in strings) + { + files.Add(item); + } + + string[] subDirs = Directory.GetDirectories(dir); + foreach (string subDir in subDirs) + { + GetAllFiles(files, subDir); + } + } + + /// + /// 清理目录 + /// + /// 目标路径 + [UnityEngine.Scripting.Preserve] + public static void CleanDirectory(string dir) + { + if (!Directory.Exists(dir)) + { + return; + } + + foreach (string subDir in Directory.GetDirectories(dir)) + { + Directory.Delete(subDir, true); + } + + foreach (string subFile in Directory.GetFiles(dir)) + { + File.Delete(subFile); + } + } + + /// + /// 目录复制 + /// + /// 源路径 + /// 目标路径 + /// + [UnityEngine.Scripting.Preserve] + public static void CopyDirectory(string srcDir, string targetDir) + { + DirectoryInfo source = new DirectoryInfo(srcDir); + DirectoryInfo target = new DirectoryInfo(targetDir); + + if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase)) + { + throw new Exception("父目录不能拷贝到子目录!"); + } + + if (!source.Exists) + { + return; + } + + if (!target.Exists) + { + target.Create(); + } + + FileInfo[] files = source.GetFiles(); + + for (int i = 0; i < files.Length; i++) + { + File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true); + } + + DirectoryInfo[] dirs = source.GetDirectories(); + + for (int j = 0; j < dirs.Length; j++) + { + CopyDirectory(dirs[j].FullName, Path.Combine(target.FullName, dirs[j].Name)); + } + } + + /// + /// 复制文件到目标目录 + /// + /// 源路径 + /// 目标路径 + /// 是否覆盖 + [UnityEngine.Scripting.Preserve] + public static void Copy(string sourceFileName, string destFileName, bool overwrite = false) + { + if (!System.IO.File.Exists(sourceFileName)) + { + return; + } + + File.Copy(sourceFileName, destFileName, overwrite); + } + + /// + /// 删除文件 + /// + /// 文件路径 + [UnityEngine.Scripting.Preserve] + public static void Delete(string path) + { + File.Delete(path); + } + + /// + /// 判断文件是否存在 + /// + /// 文件路径 + /// + [UnityEngine.Scripting.Preserve] + public static bool IsExists(string path) + { +#if ENABLE_GAME_FRAME_X_READ_ASSETS + if (IsAndroidReadOnlyPath(path, out var readPath)) + { + return BlankReadAssets.BlankReadAssets.IsFileExists(readPath); + } +#endif + return System.IO.File.Exists(path); + } + + [UnityEngine.Scripting.Preserve] + private static bool IsAndroidReadOnlyPath(string path, out string readPath) + { + if (Application.platform == RuntimePlatform.Android) + { + if (Utility.Path.NormalizePath(path).Contains(Utility.Path.AppResPath)) + { + readPath = path.Substring(Utility.Path.AppResPath.Length); + return true; + } + } + + readPath = null; + return false; + } + + /// + /// 移动文件到目标目录 + /// + /// 文件源路径 + /// 目标路径 + [UnityEngine.Scripting.Preserve] + public static void Move(string sourceFileName, string destFileName) + { + if (!System.IO.File.Exists(sourceFileName)) + { + return; + } + + Copy(sourceFileName, destFileName, true); + Delete(sourceFileName); + } + + /// + /// 读取指定路径的文件内容 + /// + /// 文件路径 + /// + [UnityEngine.Scripting.Preserve] + public static byte[] ReadAllBytes(string path) + { +#if ENABLE_GAME_FRAME_X_READ_ASSETS + if (IsAndroidReadOnlyPath((path), out var readPath)) + { + return BlankReadAssets.BlankReadAssets.Read(readPath); + } +#endif + + return File.ReadAllBytes(path); + } + + /// + /// 读取指定路径的文件内容 + /// + /// 文件路径 + /// 编码 + /// + [UnityEngine.Scripting.Preserve] + public static string ReadAllText(string path, Encoding encoding) + { + return File.ReadAllText(path, encoding); + } + + /// + /// 读取指定路径的文件内容 + /// + /// 文件路径 + /// + [UnityEngine.Scripting.Preserve] + public static string ReadAllText(string path) + { + return File.ReadAllText(path, Encoding.UTF8); + } + + /// + /// 读取指定路径的文件内容 + /// + /// 文件路径 + /// 编码 + /// + [UnityEngine.Scripting.Preserve] + public static string[] ReadAllLines(string path, Encoding encoding) + { + return File.ReadAllLines(path, encoding); + } + + /// + /// 读取指定路径的文件内容 + /// + /// 文件路径 + /// + [UnityEngine.Scripting.Preserve] + public static string[] ReadAllLines(string path) + { + return File.ReadAllLines(path, Encoding.UTF8); + } + + /// + /// 写入指定路径的文件内容 + /// + /// 文件路径 + /// 写入内容 + /// + [UnityEngine.Scripting.Preserve] + public static void ReadAllLines(string path, byte[] buffer) + { + File.WriteAllBytes(path, buffer); + } + + /// + /// 写入指定路径的文件内容 + /// + /// 文件路径 + /// 写入的内容 + /// 编码 + /// + [UnityEngine.Scripting.Preserve] + public static void WriteAllLines(string path, string[] lines, Encoding encoding) + { + File.WriteAllLines(path, lines, encoding); + } + + /// + /// 写入指定路径的文件内容 + /// + /// 文件路径 + /// 写入的内容 + /// + [UnityEngine.Scripting.Preserve] + public static void WriteAllLines(string path, string[] lines) + { + File.WriteAllLines(path, lines, Encoding.UTF8); + } + + /// + /// 写入指定路径的文件内容 + /// + /// 文件路径 + /// 写入的内容 + /// 编码 + /// + [UnityEngine.Scripting.Preserve] + public static void WriteAllText(string path, string content, Encoding encoding) + { + File.WriteAllText(path, content, encoding); + } + + /// + /// 写入指定路径的文件内容,UTF-8 + /// + /// 文件路径 + /// 写入的内容 + /// + [UnityEngine.Scripting.Preserve] + public static void WriteAllText(string path, string content) + { + File.WriteAllText(path, content, Encoding.UTF8); + } + + /// + /// 写入指定路径的文件内容 + /// + /// 文件路径 + /// 写入的内容 + /// + [UnityEngine.Scripting.Preserve] + public static void WriteAllBytes(string path, byte[] buffer) + { + File.WriteAllBytes(path, buffer); + } } } } diff --git a/Runtime/ABase/Utility/Utility.Math.cs b/Runtime/ABase/Utility/Utility.Math.cs index ae6b296..3952c0d 100644 --- a/Runtime/ABase/Utility/Utility.Math.cs +++ b/Runtime/ABase/Utility/Utility.Math.cs @@ -178,6 +178,125 @@ namespace AlicizaX return Vector3.zero; } + + /// + /// 检查两个矩形是否相交 + /// + /// + /// + /// + public static bool CheckIntersect(RectInt src, RectInt target) + { + int minX = System.Math.Max(src.x, target.x); + int minY = System.Math.Max(src.y, target.y); + int maxX = System.Math.Min(src.x + src.width, target.x + target.width); + int maxY = System.Math.Min(src.y + src.height, target.y + target.height); + if (minX >= maxX || minY >= maxY) + { + return false; + } + + return true; + } + + /// + /// 检查两个矩形是否相交 + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) + { + int minX = System.Math.Max(x1, x2); + int minY = System.Math.Max(y1, y2); + int maxX = System.Math.Min(x1 + w1, x2 + w2); + int maxY = System.Math.Min(y1 + h1, y2 + h2); + if (minX >= maxX || minY >= maxY) + { + return false; + } + + return true; + } + + /// + /// 检查两个矩形是否相交,并返回相交的区域 + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + private static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, out RectInt rect) + { + rect = default; + int minX = System.Math.Max(x1, x2); + int minY = System.Math.Max(y1, y2); + int maxX = System.Math.Min(x1 + w1, x2 + w2); + int maxY = System.Math.Min(y1 + h1, y2 + h2); + if (minX >= maxX || minY >= maxY) + { + return false; + } + + rect.x = minX; + rect.y = minY; + rect.width = System.Math.Abs(maxX - minX); + rect.height = System.Math.Abs(maxY - minY); + return true; + } + + /// + /// 检查两个矩形相交的点 + /// + /// A 坐标X + /// A 坐标Y + /// A 宽度 + /// A 高度 + /// B 坐标X + /// B 坐标Y + /// B 宽度 + /// B 高度 + /// 交叉点列表 + /// 返回是否相交 + public static bool CheckIntersectPoints(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, int[] intersectPoints) + { + Vector2Int dPt = new Vector2Int(); + + if (false == CheckIntersect(x1, y1, w1, h1, x2, y2, w2, h2, out var rectInt)) + { + return false; + } + + for (var i = 0; i < w1; i++) + { + for (var n = 0; n < h1; n++) + { + if (intersectPoints[i * h1 + n] == 1) + { + dPt.x = x1 + i; + dPt.y = y1 + n; + if (rectInt.Contains(dPt)) + { + intersectPoints[i * h1 + n] = 0; + } + } + } + } + + return true; + } } } } diff --git a/Runtime/ABase/Utility/Utility.Unity.Gameplay.cs b/Runtime/ABase/Utility/Utility.Unity.Gameplay.cs index 0c7ae86..be64cef 100644 --- a/Runtime/ABase/Utility/Utility.Unity.Gameplay.cs +++ b/Runtime/ABase/Utility/Utility.Unity.Gameplay.cs @@ -1,4 +1,7 @@ +using System; using UnityEngine; +using UnityEngine.SceneManagement; +using Object = UnityEngine.Object; namespace AlicizaX { @@ -101,6 +104,7 @@ namespace AlicizaX return PlayOneShot3D(position, clip.audioClip, clip.volume, name); } + } } }