Unity prefabs (short for prefabricated object) allow you to create, store, and modify GameObjects as reusable assets together with all its components, property values, and child GameObjects. Prefabs are easily created by dragging an object from the scene Hierarchy window into the Project window, or they may be imported as part of a Unity asset package. You can read more about prefabs in the Unity Manual at https://docs.unity3d.com/Manual/Prefabs.html.
Let's see how this works by creating a simple reusable model built from simple geometric elements.
Creating and instantiating a prefab
We're going to create a reusable character prefab named BobHead. First, let's build the little guy from a few primitive 3D objects, as follows:
- In the Hierarchy, create an empty GameObject named BobHead (by clicking on +| Create Empty; name it BobHead).
- Set its position to something like Position (3, 0.75, 1.25).
- Create a child Sphere for its head (right-click the BobHead and click 3D Object | Sphere). Name it Head.
- In the ProjectMaterials/ folder, create a new Material, named BobHead Material, and make it a dark blue (for example, #191963).
- Drag the BobHead Material onto the Head.
Now, add the eyes:
- Create a child white-colored eye (right-click the BobHead, then click 3D Object | Sphere) and name it Eye.
- Set its Transform Position (0.15, 0.2, -0.35) and Scale (0.15, 0.25, 0.25).
- Drag the White Material that we created earlier from the ProjectMaterials/ folder onto the Eye.
- Duplicate the eye (select the Eye and press Ctrl + D).
- Change its Transform Position X to -0.15.
The resulting model, including the scene Hierarchy, is shown in the following screenshot:
Now to create a prefab of the object. We'll save it in a project folder named Assets/Prefabs/ by going through the following steps:
- In the Project window, create a new folder named Prefabs (at the root Assets/, right-click, then click Create | Folder and name it Prefabs).
- In the Hierarchy window, click and drag the BobHead GameObject into the Project window's Prefabs/ folder we just created.
Note that a few things have changed in the editor, as shown in the following screenshot:
- The ProjectAssets/Prefabs/ folder contains the BobHead.prefab file, with a preview image.
- Because the asset is presently selected, the Inspector shows its component values and a button to Open Prefab for editing. We'll get to that in a moment.
- The BobHead object name in the Hierarchy is now blue, indicating that it's now a prefab instance.
- There's also a > icon on the BobHead that you can click to edit the prefab asset (the same as the Open Prefab button).
The BobHead prefab is now available as a template for other BobHead GameObjects; you may want to add one or more to your scenes. All instances of the prefab will inherit the properties of the prefab. To add a new instance, just drag it from the Project window into the scene. Let's do that now:
- In the Project window, click the BobHead prefab and drag it into the scene (either the Scene view window or the Hierarchy).
- Once positioned as you like, drop it into the scene.
- Add a few more too.
The following screenshot shows the scene with multiple BobHead instances. In the Hierarchy, the BobHead objects are blue because they reference prefab assets:
You can now modify the instance as you like, such as adjusting its position and scale.
In summary, we have learned the following definitions:
- GameObject: Generally refers to game objects in the Scene and Hierarchy, containing a geometric mesh, material, and other components that define its behavior.
- Asset: Refers to files in the ProjectAssets/ folder that can be added to a scene, including prefab GameObjects, materials, audio clips, and more.
- Prefab asset: A GameObject saved as an asset.
- Prefab instance: A prefab that's been added to a scene is said to be instantiated.
Now that we have a prefab, let's see how to edit it.
Editing and overriding a prefab
Suppose that you now decide that the BobHead needs adjustment; perhaps you want to add a hat. You can modify the prefab and all of the instances will be updated. You could select the BobHead prefab in the ProjectPrefabs/ folder and select Open Prefab to edit it (or just double-click it). Right now, we'll edit it first in the scene Hierarchy, then apply the overrides:
- To add a hat, select one of the BobHead objects in the hierarchy, right-click,and select Create Empty, then rename it Hat.
- Set its Position to (0, 0.5, 0).
- Right-click the Hat and select 3D Object | Cylinderto add a cylinder as a child of Hat— name it HatTop.
- Set itsTransform Scale to (0.5, 0.1, 0.5).
- Drag the Red Material from the Project's Materials/ folder (created earlier in this chapter) onto the HatTop.
- Right-click the Hat again and select 3D Object | Cylinderto add a cylinder as a child—name it Brim.
- Set itsTransformScale to (0.75, 0.01, 0.75) and Position to (0, -0.075, 0).
- Drag the Red Material from the Project's Materials/ folder onto the Brim.
- Adjust the Hat transform to Rotation (0, -15, 25) and Position (-0.2, 0.5, 0).
That looks cool! Presently, only one of the BobHead instances has a hat. Suppose we want each of them to have one too. Select the BobHead object in Hierarchy; note the Overrides dropdown in the Inspector. Click it to show the modifications that you have made relative to the original prefab, as shown in the following screenshot: that'd be the Hat:
Do the following steps to actually apply the overrides:
- Click the Overrides in Inspector.
- Press Apply All.
Voila! Now all the BobHeads in the scene have been updated, as shown in the following screenshot:
Because prefabs may include child objects, it is possible for a child to be another prefab. That's perfectly fine. This is called a nested prefab, and will behave as you expect—for example, we could have made the Hat object its own separate prefab and then inserted it into the BobHead prefab. If you change the original child prefab, then that change will be reflected in the parent prefab.
In addition, modified instances of a prefab can be saved as a separate prefab variant. In that case, the new prefab still references the original prefab so that if the original prefab is changed and said properties have not been overridden by the variant, then the variant (and all its instances) will inherit those changes.
One more thing to point out. When you edit a prefab instance in Unity, you have the option to view it in the Scene window in the context of its position within the scene (Normal), to gray out the surrounding scene (Gray), or hide the context altogether (Hidden). The following image shows a BobHead being editing with a Gray context:
There you have it! Prefabs are a very powerful feature of Unity that we will use throughout this book. Next, we'll look at how to import content from outside of Unity.