Search Unity

EditorSceneManager.OpenScene not working for SceneBundles?

Discussion in 'Asset Bundles' started by roboman5000, Nov 7, 2019.

  1. roboman5000

    roboman5000

    Joined:
    Sep 27, 2016
    Posts:
    19
    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.

    Code (CSharp):
    1. public class TestClass : MonoBehaviour {
    2.     [MenuItem("Menu/Load Scene")]
    3.     private static void LoadScene() {
    4.         AssetBundle bundle = AssetBundle.LoadFromFile("Bundles/samplescene");
    5.    
    6.         string path = bundle.GetAllScenePaths()[0];
    7.         Debug.Log($"Opening {path}");
    8.         if (Application.isPlaying) {
    9.             // This works!
    10.             SceneManager.LoadScene(path);
    11.         } else {
    12.             // This does not. It throws a "scene not found" error
    13.             EditorSceneManager.OpenScene(path);
    14.         }
    15.     }
    16. }
     
    Last edited: Nov 7, 2019
    amcakebread likes this.
  2. amcakebread

    amcakebread

    Joined:
    Nov 8, 2016
    Posts:
    28
    did you ever find a solution to this - I need to do exactly the same thing and I am having exactly the same problem
     
  3. Si2G

    Si2G

    Joined:
    Feb 22, 2020
    Posts:
    6
    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.
     
  4. FrankyRD

    FrankyRD

    Joined:
    Dec 18, 2019
    Posts:
    1
    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:

    Code (CSharp):
    1.  
    2.  for (int i = 0; i < sceneCount; i++)
    3.         {
    4.             scenes[i] = System.IO.Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
    5.         }
    6.  
    7.         foreach (string scene in scenes)
    8.         {
    9.             Scene loadedScene;
    10.  
    11.  
    12.             EditorSceneManager.OpenScene("Assets/Scenes/" + scene + ".unity", OpenSceneMode.Additive);
     
    AndrewSkow likes this.
  5. AndrewSkow

    AndrewSkow

    Unity Technologies

    Joined:
    Nov 17, 2020
    Posts:
    91
    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.