Join Jesse Freeman for an in-depth discussion in this video Building a GameObject utility class, part of Unity 5 2D Essential Training (2015).
- So at this point, we have the basic foundation of our game. We have our foreground, background and our spawner is now creating objects and as they move off the screen, they are 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 of these kinds of instances and then destroying them and having to create them again. That actually incurs overhead. What we'd wanna do is something called object pool where each one of these objects gets created and then it doesn't get fully destroyed, it simply gets recycled, reconfigured, 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 will help us manage that. Inside of our scripts folder, let's create a new script and we're gonna call this gameobjectutil. Then, let's open this up in MonoDevelop. 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 C# 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 not have noticed.
Generally, a static method is called when you use the full name of the class and then access its method. So, let's create our first static method. Here, we'll use public static. And we're gonna return a type game object. And the name of this method is going to be instantiate. We're also gonna pass it in a reference of 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 3 representing where in the game scene we actually want to instantiate this.
Now, since we're not extending 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 we create. Here we'll start with a type of game object. We'll call 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 class' 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 go back through two of our other classes that instantiate and destroy objects. Here, we'll start with a spawner. And here, where we're already calling 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, MonoDevelop 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 rotations, 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 DestroyOffScreen. 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 gameobjectutil, making sure to hit escape so MonoDevelop doesn't auto-complete for us. And we'll save the script. Now, let's go back into Unity and we're gonna run the game.
Now, that we're rerunning the game, you 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.
Super Zombie Runner riffs off the popular endless runner genre: a one-button casual game that's not only fun to play but exciting to build. The focus of the work is a reusable code base that you can extend and make your own, while learning the basics of object pooling, supporting multiple screen resolutions, tracking scoring, and multi-platform publishing. Along the way, Jesse covers key Unity features, such as working with sprites, creating animations, and leveraging the UI components.
- Working with sprites and animation
- Creating the player
- Spawning GameObjects
- Adding obstacles and collisions
- Building the game objects and logic
- Managing game state
- Creating pixel-perfect cameras
- Designing for multiple screen resolutions
- Adding scores
- Optimizing performance
- Publishing to desktop, web, and mobile platforms