- Home /
Load Scene in AssetBundle and Unload
I load a scene in my AssetBundle like this:
IEnumerator AsyncLoad ()
{
using (var www = new WWW("file://" + Application.dataPath + "/AssetBundles/scenes"))
{
yield return www;
var bundle = www.assetBundle;
Application.LoadLevel("Scene1");
bundle.Unload(false);
}
}
The problem is sometimes Application.LoadLevel("Scene1"); works fine and sometimes Scene1 is empty. What's wrong here?
Side note: I noticed LoadLevel destroys the current scene before loading, so the lines after LoadLevel are not being executed (since the script was destroyed). As I need to unload the asset bundle in order to use it again another time, what's the solution?
Answer by alexeyzakharov · Jul 20, 2015 at 12:34 PM
Actual problem here is that Application.LoadLevel only guaranties to finish loading level next frame (documentation will be updated). Thus Unload() call might unload data before it is loaded by LoadLevel operation.
The simplest workaround in your case is to use async operation:
var asyncOp = Application.LoadLevelAsync("Scene1");
yield return asyncOp;
bundle.Unload(false);
Also take a look at Vincent's AssetBundle Demo project where AssetBundle loading framework is provided.
Answer by djoshi · Jun 12, 2017 at 06:52 AM
For unloading scene you can make asset bundle static public and then call bundle.Unload(false) in Start() of another scene that's loaded.
Answer by Ekta-Mehta-D · Apr 17, 2015 at 11:31 AM
Hii..
For Loading scene first you need to load all assets from the server.
and for using another time u need to use loadfromcacheordownload.
Have a look at this : Similar problem
Answer by Wothanar · May 10, 2018 at 01:43 AM
its a unity bug, why i cant save a scene as a variable ? why it happend this! is no sense! its so anoying ! unity come one cache system sucks , the scenes cant being unloaded coz they go away , i dont want people load async level it will change my entire system coz just another unity bug? seriously?
//// i use AssetBundle.UnloadAllAssets(); and i record the scenes asset bundles into a variable... if (bundleMaps [0] == null) {
and its null but the damn bundle says that its loaded? whaaaaaaat? and i used the unload stuff on the beginning its a non sense bug! there is no way this error its a damn bug ... coz it happend when who knows what happend its just come from no where some times works fine my code but some times its just stop all the entire update process of assets bundles and my app become freeze and i cant act as a respond of HOW CAN I RESTART THE APP IF THIS ERROR COME ITS THERE ANY WAY FOR THIS? COZ I DONT WANT TO CHANGE MY ENTIRE GAME SYSTEM JUST FOR THIS ERROR THAT COME LIKE FROM THE NO WHERE IN A RANDOM SITUATION ITS JUST SERIOUSLY A DAMN CURSE BUG!.
while (!Caching.ready)
yield return null;
if (bundleMaps [0] == null) {
using (clonewww = WWW.LoadFromCacheOrDownload ("http://www.privatelink.com/mapspack0", MapsPackV [0])) {
showdata = true;
yield return clonewww;
if (clonewww.error != null) {
Debug.Log ("WWW download had an error:" + clonewww.error);
DebuGText.text += "WWW download had an error:" + clonewww.error + "\n";
GameObject tempobj = GameObject.Find ("RADIOPLAYEROBJ");
Destroy (tempobj);
SceneManager.LoadScene (0);
Destroy (transform.gameObject);
}
showdata = false;
totalsizedownloaded += clonewww.bytesDownloaded;
AssetBundle bundle = clonewww.assetBundle;
bundleMaps [0] = bundle;
while (bundle == null) {
yield return null;
}
//AssetBundleRequest assetBundleRequest = bundle.LoadAllAssetsAsync();
displaytext.text = "MAPS PACK 0 Loaded!";
GetAllScenePathsArray = bundle.GetAllScenePaths ();
foreach (string o in GetAllScenePathsArray) {
Debug.Log (o.ToString ());
DebuGText.text += o.ToString () + "\n";
}
foreach (string ma in GetAllScenePathsArray) {
string temps = Path.GetFileNameWithoutExtension (ma);
DLCMAPS.Add (temps);
}
// Unload the AssetBundles compressed contents to conserve memory
//bundle.Unload(false);
//END LOAD MAP
//Go final check here if all its right we display login screen.
}
Your answer