I have asset bundle with single asset → “Assets/scene.unity”. I try to instantiate my scene on Android with Unity 5:
string[] scenePath = bundle.GetAllScenePaths();
Debug.Log(scenePath[0]); // -> "Assets/scene.unity"
Object myScene = bundle.LoadAsset(scenePath[0]);
if(myScene != null)
Instantiate(myScene);
However, myScene is null. Can you please help?
2 Answers
2
Hello !
Scenes are not used like normal assets inside an Assetbundle and are not Objects that you can instantiate.
The way you have to use a scene is the following :
string[] scenePath = bundle.GetAllScenePaths();
Debug.Log(scenePath[0]); // -> "Assets/scene.unity"
SceneManager.LoadSceneAsync(System.IO.Path.GetFileNameWithoutExtension(scenePath[0]));
In fact, you don’t even have to call GetAllScenePaths() to make it works.
At the moment you use the www.assetbundle getter that give you your “bundle” variable, all scenes in it can be loaded with Application.LoadLevel() with their name.
Edit: updated with comments and new API.
Application.LoadLevel() is obsolete. Using SceneManager.LoadSceneAsync() works. use this:
string[] scenePath = bundle.GetAllScenePaths();
Debug.Log(scenePath[0]);
SceneManager.LoadSceneAsync(System.IO.Path.GetFileNameWithoutExtension(scenePath[0]));
Thanks for your reply! But in my case this won't work (for some reason - on my device - Application.dataPath points to apk instead of obb). So, if I use Application.LoadLevel() it will try to load it form apk. That's why I use asset bundles: read from obb manually, get byte[], construct asset bundle at runtime. Currently I load empty level and then populate it with prefabs. I thought that it could be easier to load level, but now it's clear that it's not possible. You saved me a lot of time! +1000 )
– AlfAtUnityEvery time I attempt your method I get: 'Scene 'Assets/AssetBundles/levelToLoad.unity' (-1) couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.' I have tried to (yield return) LoadAllAssetsAsync, LoadSceneAsync, nothing works. I get the same answer, even after I add that scene to the Build Settings (which I thought that AssetBundles Side stepped this allowing people to add scenes dynamically through DLC after release.
– ninja_gear@ninja_gear The call to load the scene needs to be just the bare scene name. Try something like: var async = SceneManager.LoadSceneAsync(System.IO.Path.GetFileNameWithoutExtension(scenePath[0]));
– ArcanafexCould you load all of the assets of a scene? Terrain, models, etc. as an asset bundle and destroy the current 'level' assets keeping the player?
– MonkeyPuzzlei uploaded my whole game in web as asset bundle , now i just want to load a scene inside scene folder how can i locate and open my scene . And can i delete all my asset from inside the unity ,and the only thing left is assetDownloader script.
– RaviVohra