How to load a scene from an asset bundle in unity 5?

Loading GameObjects from assetbundles is easy since there is documentation available. But how do I load a scene?

...
		using(www = WWW.LoadFromCacheOrDownload(moduleAddress, 1)) {
			yield return www;
			
			if (string.IsNullOrEmpty(www.error)) {
				AssetBundle assetBundle = www.assetBundle;
				if (assetBundle != null) {
					string[] scenePath = assetBundle.GetAllScenePaths();
					Debug.Log("scenepath: " + scenePath[0]);
					Application.LoadLevel(scenePath[0]);
				}
				else Debug.Log("assetBundle is null");

				assetBundle.Unload(false);
				www.Dispose();
			}
			else Debug.Log(www.error);
		}

This line fails with: IndexOutOfRangeException: Array index is out of range:
Debug.Log("scenepath: " + scenePath[0]);

Do I have to do something else first or is there something wrong with my asset bundle?

I build the asset bundle just like I did it with gameObjects: Select the Scene in the Project view, assigned it to an asset bundle at the bottom of the inspector and then run the Editor script:

	[MenuItem("Assets/Build AssetBundles - Editor")]
	static void BuildAssetBundles_Editor ()
	{
		BuildPipeline.BuildAssetBundles (	"AssetBundles/Editor", 
											BuildAssetBundleOptions.None, 
											BuildTarget.StandaloneWindows);
	}

I just can’t find a decent example on the internet and the documentation does not explain it either. So I’m asking here.

Thank yo so much for positing your answer. It helped me figure out how to load my scene!

I am facing the exact same problem. Can you please elaborate more about your solution. Its really urgent.

1 Answer

1

Don’t forget to add your scene to build setting.

Thank you this seemed to have worked for me but can you explain how this is working ? I just want to make sure this isn't a fluke . My scene is still being loaded from the asset bundle even though it's referenced in the build settings ? It looks like it's working I just want to make sure this isn't too good to be true. haha . Thank you !

What if I want to add new scenes to an already built game ? Is there a way to do this ?