using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; /// /// Class used to write extension methodes in /// public static class ExtensionMethods { /// /// Converts provided Direction to Global Vector3 /// /// Direction to convert /// Global Vector3 direction public static Vector3 ToVector(this Direction direction) { switch (direction) { case Direction.Forward: return Vector3.forward; case Direction.Right: return Vector3.right; case Direction.Back: return Vector3.back; case Direction.Left: return Vector3.left; default: return Vector3.zero; } } /// /// Converts provided Direction to local Vector3 /// /// Direction to convert /// Transform to use as directions local space /// Local Vector3 direction public static Vector3 ToVector(this Direction direction, Transform transform) { switch (direction) { case Direction.Forward: return transform.forward; case Direction.Right: return transform.right; case Direction.Back: return -transform.forward; case Direction.Left: return -transform.right; default: return Vector3.zero; } } /// /// Gets the rect of a RectTransform in World coordinates /// /// RectTransform to get rect from /// rect of a RectTransform in World coordinates public static Rect GlobalRect(this RectTransform rt) { var r = rt.rect; Vector2 centre = rt.TransformPoint(r.center); r.size = rt.TransformVector(r.size); r.center = centre; return r; } /// /// Applies an action to each element in an IEnumerable /// /// IEnumerable to loop therough /// Action to apply to each element public static void ForEach(this IEnumerable enumeration, System.Action action) { foreach (T item in enumeration) action(item); } public static T maxBy(this IEnumerable values, Func predicate) { //Define smallest value as first T max = values.First(); //compare all other values foreach (T item in values) if (predicate(item).CompareTo(predicate(max)) > 0) max = item; return max; } }