- Home /
Load scene from asset bundle
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?
Answer by pitimoi · Mar 05, 2015 at 02:36 AM
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.
Thanks for your reply! But in my case this won't work (for some reason - on my device - Application.dataPath points to apk ins$$anonymous$$d 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 )
Every 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.
The call to load the scene needs to be just the bare scene name. Try something like:
var async = Scene$$anonymous$$anager.LoadSceneAsync(System.IO.Path.GetFileNameWithoutExtension(scenePath[0]));
Could 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?
i 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.
Answer by junedmmn · Sep 17, 2018 at 03:23 PM
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]));
Your answer
Follow this Question
Related Questions
CharacterCustomization AssetBundles 1 Answer
Android Plugin and Asset Bundles 0 Answers
how to delete specific files from cache? 3 Answers
Which way is better to handle AssetBundle management 2 Answers
3D model is not showing in Android 1 Answer