From 83495840b772b4e7e4f003d689316ff1b9db58d4 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Tue, 31 May 2022 20:00:48 +0800 Subject: [PATCH] Utils Utils --- Assets/TEngine/Runtime/Unitity/Utils.cs | 220 ++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 Assets/TEngine/Runtime/Unitity/Utils.cs diff --git a/Assets/TEngine/Runtime/Unitity/Utils.cs b/Assets/TEngine/Runtime/Unitity/Utils.cs new file mode 100644 index 00000000..9488e009 --- /dev/null +++ b/Assets/TEngine/Runtime/Unitity/Utils.cs @@ -0,0 +1,220 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using UnityEngine; +using UnityEngine.UI; + +namespace TEngine +{ + public static class Utils + { + + #region Show + public static void Show(this Graphic graphic, bool value) + { + if (graphic != null && graphic.transform != null) + { + graphic.transform.localScale = value ? Vector3.one : Vector3.zero; + } + } + + public static void Show(this GameObject go, bool value) + { + if (go != null && go.transform != null) + { + go.transform.localScale = value ? Vector3.one : Vector3.zero; + } + } + + public static void Show(this Transform transform, bool value) + { + if (transform != null) + { + transform.localScale = value ? Vector3.one : Vector3.zero; + } + } + + public static void Show(this Graphic graphic, bool value, ref bool cacheValue) + { + if (graphic != null && graphic.transform != null && value != cacheValue) + { + graphic.transform.localScale = value ? Vector3.one : Vector3.zero; + } + } + + public static void SetActive(this GameObject go, bool value, ref bool cacheValue) + { + if (go != null && value != cacheValue) + { + cacheValue = value; + go.SetActive(value); + } + } + + + #endregion + + #region IEnumerator + public static IEnumerator Wait(float second, Action action = null) + { + if (second <= 0) + { + yield break; + } + + yield return new WaitForSeconds(second); + + if (action != null) + { + action(); + } + } + + + #endregion + + #region Regex + /// + /// 直接用Regex.IsMatch(xxx,xxx)每次都会New对象 + /// + private static Dictionary _regexes = new Dictionary(); + + /// + /// 字符串是否匹配(包含/不区分大小写) + /// + /// inputStr + /// keyStr + /// 匹配模式(IgnoreCase/None) + /// bool inputStr isMatch keyStr ? + public static bool IsMatch(string inputStr, string keyStr, RegexOptions options = RegexOptions.IgnoreCase) + { + if (inputStr == null || keyStr == null) + { + return false; + } + + Regex regex; + + if (_regexes.TryGetValue(keyStr, out regex)) + { + var isMatchSuccess = regex.IsMatch(inputStr); + + return isMatchSuccess; + } + else + { + regex = new Regex(keyStr, options); + + _regexes.Add(keyStr, regex); + + return regex.IsMatch(inputStr); + } + } + + + #endregion + + #region SetSprite + /// + /// Image更改Sprite接口 + /// + /// Image对象 + /// Sprite路径,通过右键菜单Get Asset Path获取的路径 + /// 是否异步加载 + /// 置空时传入path为null + public static void SetSprite(this UnityEngine.UI.Image image, string path, bool bAsync = false) + { + LoadAsset(image, path, 0, bAsync); + } + + /// + /// SpriteRenderer更改Sprite接口 + /// + /// SpriteRenderer对象 + /// Sprite路径,通过右键菜单Get Asset Path获取的路径 + /// 是否异步加载 + /// 置空时传入path为null + public static void SetSprite(this SpriteRenderer spriteRenderer, string path, bool bAsync = false) + { + LoadAsset(spriteRenderer, path, 0, bAsync); + } + + static void LoadAsset(Component component, string path, int index, bool bAsync) where T : UnityEngine.Object + { + //TODO + } + #endregion + + #region Sort + /// + /// 使用稳定排序,使sort index相等时更新列表,任务列表项的顺序不变 + /// c# list自带的排序是不稳定排序 + /// 本排序方式为插入排序 + /// + public static void InsertSort(List array, Comparison comparison) + { + var count = array.Count; + for (var i = 1; i < count; i++) + { + var temp = array[i]; + for (var j = i - 1; j >= 0; j--) + { + if (comparison(array[j], temp) > 0) + { + array[j + 1] = array[j]; + array[j] = temp; + } + else + { + break; + } + } + } + } + + /// + /// c# list自带的排序是不稳定排序 + /// 本排序方式为快速排序,从小到大 + /// + public static void QuickSort(this List array, Comparison comparison) + { + QuickSortIndex(array, 0, array.Count - 1, comparison); + } + + private static void QuickSortIndex(this List array, int startIndex, int endIndex, Comparison comparison) + { + if (startIndex >= endIndex) + { + return; + } + var pivotIndex = QuickSortQnce(array, startIndex, endIndex, comparison); + QuickSortIndex(array, startIndex, pivotIndex - 1, comparison); + QuickSortIndex(array, pivotIndex + 1, endIndex, comparison); + } + + private static int QuickSortQnce(List array, int startIndex, int endIndex, Comparison comparison) + { + while (startIndex < endIndex) + { + var num = array[startIndex]; + if (comparison(num, array[startIndex + 1]) > 0) + { + array[startIndex] = array[startIndex + 1]; + array[startIndex + 1] = num; + startIndex++; + } + else + { + var temp = array[endIndex]; + array[endIndex] = array[startIndex + 1]; + array[startIndex + 1] = temp; + endIndex--; + } + } + + return startIndex; + } + #endregion + } +}