Does the EditorSceneManager support opening scenes from AssetBundles during Edit mode? I wasn’t able to find any specific mentions of this in the documentation.
For example, the code below works in executed in play mode, but throws “ArgumentException: Scene file not found.” in Edit mode.
I would appreciate any guidance on this! Thanks in advance.
public class TestClass : MonoBehaviour {
[MenuItem("Menu/Load Scene")]
private static void LoadScene() {
AssetBundle bundle = AssetBundle.LoadFromFile("Bundles/samplescene");
string path = bundle.GetAllScenePaths()[0];
Debug.Log($"Opening {path}");
if (Application.isPlaying) {
// This works!
SceneManager.LoadScene(path);
} else {
// This does not. It throws a "scene not found" error
EditorSceneManager.OpenScene(path);
}
}
}
I’m having exactly the same trouble. The manual only states how to instantiate an object from an AssetBundle. I read on one post that Scenes that are made into AssetBundles cannot be instantiated, they are simply loaded with SceneManager. I cannot find any documentation of how to load a scene from within an AssetBundle.
I hope this is usefull to someone, but I had the same problem and it solved by adding the “.unity” sufix at the end of the scene name. Like this:
for (int i = 0; i < sceneCount; i++)
{
scenes[i] = System.IO.Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
}
foreach (string scene in scenes)
{
Scene loadedScene;
EditorSceneManager.OpenScene("Assets/Scenes/" + scene + ".unity", OpenSceneMode.Additive);
It does seem this is not supported, I tried it in 2023 version of Unity and the error is still occurring. At first it seemed to work, but that is because AssetBundle.GetAllScenePaths() returns the original file path of the scene, and when that scene is still present in the project Unity will load that scene directly, rather than what was inside the AssetBundle. Using just the scene name, with and without extension does not work.
I believe this limitation is expected, because EditorSceneManager.OpenScene will expect a scene that can be actively editor e.g. read-write. The Scene inside an AssetBundle has been processed by the build and is in a read-only state. If I can get a more complete confirmation that this is not supported then we can look to documenting the limitation in the AssetBundle reference.
Thanks @FrankyRD !!
Instead of:
EditorSceneManager.OpenScene(“MySceneName”)
it worked with:
EditorSceneManager.OpenScene(“Assets/Scenes/MySceneName.unity”)
Actually it would be a cool feature to have. It will allow to avoid using UnityDataTools (anyway extracting scenes from bundle in UnityDataTools makes those scenes broken).