using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using UnityEngine; using UnityEngine.Events; using Object = UnityEngine.Object; namespace MyFrameworkPure { /// /// 集合类拓展 /// public static class CollectionsExtension { /// /// 遍历对象 /// /// /// 遍历的对象 /// 做的事 public static void ForEach(this IEnumerable ienumerable,UnityAction callback) { foreach (var i in ienumerable) { callback?.Invoke(i); } } /// /// 带索引的对象遍历 /// /// /// /// public static void For(this T[] array, UnityAction callback) { for (int i = 0; i < array.Count(); i++) { callback?.Invoke(array[i], i); } } /// /// 获取数组中的随机元素 /// /// /// /// public static T Random(this IList iList) { return iList[UnityEngine.Random.Range(0, iList.Count)]; } /// /// 添加一个元素到数组尾 /// /// /// /// /// public static T[] Add(this T[] array,T element) { T[] newArray = new T[array.Length+1]; array.CopyTo(newArray,0); newArray[newArray.Length - 1] = element; return newArray; } /// /// 添加一个元素到数组头 /// /// /// /// /// public static T[] AddToHead(this T[] array, T element) { T[] newArray = new T[array.Length + 1]; array.CopyTo(newArray, 1); newArray[0] = element; return newArray; } /// /// 添加一个数组到数组尾 /// /// /// /// /// public static T[] Add(this T[] array, T[] elements) { T[] newArray = new T[array.Length + elements.Length]; array.CopyTo(newArray,0); elements.CopyTo(newArray,array.Length); return newArray; } /// /// 从数组移除一个元素 /// /// /// /// /// public static T[] Remove(this T[] array, T element) { var list = array.ToList(); list.Remove(element); return list.ToArray(); } /// /// 数组是否为null或长度为0 /// /// /// public static bool IsNullOrEmpty(this IList list) { return list == null || list.Count == 0; } /// /// 删除整个迭代器中的物体 /// /// /// //public static void Destroy(this IEnumerable ienumerable) where T:Component //{ // foreach (var i in ienumerable) // { // Object.Destroy(i); // } //} public static void Destroy(this IList list) where T : Component { for (int i = list.Count - 1; i >= 0; i--) { Object.Destroy(list[i]); } } /// /// 立刻删除整个迭代器中的物体 /// /// /// public static void DestroyImmediate(this IEnumerable ienumerable) where T : Object { foreach (var i in ienumerable) { Object.DestroyImmediate(i); } } public static void DestroyImmediate(this IEnumerable ienumerable,bool allowDestroyingAssets) where T : Object { foreach (var i in ienumerable) { Object.DestroyImmediate(i,allowDestroyingAssets); } } /// /// 拼接集合,以字符串输出 /// /// /// /// public static string JointToString(this IEnumerable iEnumerable,char jointChar) { StringBuilder sb = new StringBuilder(); foreach (var i in iEnumerable) { if (sb.Length != 0) sb.Append(jointChar); sb.Append(i); } return sb.ToString(); } public static void Log(this IEnumerable iEnumerable) { foreach (var i in iEnumerable) { Debug.Log(i); } } /// /// 获取元素的索引 /// /// /// /// /// public static int IndexOf(this T[] array, T element) { for (int i = 0; i < array.Length; i++) { if (array[i].Equals(element)) return i; } return -1; } } }