Search Unity

What’s new for Prefabs in 2022.2?

November 22, 2022 in Engine & platform | 10 min. read
What’s new for Prefabs in 2022.2? | Hero image, version 2
What’s new for Prefabs in 2022.2? | Hero image, version 2

Is this article helpful for you?

Thank you for your feedback!

It’s been a while since the Scene Management team has shared an update on Prefabs. During the last few releases, and after fixing a large number of bugs you’ve reported (thank you!), we’ve made several improvements to the Prefab system. Let’s take a look at each improvement coming in 2022.2 – now available in beta – and how these updates can benefit you.

Replace Prefab Asset for Prefab instance

You can now replace the Prefab Asset for a Prefab instance that exists either in a scene or nested inside other Prefabs. This feature will keep the Prefab instance position, rotation, and scale in the scene, but merge the contents from the new Prefab Asset, all while retaining as many overrides and references as possible via name-based matching (by default). More specifically:

  • The Inspector for a Prefab instance has a new Object Field that can be used for replacing the Prefab Asset.
  • The Hierarchy has Context Menus that can similarly replace the Prefab Asset of the instance.
  • Finally, a plain GameObject can be converted to a Prefab instance through the Context Menu in the Hierarchy, or by dragging and dropping with the Ctrl/Cmd modifier key.
Prefab Asset Object Field
Prefab Asset Object Field
Replace a plain GameObject with a Prefab instance.
Replace a plain GameObject with a Prefab instance.
Replace the Prefab Asset for a Prefab instance.
Replace the Prefab Asset with a Prefab instance.

This functionality is not only available in the UI, but as with most features we build, it has an API that allows you to manage how objects are matched, as well as how Overrides should be treated. See PrefabUtility.ReplacePrefabAssetOfPrefabInstance and PrefabUtility.ConvertToPrefabInstance.

This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.

Replace Prefab instances in 2022.2.

Reorder added GameObjects and components

One of the most requested improvements has been the ability to reorder added GameObjects and components. “Added GameObjects and components” refers to the GameObjects and components that are not part of a Prefab instance, but are added to the Prefab instance in a scene or inside a Variant  or Nested Prefab. So as of 2022.1, it is possible to reorder the added GameObjects by drag and drop – both among themselves and between GameObjects belonging to the Prefab instance. Getting this feature ready has required a major refactoring of the Undo system.

If you want to reorder added GameObjects from an Editor script, it is simply a matter of setting the sibling index on the Transform of the added GameObject. The ability to reorder added components in the Inspector is included in 2022.2. There is no public API for reordering components.

Delete GameObjects from Prefab instances

The last thing we needed to achieve full feature parity between GameObjects and components was the ability to delete GameObjects from Prefab instances as an Override. Deleting GameObjects as Overrides, an option available in 2022.2, ensures that once you’ve deleted a GameObject, the usual workflows for reverting from/applying to a Prefab Asset works as you’d expect.

When it comes to an Editor script, use Object.DestroyImmediate to destroy Prefab instance objects and record the destruction as an Override stored in the scene file.

Reordering added GameObjects and components

Inspect Prefab Variant relationships

Users often ask what the Variant inheritance tree looks like for a specific Prefab Asset. In 2022.2, we added the Prefab Family pop-up to the Inspector. The content of the pop-up is dependent on the selected Prefab Asset in the Project Browser. After selecting a Prefab Asset and opening the Prefab Family pop-up, the Editor lists all the ancestors of the current Prefab, as well as all the immediate children.

Prefab Family pop-up
Prefab Family pop-up
Prefab Variants overview

Explore improved Override tooling

In addition to queries about the inheritance tree, users have often asked how they can get rid of unused Overrides stored in a scene but never accessed (because the property has been removed from a script). In the worst case, such properties might reference assets that are then pulled into the final build, taking up space on the storage device and in memory – but never used.

Overrides are now flagged as unused for:

  • Null target objects
  • Unknown Property Paths (which are not subject to scripted FormerlySerializedAsAttribute usage)
  • Removed components
  • Removed GameObjects
  • Changed array dimensions (e.g., materials array) 

When selecting one or more Prefab instances in the Hierarchy and opening the Overrides drop-down, the Editor now shows whether there are unused Overrides. You can then remove them from the scene using the new Unused Overrides drop-down.

Unused Overrides drop-down
Unused Overrides drop-down

Moreover, you can remove all unused Overrides in a scene through the Hierarchy’s Scene Context Menu or via the Context Menu for an arbitrary selection of Prefab instances.

Remove unused Overrides
Remove unused Overrides.

We do not automatically remove unused Overrides. After all, the reason for their existence cannot be inferred. Removing a property from a script or deleting an asset should not automatically remove unused Overrides as you might subsequently wish to undo the removal and have the Overrides restored.

In case you’re wondering: “Why do I still have Overrides on my Prefab instance after pressing ‘Apply All’?” The answer is that those Overrides simply can’t be applied to the Prefab Asset. Most commonly, such Overrides are references to other objects in the scene that cannot be referenced from the Prefab Asset. Overrides that are not typically applicable are now highlighted by a dark blue bar in the Inspector. These cannot be applied; only reverted.

Remove unused Overrides overview

Get to know these other improvements

You can now change the default behavior when opening Prefab Mode to In Isolation instead of In Context. Go to Editor Preferences > General > Default Prefab Mode to make this change.

Default Prefab Mode in Editor Preferences
Default Prefab Mode in Editor Preferences

Now, with 2022.2, Undo is recorded as a single Undo operation when exiting Prefab Mode. This results in all changes made to the Prefab being reverted if you perform an Undo after leaving Prefab Mode.

Over the course of multiple releases, the error handling and reporting during scene load (and Prefab load in Prefab Mode) has substantially improved, and will now indicate which Prefabs the errors are related to and/or the GUID for missing Prefabs. In fact, the way we handle missing Prefabs’ assets during scene loading is safer and more stable than before.

In an effort to further improve error handling and avoid introducing bad data into your project, we’ve added a Broken Prefab Asset Type, which will be produced by the Prefab Importer when errors that cannot be rectified are encountered.

The most common case is when a Prefab Variant has lost its parent Prefab, perhaps because it was deleted. In this case, we can’t produce a meaningful Prefab Variant, so a Broken Prefab Asset is created instead. This new asset will show information about what is wrong in the Inspector when selected in the Project Browser. If it’s a case of a missing Prefab parent, then the GUID of the missing Prefab is shown. Alternatively, if it’s a chain of Prefab Variants that is broken, you can go up the chain through the Inspector until you find the Variant with the missing parent.

Prefab Variant Asset showing up as Broken Prefab due to lost parent Prefab
Prefab Variant Asset showing up as Broken Prefab due to lost parent Prefab

The concept of Disconnected Prefab instances no longer exists as of 2022.1. We still support loading Disconnected Prefab instances, but when the Editor encounters them during scene loading, the Disconnected Prefab instances are stripped of all their Prefab information and become regular GameObjects.

As mentioned, our team has fixed a series of bugs you’ve graciously reported to us over time. Some of them derive from the original Prefab system, but many have only become apparent upon the introduction of our improved Prefabs.

Today, we are confident you will enjoy the stability of the latest Prefab system. We hope you will find it smooth and efficient to work with.

Have more Prefab-related questions or comments? Join us in the forums to share your feedback.

November 22, 2022 in Engine & platform | 10 min. read

Is this article helpful for you?

Thank you for your feedback!

Related Posts