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.

Leave a rating/review
Download materials
Save for later
You are currently viewing page 2 of 3 of this article. Click here to view the first page.

Auto Saving in Prefab Mode

Back in the scene, select Furniture in the Hierarchy, and clear all items in the Furniture Prefabs list from the Inspector window by setting its Size to 0.

Drag a Table 1 prefab from Assets \ RW \ Prefabs \ Furniture onto the list to add it.

Play the scene. Notice that only instances of Table 1 are spawning onto the conveyor belt in the Game window now.

With the scene still running, double-click Table 1 from the Project window to open it in prefab mode. Now, increase or decrease the scale of the object while the scene is running.

You’ll notice that as you adjust the properties of the prefab, the changes are reflected in real-time with each new spawned GameObject in the Game window.

These live updates are possible thanks to the new Auto Save feature available within the new prefab mode; you can toggle this feature on or off as per your requirement with the checkbox at the top of the Scene view.

Overriding Prefabs

In addition to the convenient Auto Save feature, the new prefab workflow allows you to override prefabs.
A prefab instance can have overrides, which are modifications that are unique to that specific prefab instance and are not part of its corresponding prefab asset.

With property overrides, it’s possible to create prefabs that share common characteristics yet also differ in specific ways in specific contexts.

In other words, overrides allow you to change the properties of a prefab instance and preserve those changes while allowing unchanged properties to be updated and kept in sync with changes in the prefab asset.

Here’s an example of how you can override prefabs:

  • Create an instance of the Table 1 prefab from Assets \ RW \ Prefabs \ Furniture by dragging it from the Project window to the Hierarchy. Click on the arrow next to it to open it in the Prefab editor and toggle the Auto Save option off.
  • Select the child GameObject TableTop. In the Inspector, expand the Material options and change the Shader Material color to Green.
  • You’ve just created a prefab override of the Table 1 asset. With the tabletop piece selected, look at the Inspector window. You’ll notice a little blue line before the name of the property that’s been modified, in this case, the Materials property. When a property changes, the text is bold and a blue vertical line appears beside it in the Inspector, indicating a property override. It also makes it easy to revert specific changes that you’ve made without having to undo all the way back.
  • This change is now local to the prefab instance and all changes to prefab instances can be managed via the Overrides dropdown (shown in the figure below). Click it to see the list of the properties that have been modified.
  • Clicking on a GameObject or a component in the list opens up the Comparison Window, which shows the values from the prefab asset on the left, and the overridden values on the prefab instance on the right.
  • Within this dropdown you can review, apply or revert changes made to a prefab. Or you can click on Apply All to save all of the changes. For now, do not apply any changes.
  • Now, navigate to Assets \ RW \ Prefabs \ Furniture, select Table 1 and double-click to open it in prefab mode.
  • Next, select Tabletop and change the color of the material to blue. You’ll notice that the color of Tabletop currently in the scene doesn’t change, because you have overridden the property in the instance.
  • However, if you change the Scale property of Tabletop asset — the one currently open in prefab editing mode — and click Save, the changes will also get reflected in the instance of the prefab instantiated in the scene.

That’s an example of how you can override prefabs in your scene.

Whenever you want to have an instance of a GameObject in your scene, and you want to make changes to some very specific properties of a prefab, you can do so without breaking the entire prefab instance in the scene.

Before moving on to the next section, delete Table 1 from the Hierarchy.

Nested Prefabs

Nested prefabs allow you to include prefab instances as children inside of other prefabs while still maintaining a connection to their own prefab asset.
They’re are super useful in game development workflows as they allow you to make your game more modular.

Here’s how to nest prefabs within each other:

  1. Navigate to Assets \ RW \ Prefabs \ Furniture in the Project window and open Table 1 in prefab mode like you did earlier.
  2. In the same folder you’ll also see a TableCloth prefab. Drag and drop the TableCloth prefab into the Hierarchy as a child of the main Table 1 prefab. Adjust the X Rotation value to 90, and set the Y Position value so that the Tablecloth sits just above the tabletop. Click Save to save the prefab. You should now have something like this:
  3. Open Table 2 in prefab mode, add the TableCloth prefab as a child and change its Transform values to position it nicely on top of the table. Save this one too.
  4. Create an instance of both Table 1 and Table 2 by bringing them into the current scene by dragging and dropping them into the Hierarchy. Adjust their Transform value so they’re properly visible in the Game window.
  5. Finally, open TableCloth in prefab editing mode by double-clicking it in the Project window. Now, increase or decrease its Scale value.

If the Auto Save is toggled on, you’ll notice that the scale of the table cloth on both Table 1 and Table 2, which are separate prefabs, changes. This is because both of them inherit the same TableCloth prefab.

Back in the Hierarchy, delete all prefabs (Table 1, Table 2, and TableCloth GameObjects) from the scene to keep the project nice and clean.

Here’s another example:

  1. Navigate to Assets \ RW \ Prefabs \ UI. You’ll see that there are IncreaseButton and DecreaseButton prefabs; these are UI buttons that are used in the scene.
  2. Open IncreaseButton in prefab mode, and drag and drop the Assets \ RW \ Prefabs \ Arrow prefab as a child and change it’s Position to (X:150, Y:0, Z:0).
  3. Do the same with DecreaseButton, but change the Position to (X:-130, Y:0, Z:0) and the Z Rotation value of the arrow to 180.
  4. Finally, open the Arrow prefab in prefab mode, then increase it’s Scale to (X:0.75, Y:0.75, Z:1.0) (or anything larger than it’s current scale on X and Y). As expected, the scale of the arrow in both IncreaseButton and DecreaseButton prefab instances is reflected instantly.

Delete the unnecessary Arrow prefab instances from the Hierarchy to keep the project clean.

That was a quick overview of how nested prefabs work in the new Unity prefab workflow.
Before this update, if a single prefab was part of a large number of GameObjects, you would have to manually select all of those instances of the prefab for the changes to reflect.

The ability to create nested prefabs allows you to make individual modules, which can be added as children of other prefabs, and only the main prefab would have to be edited once for the changes to reflect all over the project.