|
|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
-
- /// <summary>
- /// Class used to write extension methodes in
- /// </summary>
- public static class ExtensionMethods
- {
-
- /// <summary>
- /// Converts provided Direction to Global Vector3
- /// </summary>
- /// <param name="direction">Direction to convert</param>
- /// <returns>Global Vector3 direction</returns>
- 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;
- }
- }
-
- /// <summary>
- /// Converts provided Direction to local Vector3
- /// </summary>
- /// <param name="direction">Direction to convert</param>
- /// <param name="transform">Transform to use as directions local space</param>
- /// <returns>Local Vector3 direction</returns>
- 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;
- }
- }
-
- /// <summary>
- /// Gets the rect of a RectTransform in World coordinates
- /// </summary>
- /// <param name="rt">RectTransform to get rect from</param>
- /// <returns>rect of a RectTransform in World coordinates </returns>
- 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;
- }
-
-
-
- /// <summary>
- /// Applies an action to each element in an IEnumerable
- /// </summary>
- /// <param name="enumeration">IEnumerable to loop therough</param>
- /// <param name="action">Action to apply to each element</param>
- public static void ForEach<T>(this IEnumerable<T> enumeration, System.Action<T> action)
- {
- foreach (T item in enumeration)
- action(item);
- }
-
- public static T maxBy<T>(this IEnumerable<T> values, Func<T, IComparable> 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;
- }
- }
-
-
|