top of page
Writer's picturesantosh nalla

Extension Methods in Unity

Lets say you want to add a function to Transform class called Reset() - which resets position, Rotation and Scale values of a particular transform - Like transform.Reset().


How are you gonna add this function? Firstly - unity doesn't give you access to modify the Transform class.


I can also give you an another example here -

Lets say you want to add a Shuffle() function to List class, which shuffles the list. It is the same case here - you donot have access to List class in unity.


Unity doesn't allow you to modify its standard classes like Transform, Vector, List etc.


Lets go to our first example, that is you need to add a function called Reset(), which resets the transform.

One way you can do it is via Helper class. Declare a Helper class, put all the common methods as static methods in this helper class so that everyone can use them.


public class HelperClass
{
    public static void Reset(.....)
      {
            .....
      }

}

Then you would call this function using HelperClass.Reset(...).


This serves our purpose, but it doesn't look clean.

Instead, I would love to Extend the Transform class, and Define a method Reset() and call directly using transform.Reset(). This is done using Extension Methods.


How do i create an extension Method??

1. To create an extension method, you first need a public static class.

2. Next in that class, you need to create a public static method, which takes "this" keyword as our first parameter.

The first parameter is not the one which you pass on when you call this function, but it is the one from which this extension method is called from.


Confused? Let me write a extension method called Reset as discussed above.


public static class TransformExtensionMethodsClass
{
      public static void Reset(this Transform trans)
        {   
                trans.position = Vector3.zero;
                trans.scale= Vector3.zero;
                trans.rotation= Vector3.zero;
        }
}

Note that the Reset Parameter has one argument "Transform" which is attached with a this keyword.

When you want to use this function,

you simply call this function like ----- transform.Reset() - which will modify the transfrom according to the function.


In a similar way, you can write any extension methods to Vectors, Transforms, etc which are not given by Unity by not touching the standard classes.


Extension methods can also take parameters. If you want to pass any parameter, declare those parameters after the first "this" parameter

Ex:  public static void Reset(this Transform trans, int arg1, float arg2......)

40 views0 comments

Recent Posts

See All

Asynchronous Programming in Unity

Most Unity Developers might be aware of the concept of Asynchronous Programming - if “Coroutines” are what came into your mind, yes, that...

LINQ - C# in Unity Game Programing

LINQ stands for Language Integrated Query. Basically this is used/allows us to filter through data(arrays and lists). Data filtering on...

Delegates, Events, Actions, Func

DELEGATES: Delegates and Events are usually super confusing. Here in this blog i will make them as simple as possible. Usually Delegates...

Comments


bottom of page