using System.Collections; using System.Collections.Generic; using UnityEngine; namespace A2W { public static class Extensions { public static List Shuffle(this List _list) { List tempList = new List(_list); int rand; T tempValue; for (int i = tempList.Count - 1; i >= 0; i--) { rand = Random.Range(0, i + 1); tempValue = tempList[rand]; tempList[rand] = tempList[i]; tempList[i] = tempValue; } return tempList; } public static string RemoveNameSpece(this string typeString) { string result; string[] str = typeString.Split('.'); result = str[str.Length - 1]; return result; } } public static class MathExtensions { /// /// 返回大值 /// /// 原值 /// 比较值 /// 结果 public static float Max(this float a, float b) { return Mathf.Max(a, b); } public static float Min(this float a, float b) { return Mathf.Min(a, b); } public static int Max(this int a, int b) { return Mathf.Max(a, b); } public static int Min(this int a, int b) { return Mathf.Min(a, b); } public static float Abs(this float value) { return Mathf.Abs(value); } public static int Abs(this int value) { return Mathf.Abs(value); } } public static class ConfigExtensions { public static List ParseStringToIntList(this string str, char sep = ',') { List result = new List(); string[] subs = str.Split(sep); foreach (var sub in subs) { result.Add(int.Parse(sub)); } return result; } } public static class ComponentExtensions { public static T TryAddComponent(this GameObject go) where T : Component { go.TryGetComponent(out T result); if (result is null) { result = go.AddComponent(); } return result; } public static void RemoveComponent(this GameObject go) where T : Component { go.TryGetComponent(out T result); if (result) { Object.Destroy(result); } } public static T AddChildComponent(this GameObject go) where T : Component { GameObject child = new GameObject(); string[] str = typeof(T).ToString().Split('.'); child.name = str[str.Length - 1]; child.transform.SetParent(go.transform, false); T result = child.AddComponent(); return result; } } public static class AnimatorExtensions { public static bool IsAnimationDone(this Animator animator) { if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1) return true; else return false; } public static bool IsAnimationDone(this Animator animator, string name) { if (animator.GetCurrentAnimatorStateInfo(0).IsName(name) is false) return false; if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1) return true; else return false; } public static bool IsAnimation(this Animator animator, string name) { return animator.GetCurrentAnimatorStateInfo(0).IsName(name); } } }