Unity Extensions

 

What is it?

Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type.

In the GitHub project you’ll find a number of extension methods that I’ve found useful over the years. I’ve listed a sample below.

For more information on extension methods, see the c# Programming Guide.

 

GameObject Extensions

Easily set certain variables on an a Gameobject and all children objects recursively:

public static void SetLayerRecursively(this GameObject gameObject, int layer)
{
gameObject.layer = layer;
foreach (Transform t in gameObject.transform)
{
t.gameObject.SetLayerRecursively(layer);
}
}
public static void SetCollisionRecursively(this GameObject gameObject, bool enabled)
{
var parentCollider = gameObject.GetComponent<Collider>();
if (parentCollider)
{
parentCollider.enabled = enabled;
}
Collider[] colliders = gameObject.GetComponentsInChildren<Collider>();
foreach (Collider collider in colliders)
{
collider.enabled = enabled;
}
}

Get Components on objects that have specific tag:

public static T[] GetComponentsInChildrenWithTag<T>(this GameObject gameObject, string tag) where T : Component
{
var results = new List<T>();
if (gameObject.CompareTag(tag))
{
results.Add(gameObject.GetComponent<T>());
}
foreach (Transform t in gameObject.transform)
{
results.AddRange(t.gameObject.GetComponentsInChildrenWithTag<T>(tag));
}
return results.ToArray();
}

Get specified interface attached to gameobject:

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<Component>().OfType<T>().FirstOrDefault();
}
public static IEnumerable<T> GetInterfaces<T>(this GameObject inObj) where T : class
{
if (!typeof(T).IsInterface)
{
Debug.LogError(typeof(T).ToString() + ": is not an interface");
return Enumerable.Empty<T>();
}
return inObj.GetComponents<Component>().OfType<T>();
}

 

List Extensions

Easily shuffle a list and return a random value from list.

private static Random rng = new System.Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
public static T RandomItem<T>(this IList<T> list)
{
if (list.Count == 0)
throw new System.IndexOutOfRangeException("List is empty");
return list[UnityEngine.Random.Range(0, list.Count)];
}

 

Linear remap

Remaps a number from one range to another.

public static float LinearRemap(this float value, float valueRangeMin, float valueRangeMax, float newRangeMin, float newRangeMax)
{
return (value - valueRangeMin) / (valueRangeMax - valueRangeMin) * (newRangeMax - newRangeMin) + newRangeMin;
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *