ExtensionList.cs 1.6 KB

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