How to use the New Unity Prefab Workflow
The new prefab workflow is here! Rejoice and find out how to make the most of prefab overriding, nesting and variants in Unity’s new prefab workflow. By Gur Raunaq Singh.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to use the New Unity Prefab Workflow
25 mins
One of the most requested features in Unity was having the ability to create nested prefabs. With nested prefabs, you can parent prefabs to each other as individual modules, similar to the concept of inheritance in classes in object-oriented programming.
Since Unity 2018.3 this is finally possible! You can access the new prefab mode editor which lets you edit prefabs in isolation.
Because this new prefab workflow is much faster than the previous way of editing prefabs where you had to drag the prefab into the scene, make changes and then remove it from the scene again each time you want to modify something, you can expect to save hours in development time.
In this tutorial, you’ll learn how to work with prefabs and the new prefab editing workflow, specifically:
- Prefab mode.
- Nested prefabs.
- Prefab variants.
Prerequisites
To load the starter project and work through this tutorial, you’ll need Unity version 2018.3 or newer. If you don’t have it installed, you can download it from unity3d.com or via the Unity Hub.
Once you have everything set up, download the starter project using the Download Materials link at the top or bottom or this tutorial, and open the StarterProject folder with Unity.
Look at the folder structure in the Project window:
Here’s what each folder contains:
- Materials: The necessary materials required for this tutorial.
- Models: The models required for this tutorial.
- Prefabs: All of the prefabs for the tutorial are in this folder.
- Presets: The Furniture Model Importing settings preset file.
- Scenes: The Furniture scene is in this folder.
- Scripts: The scripts for the project.
- Sprites: The sprite for the UI buttons.
- Textures: The main textures for the project.
Overview of Scene
Open the Factory scene in the Assets \ RW \ Scenes \ Factory folder.
In this scene, there’s a small section of a factory where random furniture pieces enter from one spot and are transported to another using a conveyor belt.
You can also see two UI buttons that Increase or Decrease the spawning speed and conveyor belt speed.
Click the Play button to run the scene.
Notice the conveyor belts are moving and the buttons are working, but the objects aren’t spawning.
The piece that gets spawned is randomly selected from a list of predefined GameObjects, which you can check in the Inspector by selecting Furniture from the Hierarchy and looking at the Furniture Prefabs list in the Furniture Spawner component. This component spawns a furniture prefab randomly after a number of seconds based on the value of timeBetweenSpawns.
Now that you have a broad understanding of the scene, it’s time to look at the core ideas the new prefabs workflow brings, and how they’ll make your development life easier.
Creating a Prefab
Prefabs in Unity are pre-configured reusable GameObjects that you create in the scene and store in the project.
To make a prefab, you create the GameObject in its desired configuration in the scene using whatever components you need. You then drag it into the Project window to save it as a prefab in your project. Once created, you can instantiate and clone your prefabs (i.e., create an instance of them at runtime). Prefabs have many uses and are a vital part of development in Unity.
Once created, the text color of your GameObject and its icon turns blue indicating that it’s been converted into a prefab.
Here’s an example from the project:
- Open the Assets \ RW \ Prefabs \ Furniture \ Pieces \ Chair folder. Notice there are a number of variations for the different pieces that make up a chair.
- In the Hierarchy, create an empty GameObject named NewChair.
- Select pieces from the open Project window and drag them onto the newly created, empty GameObject. Now, arrange the pieces to make it look like a chair. When you’re done, you should end up with something like this:
- Add a Box Collider and a Rigidbody component to NewChair so it can sit on top of the conveyor belt in the scene.
- You’ll notice that the green outline that shows the Box Collider might not fit the actual dimensions of the chair exactly. To fix this, with the NewChair GameObject selected, select Tools > Fit collider to children.
- You can see that the collider now fits the boundaries of the chair perfectly. You can check how this is done by checking out the nifty FitColliderToMeshes.cs script in the Assets / RW / Scripts / Editor folder. Feel free to use this script in your own projects!
- At this point, NewChair is just another GameObject that exists in the current scene. To convert it into a prefab, drag and drop it into the Prefabs folder in the Project window.
- The icon color changes to blue in the Hierarchy to indicate that NewChair is now a prefab.
Once you’ve made a prefab, you can delete it from the Hierarchy and use it anywhere within your Unity project.
- Delete the NewChair prefab from the Hierarchy.
- Now select Furniture from the Hierarchy. In the Inspector, you can see that Furniture Prefabs is a public list of the
GameObject
type . - Select the NewChair prefab you just made from the Project window and add it the Furniture Prefabs list. Add a few more already made prefabs (Chair 1, Table 1, etc.) from the Project window for good measure.
Play the scene. Notice that one of the prefabs you just added spawns and moves on the conveyor belt from left to right.
Now that you know how to make a basic prefab, you’re ready to explore the new core features of the prefab workflows.
Prefab Mode
The new prefab mode is a dedicated prefab editing mode that allows you to open and edit prefabs in isolation.
Compared to the older method where you had to bring a prefab into a scene and then edit it, the new prefab mode saves you a lot of time and hassle.
There are two ways to enter prefab mode:
- Double click a prefab in the Project Window.
- If your prefab is part of the current scene, you’ll see a small arrow next to the Gameobject in the Hierarchy. Clicking that also enters prefab mode.
Use either of the methods mentioned above to open the prefab mode within the Scene window. Notice that the Hierarchy only shows the GameObjects that are part of the prefab you selected.
Now exit the prefab mode by clicking the left pointing arrow located to the left of the root GameObject in the Hierarchy. As you can see, it’s pretty easy and fast to jump in and out of prefab mode.