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
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Prefab Variants

Variants allow you to create a new prefab from another prefab asset.

The prefab that the prefab variant is based on is referred to as the base. The prefab variant can contain overrides to the base prefab but still receive updates to properties which are not overridden.

In the previous section about prefab overrides, you learned how to override specific properties of a prefab instance in your scene. However, there might be times where you want to create two or more variants of a prefab which can be accessed throughout the project but differ from the base prefab in a small number of properties, a material for example.

To do that, you can create Variants of prefabs that inherit all of the properties of a base prefab but can be customized in specific select properties.

Currently, the UI buttons IncreaseButton and DecreaseButton are two separate prefabs, but they share a lot of common properties, and creating a variant of one from the other would be easier to maintain.

  1. Select DecreaseButton from the Hierarchy, and delete it. Also, Navigate to Assets \ RW \ Prefabs \ UI and delete the DecreaseButton asset.
  2. Create a variant of the IncreaseButton asset by right-clicking on it (in the Project window) and selecting Create > Prefab Variant option from the context menu. A prefab variant has a unique icon with two arrows indicating that it is a prefab variant. When selected, you can also see the base prefab that it inherits in the Inspector window. Rename the variant to DecreaseButton.
  3. Now, create an Instance of DecreaseButton by dragging and dropping it as a child of Canvas in the Hierarchy. Set its position to (X:-390, Y:144, Z:0)
  4. Open the DecreaseButton prefab in prefab editing mode and toggle the Auto Save option off. Select the child GameObject Text, and set its text to Decrease. Also, select the child GameObject Arrow and set its Z Rotation value to 180 and it’s X Position value to -150.
  5. Click Save in the Scene window to save changes. Now you have DecreaseButton as a prefab variant of IncreaseButton with the Text and Arrow GameObjects varying in some properties.

You can look at the properties that DecreaseButton overrides over the base prefab by clicking the Overrides dropdown from the Inspector window.

Changes that you have not overridden in the variant are still updated as changes are made in the base prefab. To demonstrate this:

  1. Open IncreaseButton in prefab editing mode and change the Text to something random like Increase 123 and click Save, you’ll notice that the changes do not reflect in the DecreaseButton prefab.
  2. However, if you change the Font size of Text in the IncreaseButton prefab to 38, the changes are reflected in the DecreaseButton prefab as well, since the font size property was not overridden in the prefab variant DecreaseButton.

Exit the prefab editing mode and delete any unnecessary DecreaseButton prefabs from the Hierarchy. With the Furniture GameObject selected, add NewChair, Table 2 and Chair 1 prefabs to the Furniture Prefabs list, and click Play.

Where to Go From Here?

Congratulations on making it to the end of this quick overview of the new prefab workflows introduced in Unity 2018.3. If you’ve worked on a prefab-heavy project in the past, I bet you’re dancing right now. :]

You can find the final project using the Download Materials link at the top or bottom or this tutorial.

To solidify what you just learned, play around with the project and try out different combinations of the techniques picked up in this tutorial.
Here’s a final recap of what you learned in this tutorial :

  • The new prefab mode is a dedicated editing mode that allows you to open and edit prefabs in isolation.
  • Nested prefabs allow you to parent prefabs to each other. This helps to increase the modularity of your project
  • Prefab variants allow you to create prefabs that are derived from other prefabs and can inherit specific properties of those prefabs, while also allowing you the ability to override some features of the prefab as per your requirements.

To learn more, you can check out these links :

If you have any questions or comments, or you want to show what you experimented with by using this tutorial, join the discussion below!