How do you load more than 1 scene from an asset bundle?

I used BuildStreamedSceneAssetBundle to build a collection of scenes into an asset bundle.

        var scenes : String[] = ["Assets/scene1.unity3d", "Assets/scene2.unity3d"];
	BuildPipeline.BuildStreamedSceneAssetBundle( scenes, "Assets/Data/Scenes.unity3d", BuildTarget.WebPlayer);

I can then use WWW to download the bundle

I can then use Application.LoadLevelAdditive(“scene1”) to load the first scene

But if I try and load any other scene but the first one I get the following error

How do you access the other scenes in the bundle?

Menu: File > Build Settings

How does that help? That’s only for scenes that are already in the project. These are getting loaded on the fly from a server.

why would you want to load more than one scene? is that even possible? practical?

Yes, that’s what Application.LoadLevelAdditive does.

is this just to load them in the background, meaning scripts etc or is this so you can have two or more levels actually playable at once? Can you give me an example?

If Application.lastLevelLoaded == 0 and I do Application.LoadLevelAdditive(1) it will load the assets from scene[1] and add it to my current scene. So my end result is a level with the assets from scenes 0 and 1.

Oh sorry I must have misread.

Thanks.

I haven’t tried to stuff more than one level into an asset bundle before, but what does your level loading code look like?

I only ever get that error you posted if I don’t do this part (copied from a portion of the example script in the docs):

// In order to make the scene available from LoadLevel, we have to load the asset bundle.
// The AssetBundle class also lets you force unload all assets and file storage once it is no longer needed.
var bundle = download.assetBundle;

Even if I do nothing with the bundle variable, it seems you have to do that or the levels won’t load. Though I’m assuming that if you’re able to load the first level, you must already be doing that, in which case I’m not sure what the problem would be. Alternatively, you can make one separate asset bundle for each level and load them that way; it’s a small bit more work (and creates larger overall bundle size because of dependencies being duplicated in each bundle), but it does work.

Yup that’s exactly the code I’m using, straight out the help file.

And yes my next task was to break them into individual bundles. Seems daft to have a function that builds stream asset bundles from multiple scenes but not to allow you to load anything other than the first one.

Just tried breaking this out into a test file to log a bug report and low and behold it works, still no idea why it doesn’t in my production file though so more digging required…

Ok I got it working but it’s one of those, “not really sure why”.
In my production file I took out the hardwired list of scenes to build into the asset and replaced it with this code that I started using in my test scene.

    static function buildSceneList() {
    var tmparr = new Array(EditorBuildSettings.scenes);
    //tmparr.Shift();
    
    var tmpstrarr = new Array(); 
    for (var o in tmparr) {
    	Debug.Log(o.path);
    	tmpstrarr.Add(o.path);
    }
    Debug.Log(tmpstrarr);
    scenes = tmpstrarr.ToBuiltin(String);
}

As far as I can tell it generates exactly the same string, but saves me having to type it in manually, anyway by using that back in the production file it all suddenly started working.

Might want to file a bug report on this. Or at the very least request that the example in the documentation be updated.

Hmm, when you do it that way though, does it work in your standalone? Because if you’ve added the levels to the Build Settings, they’ll work whether you have the asset bundles or not (since now they are in the list of available levels to load). If you uncheck all the levels from your build settings window and try to load them solely by asset bundle, does it work?

@monark - But what you are doing is updating the build settings automatically only?

That is, do the other levels need to be in build as well or you managed to load scenes from an asset bundle?

Thanks

@Giometric - in my situation I was building from one file and loading into another so the scenes did not exist even in the standalone version and therefore did not create an issue.

@ZenithCode - I’m not updating the build settings, I’m creating a string with all the scene names in it from the build settings then passing that to the streamed asset bundle builder function to make the asset bundle. All the scenes you want to build need to be in the build settings and therefore also in the file. I believe EditorBuildSettings.scenes returns a list of all scenes regardless of whether they are checked on or not, so watch out for that too.

Hello thread!

I have recently discovered the same issue in my build. For details, we have Environment, and UI in separate scenes, for workflow: UI guy can edit UI scene and artist can edit environment scene, both on their own machines, and at the same time. Both check in, and they don’t clobber eachother’s work. So that covers the “why”.

But, Just like monark, my main game needs to load the asset bundle, and load both scenes additively, and the two scenes together are “the game”.

I have tried a similar solution to monark, parsing the EditorBuildSettings.scenes, but that’s not working for me (I’m using c#):

    static private string GetBuildableScenePath(string sceneShortName)
    {
        EditorBuildSettingsScene[] buildScenes = EditorBuildSettings.scenes;
        foreach (EditorBuildSettingsScene eachScene in buildScenes)
            if (eachScene.path.IndexOf(sceneShortName) != -1)
                return eachScene.path;
        throw Exception("Scene not in build settings!");
    }

I suspect that the real problem is that the build settings in the first unity project determine an order of scenes. If you put them into the bundle in any other order, it just doesn’t work. I will attempt to verify this, but it is my current working theory.

UPDATE:
I was not correct. I can’t reproduce a solution that works in C# yet. Still investigating.
My esteemed coworker has reproduced monark’s solution, using exactly his steps (in C#). Parse the paths from the EditorBuildSettings.scenes, in-order, and they can be loaded from the bundle after it is built.
I however, was not able to rig the situation by updating EditorBuildSettings.scenes with contents matching my intended set of scenes before calling BuildPipeline.BuildStreamedSceneAssetBundle().

UPDATE AGAIN:
Building each scene in a different bundle works fine. This is not ideal, but it will work since the specific problem with the other option hasn’t been fully identified.