5 using System.Collections.Generic;
6 using System.Collections.ObjectModel;
23 public static float Duration(
this AnimationCurve curve)
25 if (curve == null || curve.length <= 1)
30 return Mathf.Abs(curve[curve.length - 1].time - curve[0].time);
40 return (ray.direction != Vector3.zero);
51 public static ReadOnlyCollection<TElement> AsReadOnly<TElement>(
this IList<TElement> elements)
53 return new ReadOnlyCollection<TElement>(elements);
62 public static ReadOnlyCollection<TElement> ToReadOnlyCollection<TElement>(
this IEnumerable<TElement> elements)
64 return elements.ToArray().AsReadOnly();
78 public static int SortedInsert<TElement>(
this List<TElement> elements, TElement toInsert, IComparer<TElement> comparer = null)
80 var effectiveComparer = (comparer ?? Comparer<TElement>.Default);
82 #if DEBUG || UNITY_EDITOR 83 for (
int iElement = 0; iElement < (elements.Count - 1); iElement++)
85 var element = elements[iElement];
86 var nextElement = elements[iElement + 1];
88 if (effectiveComparer.Compare(element, nextElement) > 0)
90 Debug.Assert(
false,
"elements must already be sorted to call this method.");
96 int searchResult = elements.BinarySearch(toInsert, effectiveComparer);
98 int insertionIndex = (searchResult >= 0)
102 elements.Insert(insertionIndex, toInsert);
104 return insertionIndex;
112 public static void DisposeElements<TElement>(
this IEnumerable<TElement> elements)
113 where TElement : IDisposable
115 foreach (var element
in elements)
129 public static void DisposeElements<TElement>(
this IList<TElement> elements)
130 where TElement : IDisposable
132 for (
int iElement = 0; iElement < elements.Count; iElement++)
134 var element = elements[iElement];
155 public static bool Approximately(
this float number,
float other,
float tolerance)
157 return (Mathf.Abs(number - other) <= tolerance);
168 public static bool Approximately(
this double number,
double other,
double tolerance)
170 return (Math.Abs(number - other) <= tolerance);
175 #region UnityEngine.Object 179 #if UNITY_EDITOR // Skip Don't Destroy On Load when editor isn't playing so test runner passes. 182 Object.DontDestroyOnLoad(target);
195 public static bool IsInLayerMask(
this GameObject gameObject, LayerMask layerMask)
197 LayerMask gameObjectMask = (1 << gameObject.layer);
198 return ((gameObjectMask & layerMask) == gameObjectMask);
211 public static IComparer<TElement> GetReversed<TElement>(
this IComparer<TElement> originalComparer)
213 return new ReverseComparer<TElement>(originalComparer);
216 private class ReverseComparer<TElement> :
219 private readonly IComparer<TElement> originalComparer;
221 public ReverseComparer(IComparer<TElement> originalComparer)
223 Debug.Assert(originalComparer != null,
"originalComparer cannot be null.");
225 this.originalComparer = originalComparer;
228 public int Compare(TElement left, TElement right)
230 return originalComparer.Compare(right, left);