Skip to content

Instantly share code, notes, and snippets.

@romainPechot
Created November 28, 2016 13:00
Show Gist options
  • Save romainPechot/025f5c902b4f55662efa6b5a140f2835 to your computer and use it in GitHub Desktop.
Save romainPechot/025f5c902b4f55662efa6b5a140f2835 to your computer and use it in GitHub Desktop.
Array Extensions
using System.Collections.Generic;
using UnityEngine;
public static class ArrayExtensions
{
public static T GetRandom<T>(this T[] array)
{
return array[Random.Range(0, array.Length - 1)];
}// GetRandom()
public static bool Find<T>(this T[] array, T refValue)
{
return array.FindFirstIndex(refValue) != -1;
}// Find()
public static int FindFirstIndex<T>(this T[] array, T refValue)
{
for(int i = 0; i < array.Length; i++)
{
if(EqualityComparer<T>.Default.Equals(array[i], refValue))
{
return i;
}
}
return -1;
}// Find()
}// ArrayExtensions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment