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~
For now use the AssetDatabase API: Code (CSharp): AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath(yourPrefabAssetPath));
It does what the documentation says: There's even a whole example there of how to use the function: Code (csharp): using UnityEngine; using UnityEditor; public class Example { [MenuItem("Examples/Add BoxCollider to Prefab Asset")] static void AddBoxColliderToPrefab() { // Get the Prefab Asset root GameObject and its asset path. GameObject assetRoot = Selection.activeObject as GameObject; string assetPath = AssetDatabase.GetAssetPath(assetRoot); // Load the contents of the Prefab Asset. GameObject contentsRoot = PrefabUtility.LoadPrefabContents(assetPath); // Modify Prefab contents. contentsRoot.AddComponent<BoxCollider>(); // Save contents back to Prefab Asset and unload contents. PrefabUtility.SaveAsPrefabAsset(contentsRoot, assetPath); PrefabUtility.UnloadPrefabContents(contentsRoot); } [MenuItem("Examples/Add BoxCollider to Prefab Asset", true)] static bool ValidateAddBoxColliderToPrefab() { GameObject go = Selection.activeObject as GameObject; if (go == null) return false; return PrefabUtility.IsPartOfPrefabAsset(go); } }
@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. I should make a full test before asking.
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.