Useful Unity C# Extension Methods


Get The Most Flexibility Out Of Unity With Extension Methods

Unity3D comes with a lot of helpful classes and functions but sometimes you will encounter cases where custom implementations are required.

Rather than writing those functionalities as completely separate from the classes that already exist, We can just 'extended' those classes to have our custom functionalities.
This leads to code that is easily discoverable, clean and that can be re-used across multiple projects.

This is not a tutorial on extension methods, For that go, HERE.
In this tutorial we will go over some really useful cases where we can improve on the default Unity code base with extension methods.

1. Converting from Vector3 to Vector3Int
public static Vector3Int ConvertToVector3(this Vector3 vec3)
{
   return new Vector3Int((int)vec3.x, (int)vec3.y, (int)vec3.z);
}
2. Resetting the Transform
public static void ResetTransformation(this Transform trans)
{
   trans.position = Vector3.zero;
   trans.localRotation = Quaternion.identity;
   trans.localScale = new Vector3(1, 1, 1);
}
3. Rotating a 2D Vector
public static Vector2 Rotate(this Vector2 vector, float degrees)
{
   float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
   float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);

   float tx = vector.x;
   float ty = vector.y;
   vector.x = (cos * tx) - (sin * ty);
   vector.y = (sin * tx) + (cos * ty);
   return vector;
}
4. Returning a normalized degree value
public static float RotationNormalizedDeg(this float rotation)
{
   rotation = rotation % 360f;
   if (rotation < 0)
       rotation += 360f;
   return rotation;
}
5. Setting children layer value recursively
public static void SetLayerRecursively(this GameObject gameObject, int layer)
{
   gameObject.layer = layer;
   foreach (Transform t in gameObject.transform)
        t.gameObject.SetLayerRecursively(layer);
}
6. Checks if GameObject has a certain component
public static bool HasComponent(this Component component) where T : Component
{
   return component.GetComponent() != null;
}
7. Destroys all children recursively of a GameObject
public static void DestroyChildren(this GameObject parent)
{
   Transform[] children = new Transform[parent.transform.childCount];
   for (int i = 0; i < parent.transform.childCount; i++)
        children[i] = parent.transform.GetChild(i);
   for (int i = 0; i < children.Length; i++)
        GameObject.Destroy(children[i].gameObject);
}
8. Children of one GameObject gets transferred to another GameObject
public static void MoveChildren(this GameObject from, GameObject to)
{
    Transform[] children = new Transform[from.transform.childCount];
    for (int i = 0; i < from.transform.childCount; i++)
         children[i] = from.transform.GetChild(i);
    for (int i = 0; i < children.Length; i++)
         children[i].SetParent(to.transform);
}
9. Checks if Renderer can be seen by camera
public static bool IsVisibleFrom(this Renderer renderer, Camera camera)
{
   var planes = GeometryUtility.CalculateFrustumPlanes(camera);
   return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
10. Checks if a Rect component intersects another Rect
public static bool Intersects(this Rect source, Rect rect)
{
   return !((source.x > rect.xMax) || (source.xMax < rect.x) || (source.y > rect.yMax) || (source.yMax < rect.y));
} 
Well I hope you learnt something of value.
Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.
If you have any questions that you might have about shaders, C# or Unity development in general don't be shy and leave a message on my facebook page or down in the comments.
For More Unity Tutorials, go HERE.
For C# Tutorials, go HERE.