Search Unity

Open a Prefab in the Editor from an editor script

Discussion in 'Immediate Mode GUI (IMGUI)' started by BenPGSN, Apr 23, 2019.

  1. BenPGSN

    BenPGSN

    Joined:
    Oct 2, 2017
    Posts:
    1
    Hi, I'm hoping someone can help me out with this issue,

    I'm trying to open a prefab from my Editor script so that it's being edited in the Scene window in Unity.
    I have found a way to do this for Scenes by using
    Code (csharp):
    1. EditorSceneManager.OpenScene(scenePath);
    this will open the Scene in the Scene editor window so I can visually edit the scene. I'm trying to do the same thing for prefabs.

    In Unity 2018.3 I double click on a prefab in the project window and it launches the prefab in the Prefab Editing Environment in the Scene window. How can I do this from an editor script?
    I have tried
    Code (csharp):
    1. PrefabUtility.LoadPrefabContents(prefabPath);
    and
    Code (csharp):
    1. Scene scene = EditorSceneManager.NewPreviewScene();
    2. PrefabUtility.LoadPrefabContentsIntoPreviewScene(prefabPath, scene);
    but this seems to silently edit the prefab? I want the prefab to open in the Scene view like it does if I double click it.
    I'm looking for something like OpenPrefab(prefabPath) to edit a prefab from the project window in the Scene window using the Prefab Editing environment but not sure how, or which Utility class to use to do that.

    I am also trying to set a template scene, I think I need to do this by setting the
    EditorSettings.prefabEnvironment


    Any insight into solving this will be greatly appreciated!
     
  2. alex_roboto

    alex_roboto

    Joined:
    Nov 11, 2019
    Posts:
    26
    It seems the answer is to just call this:
    AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath(yourPrefabAssetPath));

    I could not get LoadPrefabContentsIntoPreviewScene to work either.
     
    konstatos likes this.