Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How: UnPreFab a PreFab? (Not just break it...)

Discussion in 'Editor & General Support' started by Adam-Buckner, Jul 8, 2011.

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There are times when I have a prefab, and I want to make changes to one and have it NOT be a prefab any more. If I make changes, it becomes a BROKEN PREFAB, but not a clean set of objects. It continues to be a prefab - albeit broken.

    I just want to select a GameObject or family of GameObjects and say "Prang! You are now mortal. You are free of your bonds of immortality and are a prefab no longer."

    Is there a way to just "Clear Prefab"?

    Editor Scripting?

    I would imagine something like "SetPrefabType" to "PrefabType.None" but I can only find "GetPrefabType".

    GetPrefabType
    http://unity3d.com/support/documentation/ScriptReference/EditorUtility.GetPrefabType.html

    PrefabType
    http://unity3d.com/support/documentation/ScriptReference/PrefabType.html
     
    Last edited: Jul 8, 2011
  2. BazLevelUp

    BazLevelUp

    Joined:
    Jun 27, 2012
    Posts:
    36
    any news ? feature would be great !
     
  3. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    656
    Can't be done. I have investigated this already for so long, and it is a no go, for some obscure reason :)
    The only way to make an object a non prefab, is by disconnecting it (either by hand, or by code), and then delete the prefab master from the project assetdatabase. Then it will look normal again.
     
  4. Teku-Studios

    Teku-Studios

    Joined:
    Sep 29, 2012
    Posts:
    257
    There's now a way to do this:

    Select the object in the Hierarchy > Menu > GameOject > Break Prefab Instance
     
  5. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    Here is a code:
    Code (csharp):
    1.  
    2. static public class PrefabReset
    3. {
    4.     [MenuItem("GameObject/Remove Prefab Link")]
    5.     static private void ResetPrefabSelected()
    6.     {
    7.         ResetPrefabParent(Selection.objects);
    8.     }
    9.  
    10.     static public void ResetPrefabParent(params Object[] objs)
    11.     {
    12.         foreach (var obj in EditorUtility.CollectDeepHierarchy(objs))
    13.         {
    14.             if (AssetDatabase.Contains(obj))
    15.                 continue;
    16.  
    17.             ResetPrefabLink(obj);
    18.         }
    19.     }
    20.  
    21.     static private void ResetPrefabLink(Object obj)
    22.     {
    23.         if (obj == null)
    24.             return;
    25.  
    26.         var destSO = new SerializedObject(obj);
    27.  
    28.         destSO.FindProperty("m_PrefabParentObject").objectReferenceValue = null;
    29.         destSO.FindProperty("m_PrefabInternal").objectReferenceValue = null;
    30.         destSO.ApplyModifiedProperties();
    31.     }
    32. }
    33.  
     
    TwistOfFat3 likes this.
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Sadly, the GameObject > Break Prefab Instance does not "unprefab a prefab", but simply makes it a broken prefab.
     
  7. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    But the code above will do so.
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    And, yes, Thanks for the code!

    I've not tried it, but it looks correct.

    Also, we've put in a ticket to review the "Break Prefab" item, to make sure this is the expected behaviour.
     
  9. TheAniMob

    TheAniMob

    Joined:
    Sep 4, 2020
    Posts:
    1
    I had the same problem. I fixed it by right clicking the object you want to un-prefab in the Hierarchy, then select unpack prefab completely. This will make the object no longer a prefab, and you can then just delete the prefab. I've put a link to a short clip of myself doing so if you want to check it out.

    https://drive.google.com/file/d/1MDiWA8d26DFmwJ6Boa01ae3yvx_DfGh-/view?usp=sharing

    hope this helps!

    lol I just realized this thread was created 6 years ago
     
    tiddybear and PraetorBlue like this.
  10. ccrawf35

    ccrawf35

    Joined:
    May 18, 2020
    Posts:
    1
    No no! Thank you. People still need this info, so don't apologize for putting it out, I literally just searched for this!
     
    tiddybear and PraetorBlue like this.
  11. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    656
    I think it is great you put in a solution, even if the solution is something that didn't exist back when this thread was active (Which it didn't)