You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.1 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. /// <summary>
  7. /// Class used to write extension methodes in
  8. /// </summary>
  9. public static class ExtensionMethods
  10. {
  11. /// <summary>
  12. /// Converts provided Direction to Global Vector3
  13. /// </summary>
  14. /// <param name="direction">Direction to convert</param>
  15. /// <returns>Global Vector3 direction</returns>
  16. public static Vector3 ToVector(this Direction direction)
  17. {
  18. switch (direction)
  19. {
  20. case Direction.Forward:
  21. return Vector3.forward;
  22. case Direction.Right:
  23. return Vector3.right;
  24. case Direction.Back:
  25. return Vector3.back;
  26. case Direction.Left:
  27. return Vector3.left;
  28. default:
  29. return Vector3.zero;
  30. }
  31. }
  32. /// <summary>
  33. /// Converts provided Direction to local Vector3
  34. /// </summary>
  35. /// <param name="direction">Direction to convert</param>
  36. /// <param name="transform">Transform to use as directions local space</param>
  37. /// <returns>Local Vector3 direction</returns>
  38. public static Vector3 ToVector(this Direction direction, Transform transform)
  39. {
  40. switch (direction)
  41. {
  42. case Direction.Forward:
  43. return transform.forward;
  44. case Direction.Right:
  45. return transform.right;
  46. case Direction.Back:
  47. return -transform.forward;
  48. case Direction.Left:
  49. return -transform.right;
  50. default:
  51. return Vector3.zero;
  52. }
  53. }
  54. /// <summary>
  55. /// Gets the rect of a RectTransform in World coordinates
  56. /// </summary>
  57. /// <param name="rt">RectTransform to get rect from</param>
  58. /// <returns>rect of a RectTransform in World coordinates </returns>
  59. public static Rect GlobalRect(this RectTransform rt)
  60. {
  61. var r = rt.rect;
  62. Vector2 centre = rt.TransformPoint(r.center);
  63. r.size = rt.TransformVector(r.size);
  64. r.center = centre;
  65. return r;
  66. }
  67. public static Rect TransformRect(this RectTransform rt, Rect rect)
  68. {
  69. Vector2 centre = rt.TransformPoint(rect.center);
  70. rect.size = rt.TransformVector(rect.size);
  71. rect.center = centre;
  72. return rect;
  73. }
  74. /// <summary>
  75. /// Applies an action to each element in an IEnumerable
  76. /// </summary>
  77. /// <param name="enumeration">IEnumerable to loop therough</param>
  78. /// <param name="action">Action to apply to each element</param>
  79. public static void ForEach<T>(this IEnumerable<T> enumeration, System.Action<T> action)
  80. {
  81. foreach (T item in enumeration)
  82. action(item);
  83. }
  84. public static T maxBy<T>(this IEnumerable<T> values, Func<T, IComparable> predicate)
  85. {
  86. //Define smallest value as first
  87. T max = values.First();
  88. //compare all other values
  89. foreach (T item in values)
  90. if (predicate(item).CompareTo(predicate(max)) > 0)
  91. max = item;
  92. return max;
  93. }
  94. }