Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseprefabInstanceRoot | The Prefab instance root that will have its Prefab Asset replaced. |
prefabAssetRoot | The new Prefab Asset used for the Prefab instance. |
mode | The interaction mode used. |
settings | The settings used to control the details of the replacement. |
Replace the Prefab Asset for a Prefab instance that exists in a Scene or for a nested Prefab instance inside another Prefab.
This function will keep the Prefab instance root position, rotation and scale in the Scene but merge the contents from the new Prefab Asset while, by default, maintaining as many overrides and references as possible by using name based matching. Note that the root GameObject and its components will always reuse these objects regardless of the ObjectMatchMode, so references to these objects will always survive a replacement.
Use ObjectMatchMode.ByHierarchy which will retain references if GameObjects and Components are matched up using their full GameObject hierarchy path so ensure all siblings have unique names.
When using ObjectMatchMode.ByName an object match is only performed if the name match is unique. It is therefore recommended that the Prefab instance and Prefab Asset only have unique names in the hierarchy of GameObjects. Matching cannot be done for GameObjects with duplicate names.
No property overrides are deleted from the serialized state of the Prefab instance so replacing back and forth between different Prefab Assets is a non-destructive action for property overrides. When the final Prefab instance is decided upon then any unused overrides can be cleaned up from either Hierarchy or Overrides window.
Added Components are preserved if a name match exists, otherwise the added Component is deleted. Added GameObjects are preserved by adding them to the root of the new instance if no name match was found for its parent GameObject. Since an added GameObject can have a full hierarchy under it, we leave it to you to decide whether it makes sense to delete the object if it proves redundant on the new instance. This can be done from either the Hierarchy or Overrides window.
For explict control over which overrides should be cleared use the PrefabReplacingSettings.prefabOverridesOptions flags found in the PrefabReplacingSettings.
Additional resources: ReplacePrefabAssetOfPrefabInstances, ConvertToPrefabInstance.
using System.Collections.Generic; using UnityEditor; using UnityEngine;
class PrefabReplacing { [MenuItem("Assets/Replace Scene Selection with Prefab Asset", false, 32)] static void ReplaceSelectedWithPrefabInstance(MenuCommand menuCommand) { GameObject prefabAsset = null; var listOfInstanceRoots = new List<GameObject>(); var listOfPlainGameObjects = new List<GameObject>(); foreach (var go in Selection.gameObjects) { if (AssetDatabase.Contains(go)) prefabAsset = go; else if (PrefabUtility.IsOutermostPrefabInstanceRoot(go)) listOfInstanceRoots.Add(go); else if (!PrefabUtility.IsPartOfNonAssetPrefabInstance(go)) listOfPlainGameObjects.Add(go); }
if (prefabAsset == null || (listOfInstanceRoots.Count == 0 && listOfPlainGameObjects.Count == 0)) { ShowHelpDialog(prefabAsset); return; }
if (listOfInstanceRoots.Count > 0) { var settings = new PrefabReplacingSettings { logInfo = true, objectMatchMode = ObjectMatchMode.ByHierarchy, prefabOverridesOptions = PrefabOverridesOptions.ClearAllNonDefaultOverrides }; PrefabUtility.ReplacePrefabAssetOfPrefabInstances(listOfInstanceRoots.ToArray(), prefabAsset, settings, InteractionMode.UserAction); }
if (listOfPlainGameObjects.Count > 0) { var settings = new ConvertToPrefabInstanceSettings { logInfo = true, objectMatchMode = ObjectMatchMode.ByHierarchy, }; PrefabUtility.ConvertToPrefabInstances(listOfPlainGameObjects.ToArray(), prefabAsset, settings, InteractionMode.UserAction); } }
[MenuItem("Assets/Replace Scene Selection with Prefab Asset", true, 32)] static bool ValidateReplaceSelectedWithPrefabInstance(MenuCommand menuCommand) { foreach (var go in Selection.gameObjects) { if (AssetDatabase.Contains(go)) return true; }
return false; }
static void ShowHelpDialog(GameObject prefabAsset) { var helptext = "Please make a multiselection with at least one Prefab instance root or plain GameObject in the Scene and one Prefab Asset from the Project Browser. \n\nUse Ctrl/Cmd + Click."; EditorUtility.DisplayDialog("Replace Prefab Asset of Prefab instance", (prefabAsset == null ? "Prefab Asset missing.\n\n" : "Prefab instance missing.\n\n") + helptext, "OK"); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.