Search Unity

PrefabUtility.IsPartOfPrefabAsset is unreliable.

Discussion in 'Prefabs' started by overki11, Mar 25, 2020.

  1. overki11

    overki11

    Joined:
    Jan 9, 2016
    Posts:
    17
    Title. PrefabUtility.IsPartOfPrefabAsset works fine when you are not editing a prefab, but as soon as you open up the prefab editor scene by double clicking on a prefab, the object magically returns false instead of true, despite it being part of a prefab that is being edited.

    Any workaround?

    Thanks.

    Also: Not sure if this is the right section, might belong in the programming section instead. IDK.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    The name of this API is a bit unfortunate and it's embarrassing the docs for this has not been clarified. I've created a bug case for this now. However, the the way the API works is by design. Prefab contents in Prefab Mode are loaded from a Prefab Asset, and upon saving, saved back to the Prefab Asset. But the contents is regular GameObjects.

    There is (experimental) API to get the Prefab Stage a GameObject might be part of:
    https://docs.unity3d.com/ScriptRefe...gement.PrefabStageUtility.GetPrefabStage.html
    and then check if the object is part of the Prefab Contents in that stage:
    https://docs.unity3d.com/ScriptRefe...ement.PrefabStage.IsPartOfPrefabContents.html

    (Some technical issues have prevented us from moving this out of experimental for 2020.1 but we're working on it. It should be auto-upgraded once it's moved out of experimental.)