Skip to content

Instantly share code, notes, and snippets.

@david-hodgetts
Created December 24, 2014 12:20
Show Gist options
  • Save david-hodgetts/25e26a4699c6afc8a45a to your computer and use it in GitHub Desktop.
Save david-hodgetts/25e26a4699c6afc8a45a to your computer and use it in GitHub Desktop.
public static class Extensions
{
/// <summary>
/// extension method on Array of T
/// returns true if array only contains null items
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static bool ContainsOnlyNullItems<T>(this T[] array){
foreach (var item in array){
if (item != null) return false;
}
return true;
}
/// <summary>
/// extension method on Array of T
/// returns true if any item in Array is non null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static bool AnyNonNull<T>(this T[] array) {
return !array.ContainsOnlyNullItems();
}
public static T GetInterface<T>(this GameObject inObj) where T : class
{
if (!typeof(T).IsInterface)
{
Debug.LogError(typeof(T).ToString() + ": is not an interface!");
return null;
}
return inObj.GetComponents(typeof(MonoBehaviour)).OfType<T>().FirstOrDefault();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment