- Home /
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.
Okay, I got a little bit further. The scene gets loaded now, but none of the scripts in it are working.
However, for one part I don't know what I did but I got over the empty scenePath[] somehow. I guess a rebuild of the asset was responsible (with different name).
For another part I had to strip the path and the filename.extension from the scene name:
string[] scenePath = assetBundle.GetAllScenePaths();
string mySceneName = scenePath[0];
mySceneName = mySceneName.Substring(0, mySceneName.Length - 6);
mySceneName = mySceneName.Substring(mySceneName.LastIndexOf("/") + 1);
Application.LoadLevel(mySceneName);
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.
Answer by vishal · Dec 02, 2016 at 06:26 PM
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 . $$anonymous$$y 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 ?
@RLasne In that case you have to call Scene$$anonymous$$anager.CreateScene (Empty container) and LoadScene$$anonymous$$ode.Additive for any further assistance.
Your answer
Follow this Question
Related Questions
Why does a scene loaded from an Asset Bundle returns empty string when getting its name? 0 Answers
'Resource file has already been unloaded' error when exiting application in Editor 1 Answer
How to change scene without the previous scene being reset when you return? 0 Answers
How to export a Scene as Asset-bundle [Through Command-line args] 0 Answers
Scene assets not bundled in asset bundle 0 Answers