Static Methods and Static Classes
Utility classes basically have “helper” methods so you can streamline your development process. We’re going to define what those methods are. Imagine you’re creating several cubes and setting their positions for several of your games. Instead of manually copying and pasting your code into each game, you can have one Utility class that has code you can share with each of your games.
Non-static Classes with Static Members
Note for non-static classes with static members, you can optionally inherit MonoBehaviour.
For example, you may decide to define a new struct type that has static members, but the struct type itself (per-instance) may store a Vector3 and a Color together.
This is fine.
You may decide to define a new Hitbox MonoBehaviour class, but decide to define a static float that represents the defaultDamage, even though you can add Hitboxes to GameObjects (as components), the class is still allowed to have single fields that exist for the entire duration of the program, and is shared by all instances.
This is also fine.
Static Classes
On the other hand, within a static class, it’s important to know that:
- You can’t inherit MonoBehaviour and you can’t apply it to game objects.
- Everything in this class must be static for it to compile (otherwise you’ll see an error).
- The static call is not attached to anything and it doesn’t have any references.
Let’s create a new static class and call it GameObjectUtility.
Note that we’re writing more than one line of code and our method is doing more than one thing. This is because if our static method was only doing one thing, this is what we’d call a wrapper method.
The “utility” (in the name) comes from the fact that the method does multiple things for you, so you don’t have to rewrite multiple lines of code many times throughout your code.
A wrapper method typically does not do anything new other than just wrapping existing functionality and although it’s not technically wrong to do this, it’s much more efficient and makes more sense to have our helper methods have multiple lines of code.
We have the option to pass in parameters into our methods. In CreateCubeAndSetPos(), we will need to pass in a Game Object and a Vector3. The Game Object will be the empty object that we create a primitive cube on and the Vector3 will be the position we want to set it to.
So let’s write the logic in our helper method. We start with creating a primitive cube. PrimitiveType is an enum and then we can select Cube. This will create a cube inside of Unity. Then we will set the object’s position to newPosition.
How to Use Your Static Methods
We’ll create an empty game object in Unity, make a new Primitive Cube script and attach it to that game object.
We’re going to call CreateCubeAndSetPos() when we press the Space key. We don’t need to get a reference to it and we don’t need to use the new keyword to create a new object of this type. It’s a static class and so we can access it at the class level and call its static method.
So that’s the benefit of working with static Utility classes! You can pass in parameters, you can use return type methods, etc.. Just know, because it is a static class with static methods, this class is going to be held in memory for the life of the game.