Search Unity

Enter Prefab Mode via script

Discussion in 'Prefabs' started by ArmelGibson, Nov 17, 2018.

  1. ArmelGibson

    ArmelGibson

    Joined:
    Jul 4, 2015
    Posts:
    8
    Hello!

    I'm making to tool to help design our game, and I would like to know if there is a way to enter the new Prefab Mode via script in edit mode, and have it showing in the "preview" scene view with the breadcrumbs etc, exactly as if I had double clicked on the prefab from the project window.

    Thanks~
     
  2. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    217
    For now use the AssetDatabase API:

    Code (CSharp):
    1. AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath(yourPrefabAssetPath));
     
  3. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    237
    So what does
    PrefabUtility.LoadPrefabContents
    use for? I called it and nothing happened.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    It does what the documentation says:

    upload_2024-2-27_12-13-20.png

    There's even a whole example there of how to use the function:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class Example
    5. {
    6.    [MenuItem("Examples/Add BoxCollider to Prefab Asset")]
    7.    static void AddBoxColliderToPrefab()
    8.    {
    9.        // Get the Prefab Asset root GameObject and its asset path.
    10.        GameObject assetRoot = Selection.activeObject as GameObject;
    11.        string assetPath = AssetDatabase.GetAssetPath(assetRoot);
    12.  
    13.         // Load the contents of the Prefab Asset.
    14.        GameObject contentsRoot = PrefabUtility.LoadPrefabContents(assetPath);
    15.  
    16.         // Modify Prefab contents.
    17.        contentsRoot.AddComponent<BoxCollider>();
    18.  
    19.         // Save contents back to Prefab Asset and unload contents.
    20.        PrefabUtility.SaveAsPrefabAsset(contentsRoot, assetPath);
    21.        PrefabUtility.UnloadPrefabContents(contentsRoot);
    22.    }
    23.  
    24.     [MenuItem("Examples/Add BoxCollider to Prefab Asset", true)]
    25.    static bool ValidateAddBoxColliderToPrefab()
    26.    {
    27.        GameObject go = Selection.activeObject as GameObject;
    28.        if (go == null)
    29.            return false;
    30.  
    31.         return PrefabUtility.IsPartOfPrefabAsset(go);
    32.    }
    33. }
     
    SolarianZ likes this.
  5. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    237
    @Baste Ok, it's my fault.
    I have read the document and I thought it would open the prefab in prefab mode, but it didn't, so I asked the question.
    upload_2024-2-28_10-17-51.png
    I should make a full test before asking.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    Right!

    So it does actually open the prefab into an isolated scene - just not one that's visible. The consequence of that is that adding things to the prefab or modifying it or whatever will not dirty the current scene.

    It's somewhat a leaked implementation detail - what a scene is internally in Unity is slightly different from what it is to Unity users.