Search Unity

Opening the Prefab Stage from an Editor Tool.

Discussion in 'Prefabs' started by JamesA24, Jun 14, 2019.

  1. JamesA24

    JamesA24

    Joined:
    Sep 6, 2018
    Posts:
    28
    Hey there,

    I'm looking for any answers on how to do this if this is available yet.

    I've seen the PrefabStage and PrefabStageUtility scripts posted to the Unity Github and could see there's a LoadStage and OpenStage method. Both are internal however. That means after I create a new prefab within my editor tool, I can't open that prefab within the Prefab Stage right away.

    While this is just what I could see from my findings. I'd like to know if there is an actual way to open the prefab stage from code. And if there isn't, it seems weird to have several functions about going to a previous stage or the main stage but not the prefab stage.

    Sorry if this is in the wrong category of forum. It was either here or the editor forum.
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @JamesAiken

    Do you want to continue editing the prefab from script but have it isolation or do you actually want to force the editor into Prefab Mode?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,335
    Opening a prefab in prefab mode can be done with AssetDatabase.OpenAsset.

    It's not in any way intuitive.
     
    gresolio and _met44 like this.
  4. JamesA24

    JamesA24

    Joined:
    Sep 6, 2018
    Posts:
    28
    @SteenLund I'm trying to force the editor into prefab mode with the prefab I just created.
    @Baste Thank you, i'll give it a shot.
     
  5. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Ok, then what @Baste said is the way to do it
     
  6. bamncan

    bamncan

    Joined:
    Dec 15, 2013
    Posts:
    47
    How does one tell the editor to wait until loading of the Prefab is done, and create additional/new Game Objects to the asset? Any GameObject I seem to create creates before the prefab is loaded and mode is switched.

    For example, my current code stub:
    Code (CSharp):
    1. AssetDatabase.OpenAsset(screen);
    2. Canvas canvasPreview = new GameObject("Canvas");
    Same result with teh following:
    Code (CSharp):
    1. AssetDatabase.OpenAsset(screen);
    2. PrefabUtility.ApplyAddedGameObject(new GameObject("Canvas"), $"Assets/{screen.name}.prefab", InteractionMode.AutomatedAction);
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,335
    fuser likes this.
  8. bamncan

    bamncan

    Joined:
    Dec 15, 2013
    Posts:
    47
    I suppose I could, but since it's an experimental name space I'm not guaranteed to have that in the future, so is there another method?

    My previous code (in summation) was something like this through an EditorWindow where it would instantiate the Prefab and add some game objects to it upon instantiation:

    Code (CSharp):
    1. public class ScreenWindow : EditorWindow {
    2.     public void OnGUI() {
    3.         if (GUILayout.Button("Open") {
    4.             GameObject canvasPreview = new GameObject("Canvas");
    5.             canvasPreview.AddComponent<Canvas>();
    6.             screen = PrefabUtility.InstantiatePrefab(ScreenManager.screen);
    7.             (screen.transform as RectTransform).SetParent(canvasPreview.transform);
    8.         }
    9.     }
    10. }
    Adding a call to the event in the OnGUI obviously ran the code multiple times before the prefab finishes loading in isolation, and even then it seems I can't add/create GameObjects once in the Prefab isolation. So i'm just at a loss atm on how to have a button on an editor window open a prefab, add GameObjects to said prefab, and manipulate them appropriately.

    Sorry, maybe I just need sleep.