Search Unity

How to disconnect a prefab & then modify it in a scene

Discussion in 'Prefabs' started by Ylisar, Dec 14, 2018.

  1. Ylisar

    Ylisar

    Joined:
    Jan 27, 2014
    Posts:
    19
    We use the editor API to build in order to select the correct settings & compose the correct game objects depending on platform. This used to work fairly well, but seems to be broken with the new system.


    Before starting the build for a specific scene.

    Aborting the build system just before saving the new scene gives us this, this is actually the result we'd like, with the correct children removed. Inspecting the entirety of WorldmapUIRoot we can see that there's no blue to be seen anywhere. However, if we save this scene & then reload it we get:

    This is obviously wrong. Not only was Main Camera restored somehow, but the prefab for WorldmapUIRoot is now altered. If we don't save the scene after aborting the build at the previous step & then load it again we get:

    Obviously this is also wrong.

    This is how I'm trying to disconnect the prefabs in the new system:
    Code (CSharp):
    1.  
    2.     private static void UnpackIt( UnityEngine.Object a_Obj )
    3.     {
    4.         if (PrefabUtility.IsPartOfPrefabInstance(a_Obj))
    5.         {
    6.             var nearRoot = PrefabUtility.GetNearestPrefabInstanceRoot(a_Obj);
    7.             if (nearRoot.transform.parent != null)
    8.                 UnpackIt(nearRoot.transform.parent.gameObject);
    9.             PrefabUtility.UnpackPrefabInstance(nearRoot, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
    10.         }
    11.     }
    12.  
    After I've unpacked I use GameObject.DestroyImmediate( go,true );. This used to work with a combination of PrefabUtility.DisconnectPrefabInstance & GameObject.DestroyImmediate( go ) in the past, so I'm wondering what the correct way to go about it in 2018.3 is.
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Hi, I am not sure what you are trying to achieve?

    Disconnecting is no longer supported but depending on what you are trying to achieve there are other ways
     
  3. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
  4. Ylisar

    Ylisar

    Joined:
    Jan 27, 2014
    Posts:
    19
    We want to modify a prefab instance & its hierarchy without affecting the source prefab. As such we want to first disconnect it to make sure that the source prefab isn't affected.
     
  5. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Ok, yes, in the case you want PrefabUtility.UnpackPrefabInstance
     
  6. Ylisar

    Ylisar

    Joined:
    Jan 27, 2014
    Posts:
    19
    I'm using that though, check the code in my first post. It seems like it does do something along the lines which I want ( as shown in the first screenshot ). It still seems to mess up the source prefab in some way though, so perhaps it's bugged?
     
  7. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    The code you posted above should not mess with the source prefab unless you pass it the source prefab, but in that case you should be getting an error
     
  8. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
  9. OrderOfOut

    OrderOfOut

    Joined:
    Sep 21, 2018
    Posts:
    37
    I believe this is a bug, I’m having the same issue in Unity 2019.2. I have a fish prefab. I drag an instance to the hierarchy, unpack it completely, change the instance fish sprite, and the prefab sprite still changes along with the completely unpacked instance.

    Edit: the cause of this was having the inspector locked. Seems like inspector lock cancels out prefab unpacking.
     
    Last edited: Sep 2, 2019