using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace BF { /// /// 扩展原生类方法 /// public static class ExtensionMethods { public static bool HasComponent(this GameObject go) where T : Component { return go.GetComponent() != null; } public static T GetMissingComponent(this GameObject go) where T : Component { T t = go.GetComponent(); if (t == null) t = go.AddComponent(); return t; } public static void DestroyComponent(this GameObject go) where T : Component { if (go == null) return; T comp = go.GetComponent(); if (comp != null) UnityEngine.Object.Destroy(comp); } public static bool TryAdd(this List list, T t) { if (list.Contains(t)) { return false; } else { list.Add(t); return true; } } public static bool TryAdd(this Dictionary dic, Key k, Value v) { if (dic.ContainsKey(k)) { return false; } else { dic.Add(k, v); return true; } } public static bool TryAdd(this HashSet hs, T t) { if (hs.Contains(t)) { return false; } else { hs.Add(t); return true; } } public static List TryAddRange(this List list, IEnumerable t) { list.AddRange(t); return list; } public static List TrySort(this List list, Comparison t) { list.Sort(t); return list; } // 扩展Dotween public static void SetIntId(this DG.Tweening.Tween tween, int id) { tween.intId = id; } // 扩展TextMeshPro public static TMPro.TMP_Character GetTMPCharacter(this TMPro.TMP_FontAsset fontAsset, uint index) { return fontAsset.characterLookupTable[index]; } public static void ClearTMPCharacter(this TMPro.TMP_FontAsset fontAsset) { fontAsset.characterLookupTable.Clear(); } public static void AddTMPCharacter(this TMPro.TMP_FontAsset fontAsset, uint index, TMPro.TMP_Character character) { fontAsset.characterLookupTable.Add(index, character); } } }