ExtensionDictionary.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using UnityEngine;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public static class ExtensionDictionary
  6. {
  7. public static T2 Random<T1, T2>(this Dictionary<T1, T2> dic, bool remove = false, ExtList.RandomDelegate<T2> randomDelegate = null)
  8. {
  9. while (true)
  10. {
  11. int index = UnityEngine.Random.Range(0, dic.Count);
  12. T1 resultKey = dic.Keys.ToList()[index];
  13. T2 resultValue = dic.Values.ToList()[index];
  14. if (randomDelegate != null)
  15. {
  16. if (!randomDelegate(resultValue))
  17. {
  18. continue;
  19. }
  20. }
  21. if (remove)
  22. {
  23. dic.Remove(resultKey);
  24. return resultValue;
  25. }
  26. else
  27. {
  28. return resultValue;
  29. }
  30. }
  31. }
  32. public static bool UniqueAdd<T1, T2>(this Dictionary<T1, T2> dic, T1 t1, T2 t2)
  33. {
  34. if (dic.ContainsKey(t1))
  35. {
  36. return false;
  37. }
  38. else
  39. {
  40. dic.Add(t1, t2);
  41. return true;
  42. }
  43. }
  44. }