107 lines
2.8 KiB
C#
107 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BF
|
|
{
|
|
/// <summary>
|
|
/// 扩展原生类方法
|
|
/// </summary>
|
|
public static class ExtensionMethods
|
|
{
|
|
public static bool HasComponent<T>(this GameObject go) where T : Component
|
|
{
|
|
return go.GetComponent<T>() != null;
|
|
}
|
|
|
|
public static T GetMissingComponent<T>(this GameObject go) where T : Component
|
|
{
|
|
T t = go.GetComponent<T>();
|
|
if (t == null)
|
|
t = go.AddComponent<T>();
|
|
return t;
|
|
}
|
|
|
|
public static void DestroyComponent<T>(this GameObject go) where T : Component
|
|
{
|
|
if (go == null)
|
|
return;
|
|
T comp = go.GetComponent<T>();
|
|
if (comp != null)
|
|
UnityEngine.Object.Destroy(comp);
|
|
}
|
|
|
|
public static bool TryAdd<T>(this List<T> list, T t)
|
|
{
|
|
if (list.Contains(t))
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
list.Add(t);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public static bool TryAdd<Key, Value>(this Dictionary<Key, Value> dic, Key k, Value v)
|
|
{
|
|
if (dic.ContainsKey(k))
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
dic.Add(k, v);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public static bool TryAdd<T>(this HashSet<T> hs, T t)
|
|
{
|
|
if (hs.Contains(t))
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
hs.Add(t);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public static List<T> TryAddRange<T>(this List<T> list, IEnumerable<T> t)
|
|
{
|
|
list.AddRange(t);
|
|
return list;
|
|
}
|
|
|
|
public static List<T> TrySort<T>(this List<T> list, Comparison<T> 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);
|
|
}
|
|
}
|
|
} |