My first real post is going to be about extension methods. While they’re fairly normal to see in the .NET development world, I tend not to see them much when it comes to Unity, so here’s my quick primer on them.
Though they can be used with Javascript, they can’t be used in the much more elegant way C# does them, nor can they be declared in Javascript, so this post is really for C# only.
So, what can they do for you?
The idea behind them is allowing for adding functionality to a preexisting Type, regardless of whether you have access to the source or not. This means that you can add your own methods to the built in Unity classes (amongst other things) to make using them more manageable.
Note that I said Type, not class. You can use them to add methods to interface types as well, which you can’t do in the interface itself.
Now, for an example; Imagine that you dislike the fact that Rigidbody has no simple way to set its position via an offset instead of using a world position. To get around this, you could code your own extension method to do it:
using UnityEngine; public static class RigidbodyExtensions { public static void Translate(this Rigidbody rigidbody, Vector3 offset) { Translate(rigidbody, offset, Space.Self); } public static void Translate(this Rigidbody rigidbody, Vector3 offset, Space space) { if (space == Space.Self) { //rotate the local space offset into world space offset = rigidbody.rotation * offset; } rigidbody.position += offset; } }
Now, if you don’t understand all of the syntax, don’t worry, I’ll get to that soon.
My example shows two Translate methods, both with the same syntax as Transform.Translate. It very simply sets the position of the Rigidbody, using the original Rigidbody position and the given offset, rotated into world space if Space.Self was wanted.
The two things to look at regarding extension methods are the class definition and the method definitions.
For extension methods to work, they need to be placed inside static classes. This (besides the technical reasons behind it) helps to keep the extension methods away from your other code, preferably in obviously named classes with something like the word Extensions on the end.
For the method definition, extension methods need to be static, and their first parameter needs to have the word ‘this’ before the Type. The this parameter is not actually passed to the method by the calling code, but is instead assigned with the object the extension method is called on.
To use the above methods is fairly simple:
//move forward by 1 meter per second (if called in FixedUpdate) rigidbody.Translate(Vector3.forward * Time.deltaTime); //move by Vector3(5, 5, 5) in world space rigidbody.Translate(new Vector3(5, 5, 5), Space.World);
Note the lack of the static class, or the first Rigidbody parameter for the function.
This technique can be applied to many situations, giving you a large boost in productivity with the right extension methods.
If you want to see some more examples, check out my extension methods on the wiki:
IsVisibleFrom Renderer Extension
A simple extension to check if a Renderer is visible from a specific Camera
LayerMask Extensions
A set of extensions to help create and manipulate LayerMasks by name
Nice post.
Great for building up a library of helpers personalized for your coding style that you can re-use on each project.
Thanks, and indeed, I actually have a set of extensions I use in multiple projects, pulling them into a project as a git submodule.
It’s a fairly useful way to make up for design issues in closed source situations.