Mastering the basics of Unity: Understanding prefabs
When creating games inside of Unity you’ll likely be dealing with numerous different game assets. Whether this is a simple prop like a fence post or even something more complex like an NPC. As your game projects get more advanced you’ll deal with more and more assets that make up your game. A technique utilized in game design that is vital for speeding up both the asset creation process and the level design is to re-use the same asset in various areas of your games. You can see this technique in just about every game whether it’s an indie platformer or a triple-A title. For instance, if you need a series of lamp posts along a street, each lamp post wouldn’t be unique, instead there would be one asset created, and that asset would then be duplicated within the game engine.
Unity allows you to intuitively create these types of duplicates using what is called a prefab. The prefab asset allows you to store an asset with all its properties inside the prefab, and the prefab acts basically as a template that you use to create new instances of the same object in the scene. The reason you would want to do this is because it gives you much more control over the assets that you will need to repeat throughout your game. You certainly could duplicate the asset over and over, however, each duplicate will be independently editable. So if there's ever a large scale change that needs to happen across this asset, each duplicate will have to be independently edited.
Since this is an asset you’ll be repeating many times in your game, you probably don’t want each one to look different. Storing an asset inside a prefab allows any edits that you do to the prefab to be reflected across all instances. Meaning, editing just the prefab will immediately change all the instances of that prefab inside the game. If you ever need to make an independent change to a single asset, you’re still able to by simply overriding the settings for that asset.
Now that you have a basic understanding of the prefab inside of Unity, and some of the areas where it can be useful, let’s quickly go over how you setup a prefab. If you’re new to Unity, be sure to check out Mastering the Basics of Unity: UI article before tackling this one.
There are a couple different ways to create a prefab.
With Unity opened you can go up to Assets>Create>Prefab. You’ll notice that a new icon gets placed within your assets window. You can name it whatever you like. If you select the Prefab you just created, you’ll notice that there's no information being displayed in the inspector. That’s because it’s an empty Prefab. Once you’ve created a new Prefab it’s now ready for you to drop in the assets you want.
You can select the asset in the Hierarchy window and simply drag it onto the new prefab you just created. You should see that the prefab icon has changed to reflect what the asset looks like. If you drag the new prefab directly into the scene view you should see the same asset is now dropped in. This creates an instance of the prefab, if you look in your hierarchy window you should see that a new object has been created and is displayed in blue text, normal objects are shown in black.
If you have an asset in your hierarchy window that you’re ready to use for a prefab all you have to do is simply drag the asset from the hierarchy window into the project assets panel. This automatically creates a prefab for that asset. If your asset is named, the new prefab will reflect that name, and the prefab image will automatically change as well.
Another way you can create a prefab is by right clicking inside the Assets folder of your project panel and going to Create>Prefab. A new empty prefab will be created inside the assets folder. When you’ve created a prefab asset you can delete the asset in your scene and the asset will still be in your prefab.
To edit a prefab you can either select the prefab in the project panel and adjust the properties you want inside the inspector, or you can place an instance of the prefab back into the scene and select that instance. As mentioned earlier, you can make changes to all the assets within your scene at the same time, or you can edit just one of the instances.
If you have several instances of the prefab created in your scene, and you want to make a change to all of the assets, you can select the prefab inside the project panel, and any changes you make there will be reflected through all of the other instances within the scene. You can also select just a single prefab instance in the scene and when you’re happy with the changes you’ve made you can simply hit the apply button inside the inspector to make those changes reflect throughout all the other assets in the scene.
By default any changes that you make to an instanced prefab will not automatically apply to the other prefabs, you’ll need to hit the apply button when you’re ready. If you just want to change a single instanced prefab you don’t have to choose to apply it to the other assets. However, if you edit the prefab inside the project panel those changes will be reflected inside the scene, since that is the main prefab.
When you have an instanced prefab selected in your scene you should notice the three buttons that aren’t visible for a normal object, one of them is the apply button which we talked about, the other two are Select and Revert. The select button will select the main prefab allowing any changes you make to be reflected to all other instances.
The revert button lets you reset the values of the instanced prefab back to the main prefab. So if you begin to make changes to an instanced prefab and you decide you don’t like the changes you made you can select revert to go back to its original settings.
Now that you know more about prefabs and where they can be used you’ll be able to quickly place assets to be used multiple times and easily make large scale changes to them. Prefabs give you the flexibility to make changes that are reflected across all other assets or individual changes when needed. Be sure to visit the Digital-Tutors library more in-depth training in Unity.