Creating Class Extensions in Unity

Creating Class Extensions in Unity

Category : C# , Unity

So… you’ve got a useful method that does some pretty cool stuff but you don’t know where to put it. You’ve probably labeled this in your mind as a helper method which can be easily defined as a method that specifically aids in one or more repetitive tasks.

Great you say.. lets just create a new C# script and call it HelperMethods and throw it in there. This is not a bad idea and honestly I used to do this a lot myself but… there is a more elegant approach to this.

What if there was a way that you could add functionality to an already existing class? What if for instance you wrote a method which could easily be part of the Random class that is part of the UnityEngine?

This is actually exactly what I did. I wrote a method that returns not just a random range between min and max values but found that I needed to return a random number with specific numbers being excluded between the min and max values.

This is a perfect example of a method that should be an extension method to the Random class. So how do we do it? Its actually really simple. First know that we can place all of our extension methods within one simple class. Lets call this class ExtensionMethods.

Here’s what the code will look like:

Let’s have a look at this code line-by-line:

First, if you don’t already know this, the first 2 lines are part of every C# script created with Unity. These 2 lines are statements that basically load what are called namespaces which are nothing more then a collection of classes. So using UnityEngine is simply stating that we want to have access to all of the Unity classes.

This line simply defines the class as static which is required for creating a class that will contain extension methods along with declaring the class name.

This line of code is where we actually define that this method is an extension method. We do this by first defining it as a static method and then by adding the keyword this with our first parameter being the class that we are adding an extension for. If this would have been an extension method for the Transform or GameObject class then the first parameter would have been this Transform trans and this GameObject gobject respectively. The names after the classes are just variables that refer to this instance.

The last few lines are only specific to the actual method which I will cover in another post.

So there you have it. Now you know how to create extensions for any class. This is so much better then putting them all in a class and then having to remember that they are in that class. Its much more elegant to be able to use these types of methods by using the instance of the class of which they should be a part of. Which actually brings up one last tip.

These method extensions will only work with an instance of a class. So you can’t call them from the class directly. You must first create an instance of your class such as the code below:

Here we create an instance of the Random class with a variable named rand. We then use rand to invoke our RangeWithExclusions method.

Well… that’s pretty much everything. Stay tuned for the next post where I cover the RangeWithExclusions method in details. Questions / Suggestions? Comment below.

Tell your friends what you found!
The following two tabs change content below.
I've been in the Geek category for as long as I can remember. I am into anything technology and Sci-Fi related. I like tinkering with electronics as well as writing applications for Mac & iPhone.

Latest posts by Chris Fletcher (see all)

Liked it? Take a second to support Chris Fletcher on Patreon!

About Author

Chris Fletcher

I've been in the Geek category for as long as I can remember. I am into anything technology and Sci-Fi related. I like tinkering with electronics as well as writing applications for Mac & iPhone.

Leave a Reply

Show Some Love

Support The Weekly Coder on Patreon!

WeeklyCoderTV

Recent Popular Info

About:
This channel is all about coding. We will mostly post tutorials on making games with Unity 5 and above. Other posts will include tutorials on specific game mechanics and whatever else we feel like talking about.

Don't be shy.. If you have a question, leave a comment. If you you'd like to see a specific tutorial feel free to request it. If we think its worthwhile, then we may just do it.

Please don't forget to click the like button and remember to subscribe. Feel free to support our channel and the awesome content we create.

Total subscribers:
767

Total uploads:
71

Total upload views:
89.1 thousand

Follow me on Twitter

Subscribe

RSS Feed

Blog Authors