Search Unity

NestedPrefab: Internal Hatred

Discussion in 'Prefabs' started by LightStriker, Jan 2, 2019.

  1. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,406
    Code (CSharp):
    1.         public class PrefabStageUtility
    2.         {
    3.             internal static PrefabStage OpenPrefab(string prefabAssetPath, GameObject instanceRoot) { }
    4.         }
    Internal??? WHY?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,863
    Yeah, that's silly. Calling it through reflection does exactly what you'd expect, and it's very necessary for a bunch of our dev tools once we switch over.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,863
    Huh, looking at the code @TJHeuvel-net posted here, it seems like calling AssetDatabase.OpenAsset on a prefab asset opens it in prefab mode?
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    403
    Yes, i saw that was recommended by a Unity employee elsewhere.
     
    Last edited: Jan 3, 2019
  5. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    Hi,

    Yes the recommended way of open a Prefab in Prefab Mode from script is to use
    AssetDatabase.OpenAsset


    Our philosophy is that you should (most of the time) be able to build the same editor tools as us using public API. In this case it is unfortunately not completely obvious which API to use.

    The Stage API is going to change which is why a lot of it is in experimental name space and internal, but there shouldn't really be anything we do related to prefabs that you can't reimplement with the public API
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,863
    The problem here is that it's not very intuitive. If I'm thinking "I want to open one of our assets in the prefab stage", I'm going to look through PrefabUtility and PrefabStageUtility.

    AssetDatabase.OpenAsset is for opening assets in external applications, at least that's what it's documented to do - so it's in no way obvious that it opens them in internal windows if that's the case.
     
    Flavelius and TJHeuvel-net like this.
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,406
    I know it's not the current philosophy, but in my opinion everything in Editor should be public, unless it makes perfect sense for it to be private/internal. Because let's be clear, making it private/internal doesn't stop us from using it. It just makes it a pain in the ass to use it.

    We have project that must move forwards and tool that has to be updated to new version of Unity. Anytime I hit myself on something that I must use and is behind a stupid internal wall, I waste precious time accessing it by reflection. I don't care if it's experimental or bound to change. It's easier to update it if I don't have pages of reflection code to get rid of later.

    In some case I've even used Mono.Cecil to change some of the existing stuff. Which makes me think I should make a tool to turn every internal into public and be done with this absurdity forever.
     
    ortin likes this.
  8. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,558
    We'll get the documentation fixed. AssetDatabase.OpenAsset has been also for opening assets internally for a long time. AnimationClips, AnimatorControllers, and probably other existing assets have opened internal windows for years when used with AssetDatabase.OpenAsset. Prefabs are just the latest addition to follow this pattern.

    In general, AssetDatabase.OpenAsset performs the same action as when you double-click on an asset in the Project Browser in Unity.
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,863
    Thanks for that, it'll help somewhat. Still, people who are looking to open a prefab will have a much easier time to figure out how if you just have a method in PrefabUtility that does that. The new API is hard enough to understand as-is.
     
  10. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,558
    There are obviously different schools of thought on this, but we prefer to not have multiple API-points that do the same thing.

    And if we look a bit beyond just Prefabs, it's a lot easier learn how to open any asset (the equivalent of double-clicking it) with a single API point compared to having countless different API points in different classes that you have to know about for different asset types. Now that you know OpenAsset works for internal asset types too, you can also use that knowledge next time you need to open some other internal asset type, rather than having to guess which feature-specific API to use.
     
    TJHeuvel-net and Baste like this.