ExtensionList.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public static class ExtensionList
  5. {
  6. public static T Last<T>(this List<T> list, int index)
  7. {
  8. return list[list.Count - 1 - index];
  9. }
  10. public static T Prev<T>(this List<T> list, int index)
  11. {
  12. return list[(index + list.Count - 1) % list.Count];
  13. }
  14. public static T Next<T>(this List<T> list, int index)
  15. {
  16. return list[(index + 1) % list.Count];
  17. }
  18. public static T Random<T>(this List<T> list, bool remove = false)
  19. {
  20. if (list.Count == 0)
  21. {
  22. Debug.Log("Count is 0");
  23. }
  24. int index = UnityEngine.Random.Range(0, list.Count);
  25. if (remove)
  26. {
  27. T result = list[index];
  28. list.RemoveAt(index);
  29. return result;
  30. }
  31. else
  32. {
  33. return list[index];
  34. }
  35. }
  36. public static bool Valid<T>(this List<T> list)
  37. {
  38. if (list == null || list.Count == 0)
  39. {
  40. return false;
  41. }
  42. else
  43. {
  44. return true;
  45. }
  46. }
  47. public static bool UniqueAdd<T>(this List<T> list, T obj)
  48. {
  49. if (list.Contains(obj) == false)
  50. {
  51. list.Add(obj);
  52. return true;
  53. }
  54. else
  55. {
  56. return false;
  57. }
  58. }
  59. public static void LastRemoveAt<T>(this List<T> list, int index)
  60. {
  61. list.RemoveAt(list.Count - 1 - index);
  62. }
  63. }