Search Unity

How to check if the edited instace is a prefab or How to know we are in prefab editing mode

Discussion in 'Prefabs' started by Jean-Fabre, Nov 15, 2018.

  1. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Trying to battle this one out as EditorUtility.IsPersistent(go) is now obsolete to check if we are dealing with a prefab asset or prefab instance.

    so, within a script attached to a prefab that we are editing using the new prefab workflow, that is within its own scene. How can we know we are editing a prefab? since now, it's actually an instance of the prefab we are editing.

    PrefabUtility.GetPrefabAssetType(go) returns NotAPrefab


    currently the only way I found is by querying the .scene of the gameobject and if the scene name is the same as the gameobject name and the scene path is empty then that must mean this is the prefab source. But that is not very clean.

    Thanks :)

    Jean
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
  3. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Woah! ok, thanks for that, I would have had a hard time figuring all of this...

    Is the UnityEditor.Experimental namespace likely to change when 2018.3 is going to go out of beta?

    Bye,

    Jean
     
  4. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    The Experimental API will not change in 18.3 but will be moved in 19.x
     
  5. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    ok, thanks for you time and infos, have a good day!

    Bye,

    Jean
     
  6. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,338
    Hi, the API still seems to be experimental in 2019.3 (https://docs.unity3d.com/2019.3/Doc...Experimental.SceneManagement.PrefabStage.html).
    Any news on when and where it will finally land?

    I am currently detecting whether the user is in PrafbStage or not with the code below, but it seems a bit risky to put this into production (AssetStore asset) since the API location may change.

    Code (CSharp):
    1. private static bool isInPrefabStage()
    2. {
    3. #if UNITY_2018_3_OR_NEWER
    4.     var stage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
    5.     return stage != null;
    6. #else
    7.     return false;
    8. #endif
    9. }
    Thanks,
    Georg
     
    Last edited: May 28, 2019
  7. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
    Ping :p Knowing this is pretty important for editor tool. I've tried various workarounds to avoid using the experimental PrefabStageUtility but I couldn't find a system that works in 100% of the cases.
     
  8. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,338
    My "resolution" was to just keep using the experimental namespace until it breaks, which has not happened to me yet. It is also in the latest version: 2020.1, so I don't think it will go away soon.

    In my opinion an API which has been existing unchanged for three major releases (2018, 2019, 2020) is no longer experimental. I understand that the reason for not changing it is probably backwards compatibility, but being experimental for so long defeats the purpose of the namespace imo.

    It would be nice to have some kind of newsletter where you can sign up and get notified if something breaks in the namespace/classes which you have signed up for. I know it's a big wish, but that would spare me to check which versions of unity are breaking my code (at least API wise). This could be automated too. Just scan for API changes in releases/docs and send the emails.
     
    Last edited: Apr 9, 2020
    Demigiant likes this.
  9. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    The are a lot of changes to the API in 2020.1; for example PrefabStage inherits from a different class. However, it's mostly backwards compatible. We wanted to get it out of experimental API for 2020.1 but due to issues with the API updater and various build and test infrastructure, it hasn't been possible. It's actively being worked on though. The API, once out of experimental, will be the same as in 2020.1, just moved out of experimental. The API updater should handle almost all cases of this automatically.
     
  10. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,338
    Thanks for the info. So the API updater will attempt to fix it automatically, that's good news.