- Home /
How do I "Apply" a prefab if I have access to one of its instances via code?
If I modify something in an instance of my prefab, how can I "Apply" the changes to the prefab? (Just like clicking "Apply" from the editor)
Thanks!
Answer by mindlace · Mar 06, 2012 at 11:30 PM
If you want to recreate the behaviour of the Apply button in an editor script, this works (assuming instance
is your disconnected scene instance)
PrefabUtility.ReplacePrefab(instance, PrefabUtility.GetPrefabParent(instance), ReplacePrefabOptions.ConnectToPrefab);
Thank you very much for providing the actual answer.
Here is a script I made that does it to all children of a game object.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class UnitEditorBulkApply : $$anonymous$$onoBehaviour, ICheckForUnsafeChanges
{
public bool resetPosition = true;
public bool resetScale = true;
public bool resetRotation = true;
#if UNITY_EDITOR
[Context$$anonymous$$enu("Apply Bulk Prefab Apply")]
public void CheckForUnsafeChanges()
{
for ( int i = 0; i < transform.childCount; i++)
{
GameObject go = transform.GetChild( i ).gameObject;
if ( PrefabUtility.GetPrefabParent( go ) != null )
{
Vector3 pos = go.transform.localPosition;
Vector3 scale = go.transform.localScale;
Quaternion rot = go.transform.localRotation;
if ( resetPosition ) go.transform.localPosition = Vector3.zero;
if ( resetScale ) go.transform.localScale = Vector3.one;
if ( resetRotation ) go.transform.localRotation = Quaternion.identity;
{
$$anonymous$$onoBehaviour [] behaviours = go.GetComponents<$$anonymous$$onoBehaviour>();
foreach ( $$anonymous$$onoBehaviour b in behaviours )
if ( b is ICheckForUnsafeChanges )
( b as ICheckForUnsafeChanges ).CheckForUnsafeChanges();
}
Debug.Log( "Applying changes to " + go.name, go );
PrefabUtility.ReplacePrefab( go, PrefabUtility.GetPrefabParent( go ), ReplacePrefabOptions.ConnectToPrefab );
if ( resetPosition ) go.transform.localPosition = pos;
if ( resetScale ) go.transform.localScale = scale;
if ( resetRotation ) go.transform.localRotation = rot;
}
}
}
#endif
}
public interface ICheckForUnsafeChanges {
#if UNITY_EDITOR
void CheckForUnsafeChanges();
#endif
}
Anyone else finding that PrefabUtility.ReplacePrefab breaks all connections to that prefab from other assets?
It definitely breaks them. It started happening with the new Undo system. I've had to abandon the above script because I haven't had the sanity to find a work around or the proper way of doing things.
You have to be really careful with the new Undo system to log everything or else your prefabs will get mangled. You'll find objects floating in the middle of nowhere from time to time.
I also found that this solution broke all references that any other prefab had to the prefab that I was updating using this method. I was able to solve the problem by first instantiating the prefab, then using PrefabUtility.ConnectGameObjectToPrefab() to connect the instantiated game object to the original prefab. Then, I called PrefabUtility.ReplacePrefab() with ReplacePrefabOptionsDefault argument. See example code below.
var go = Instantiate(prefab);
go = PrefabUtility.ConnectGameObjectToPrefab(go, prefab);
// Change the "go" game object as needed.
var updatedPrefab = PrefabUtility.ReplacePrefab(
go,
prefab,
ReplacePrefabOptions.Default
);
Answer by stevesan · May 21, 2014 at 07:04 PM
One caveat to the above answer: If 'instance' is actually a child object inside the prefab, that one line of code will do the wrong thing. You want this instead:
var instanceRoot = PrefabUtility.FindRootGameObjectWithSameParentPrefab(target.gameObject);
var targetPrefab = UnityEditor.PrefabUtility.GetPrefabParent(instanceRoot);
PrefabUtility.ReplacePrefab(
instanceRoot,
targetPrefab,
ReplacePrefabOptions.ConnectToPrefab
);
On a prefab; I had values that was set in the inspector by script but disapeared at play. Your lines resolved my problem. Thanks.
I just happened to be passing by, but for somebody referencing this in the future I shall leave one note here. "to recreate the behaviour of the Apply button" you need ReplacePrefabOptions.ReplaceNameBased.
Answer by Veehmot · Jan 31, 2018 at 08:01 AM
From Unity source:
UnityEngine.Object prefabParent = PrefabUtility.GetPrefabParent(target);
GameObject gameObject = PrefabUtility.FindValidUploadPrefabInstanceRoot(target);
PrefabUtility.ReplacePrefab(gameObject, prefabParent, ReplacePrefabOptions.ConnectToPrefab);