- [Instructor] So now that we have our player set up with all of its animations, let's talk about how we can save this player's GameObject and reuse it throughout our game. Let's click on the root of our Jetroid folder and create a new folder, and we're going to call this Prefabs. In Unity, a prefab is basically a template of a GameObject we want to use. Whatever GameObject we drag into the Prefab folder will become the prefab itself.
Let's go ahead and rename the player GameObject in our scene to Player and drag it into our Prefab folder. As you can see, we've now created a prefab for our player. There's some important things to note now that we've connected this prefab up. First, you'll see that the Player in the Hierarchy view has turned blue. This lets us know that it's connected to a prefab. If we select the Player in the Hierarchy, you'll now see that we have three new buttons in the Inspector for the Prefab options of Select, Revert, and Apply.
Let's take a quick look at how this actually works. Let's start by changing the prefab itself. Here, we'll select the Player from inside of our Prefab folder. This is the base template. Any changes we make here will affect all of the linked GameObjects inside of our scene. Just as an example, let's go ahead and change the Flip property of X. As you can see, we have changed this property from the prefab in our folder, but our scene has automatically updated.
If we go into the Hierarchy and select our Player, you'll see that the Flip X value has also been selected. Let's go ahead and disable that from the GameObject in our scene. If we go and click on the Player prefab, you'll see that it's still selected, and that's because prefabs in our scene don't affect the source prefab. If we wanted to fix this, we can select the Player from our scene and click Apply.
This will save all of the changes from our scene's prefab back to the actual source prefab. Let's go and select the Player prefab, and you'll now see that the Flip X value has been unchecked. This is an important relationship and something that takes some time to understand if you're not familiar with working with prefabs inside of Unity. But it's an incredibly powerful feature that allows you to create templates of all the GameObjects you'll be using in your game and allow you to one, reuse them, without having to set them up from scratch, and two, make custom versions of them in the scene that don't affect other instances of the prefab in different scenes in your game.
Now that we have our player prefab set up, let's go ahead and save our scene.
- Optimizing sprites and draw cells
- Cutting up sprite sheets
- Building animations
- Changing animations
- Creating a player
- Moving the player
- Building a level with a simple map
- Creating interactive objects
- Creating alien enemies
- Switching between scenes