Version: 2022.3
LanguageEnglish
  • C#

PrefabUtility.MergePrefabInstance

Suggest a change

Success!

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.

Close

Submission failed

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

Close

Cancel

Declaration

public static void MergePrefabInstance(GameObject instanceRoot);

Parameters

instanceRoot Root of Prefab instance to update.

Description

Forces a Prefab instance to merge with changes from the Prefab Asset.

Unity will in most cases handle re-merging of the Prefab instance when you make changes in the Editor or via scripting. However, as shown in the example below, there are some rare cases where editing a Prefab instance from script requires you to force merge the instance to get immediate updates.

If you do not call MergePrefabInstance in those rare cases the re-merge will happen automatically at the end of the current frame, but if you need the changes reflected immediately in your script you have to force the merge.

using UnityEngine;
using UnityEditor;

// This example shows how to use PrefabUtility.MergePrefabInstance to force update an instance after some changes

public class SuppressedComponentExample { public static void MergePrefabInstanceExample() { var instance = new GameObject("MyPrefabInstance"); const string path = "Assets/MyPrefab.prefab"; var prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(instance, path, InteractionMode.AutomatedAction);

// Add Component to instance var component = instance.AddComponent<Rigidbody>();

// Add same type of component to the Prefab asset using (var scope = new PrefabUtility.EditPrefabContentsScope(path)) { scope.prefabContentsRoot.AddComponent<Rigidbody>(); }

// Because a Rigidbody already exists on the Prefab instance the one from the asset will be suppressed // we can get the suppressor and verify this var suppressor = instance.GetComponent<Rigidbody>(); Debug.Log($"Is component part of the Prefab instance: {PrefabUtility.IsPartOfPrefabInstance(suppressor)}");

// Destroy the suppressor to make the suppressed object return to glory Undo.DestroyObjectImmediate(suppressor); PrefabUtility.MergePrefabInstance(instance);

// Now we will get the former suppressed component and verify that it is actually part of the prefab var formerSuppressed = instance.GetComponent<Rigidbody>(); Debug.Log($"Is component part of the Prefab instance: {PrefabUtility.IsPartOfPrefabInstance(formerSuppressed)}"); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961