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.
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:
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.
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.
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.
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.
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:
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.
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.
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.
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.
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.
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.