In this video, Jesse Freeman teaches you how to how to create a unity class in Unity for managing GameObjects. Utility classes allow you to execute static methods and perform one off actions that are reusable throughout your project. Learning how to create a reusable object pool system in Unity 5 is an important skill for building a 2D game.
- [Voiceover] So at this point we have the basic foundation of our game. We have our foreground, background, and our spawner's now creating objects. And as they move off the screen they're getting destroyed. Now with a game this simple we wouldn't really worry about performance. But being forward thinking, we wouldn't want to be creating all these kind of instances and then destroying them, and having to create them again. That actually incurs overhead. What we'd want to do is something called object pooling. Where each one of these objects gets created.
And then it doesn't get fully destroyed. It simply gets recycled, reconfigure, and used again. Now before we can actually implement our object pooling system, we're gonna wanna route the creation and destruction of game objects through a single point in our game. In this case we're gonna create a game object utility that'll help us manage that. Inside of our scripts folder. Let's create a new script. And we're gonna call this game object util. Then let's open this up In Mono Develop.
Now our utility is gonna be accessible throughout our game. In fact, it's not gonna even be attached to a game object. In order to do this first, let's actually remove the Mono Behavior extension. We actually don't need to extend Mono Behavior. We also won't need the start and update methods. At this point we just have a simple see sharp class. Now for other classes to access the methods on this class we're gonna create static methods.
We've been using static methods throughout the game so far. You just may have not noticed. Generally a static method is caused when you use the full name of the class and then access it's method. So let's create our first static method. Here we'll use public. Static. And we're gonna return it's hype game object. And the name of this method is gonna be instantiate. We're also gonna pass it in a reference to the game object we want to instantiate. So we'll need a type of game object.
We'll call this prefab. And we'll need a vector three representing where in the game scene we actually want to instantiate this. Now since we're not extended Mono Behavior we no longer have access to the instantiate method. We're gonna use the game objects own static method for instantiating. Let's set up a container variable that'll store the reference of the instance that we create. Here we'll start with a type of game object called this instance.
And we'll set this to null. Later on when we're going through and instantiating objects, we're gonna have to decide whether we use an instance that already exists from the object pool. Or if we create our own. But for now we'll just leave this here so that we have a way of referencing this variable before we return it at the end of our method. Now let's set the instance to equal our game object static method of instantiating the prefab itself. Then once we have the instance, we're gonna change the transform position on it to equal the position we pass into the method.
And finally, we're gonna return a reference to the instance itself. This way once we create an instance we can use it in other places of our code. The last thing we're gonna wanna do is create a destroy method. We'll do the same type of public static method except we're not gonna return anything when we destroy an object. And we'll call this destroy. Here. We're also gonna pass in a game object. And this will be the instance we want our utility to destroy.
And finally we'll use the game object classes own static destroy method to do this. So now we have a single place in our game where we can control the creation and destruction of objects in the game. In order to make this work, we're gonna have to back though two of our other classes that instantiate and destroy objects. Here we'll start with a spawner. And here where we're already calling it instantiate, let's simply add the game object utility in front of it. Now when you hit period this close to another method, it's gonna wanna create.
Mono Develop is gonna wanna auto complete. Just simply hit escape to exit out of this menu so we don't get two versions of instantiate. Also, since we're not gonna be handling the rotation since we know in our game nothing gets rotated. And we only pass two values into our instantiate method. Let's remove this last line of code. So now we create a random prefab. And we simply pass in the position. The game object utility takes care of everything else. Let's save our script.
And move on to destroy off screen. If we scroll down to out of bounds, we're gonna do the same thing here where we call destroy. Here we'll just simply use game object util. Making sure to hit escape so Mono Develop doesn't auto complete for us. And we'll save the script. Now let's go back in unity. And we're gonna run the game. Now that we're re-running the game. We should still see that objects get instantiated. And that objects get destroyed as everything is now running through our game object utility.
In the next video we're gonna talk about how we actually can start recycling objects instead of destroying them completely.
Start watching to learn how to convert artwork into sprites, create repeating background textures with Unity's TexturePacker, and build reusable game objects such as players, obstacles, and enemies. Jesse also covers the basics of object pooling, working with custom UI components, and multiplatform publishing.
- Importing artwork
- Editing sprites
- Creating repeating background textures
- Building reusable obstacles
- Recycling game objects
- Building an object pool
- Creating the player
- Starting and ending the game
- Displaying and saving the score
- Adding menus and text
- Publishing a Unity game in different formats