Creating Class Extensions in 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:
using UnityEngine;
using System.Collections;
public static class ExtensionMethods {
public static int RangeWithExclusions(this Random rand, int min, int max, int[] exclusions) {
int randomnum = Random.Range (min, max);
foreach (int x in exclusions) {
if (x == randomnum) {
randomnum = rand.RangeWithExclusions(min, max, exclusions);
}
}
return randomnum;
}
}
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.
public static class RandomExtensions {
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.
public static int RangeWithExclusions(this Random rand, int min, int max, int[] exclusions) {
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.
int randomnum = Random.Range (min, max);
foreach (int x in exclusions) {
if (x == randomnum) {
randomnum = rand.RangeWithExclusions(min, max, exclusions);
}
}
return randomnum;
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:
Random rand;
int[] exclusions = {0,1};
int randomnumber = rand.RangeWithExclusions(-5,5,exclusions);
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.