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.

85 lines
2.5 KiB

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