x


Problem in loading Scene from Asset Bundle...

Hi All,

I am trying to load a scene from an AssetBundle(unity web player)the bundle is also cached.

When i run it for the first time everything runs absolutely perfect and the new scene is loaded, but when i quit the game once and run the build again it gives the error

the level (level name) could not be loaded as it has not been added to the build settings.

Here is the code

Code to Build Asset Bundle

 //Creating Asset Bundles For web Player
 @MenuItem ("Assets/Build AssetBundle Factory Area %u")
 static function CreateSceneBundle1(){
     
     // list of the levels to be streamed 
     var levels : String[] = ["Assets/1-Scenes/AbandonedDay.unity",
                                 "Assets/1-Scenes/AbandonedSunset.unity",
                                  "Assets/1-Scenes/AbandonedNight.unity"];
 
     var path = EditorUtility.SaveFilePanel ("Save Resource", "", "factoryarea", "unity3d");
     
     if (path.Length != 0)
     {
         BuildPipeline.BuildStreamedSceneAssetBundle( levels,path, BuildTarget.WebPlayer);
     }
 }


code to load level from assetbundle

 void Update()
 {
         if(Input.GetButtonDown("Jump"))
         {
             if(CheckBundleCacheStatus(url))
             {
                     Debug.Log("Level already Cached");
                 Application.LoadLevel("AbandonedDay");
             }
             else
             {
                 StartCoroutine(LoadCurrentLevel(url));
             }
         }
     }



 bool CheckBundleCacheStatus(string url)
 {
     if(Caching.IsVersionCached(url,1))
         return true;
     else
             return false;
 }


 IEnumerator LoadCurrentLevel(string url)
 {
     bundleWWW = WWW.LoadFromCacheOrDownload(url,1);
     yield return bundleWWW;
     if (bundleWWW.error != null)
     {
         Debug.Log("WWW download had an error:" + bundleWWW.error);
     }
     else
     {
         AssetBundle myLoadedAssetBundle = bundleWWW.assetBundle;
         myLoadedAssetBundle.LoadAll();
         Debug.Log("Download Completed now load level");
         Application.LoadLevel("AbandonedDay");
     }
 }


can anyone help me with this. Thanks

more ▼

asked Jan 17, 2013 at 08:20 AM

crazy_boy_02 gravatar image

crazy_boy_02
89 68 65 65

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

It looks like the problem here is related to the caching - not how it works, but what it does & doesn't cache.

What it will cache is the bundle itself - you've got that portion right, and can avoid downloading it again. However, just because the bundle is cached, you still have to load the assets from it again!

Fortunately, you've already got both sides of the download covered with the:

 bundleWWW = WWW.LoadFromCacheOrDownload(url,1);


So, ultimately, you can still do the cache check, but your LoadCurrentLevel function should be called regardless of whether the bundle is cached or not.

It's really the

        AssetBundle myLoadedAssetBundle = bundleWWW.assetBundle;
        myLoadedAssetBundle.LoadAll();

portion that's going to make the level available to Application.LoadLevel.

more ▼

answered Jan 17, 2013 at 02:48 PM

joelmgallant gravatar image

joelmgallant
66 4 3 4

sorry but i disagree here, bcoz if i call LoadCurrentLevel regardless of whether the bundle is cached or not, then i will get the error that you cannot download an already cached bundle when i reopen the leven again without quitting the game as in that case the bundle is already loaded.

Jan 17, 2013 at 04:37 PM crazy_boy_02

Right, you'll only want to do the bundle load part at the very beginning (each time the application is launched) - every time you want to load a level after that Application.LoadLevel() should be enough!

Jan 17, 2013 at 04:40 PM joelmgallant

yes i figured that after 5 hours...it's done now..anyways thanks for your reply ...

Jan 17, 2013 at 04:45 PM crazy_boy_02

Glad to hear it! :)

Jan 17, 2013 at 04:50 PM joelmgallant

@ crazy_boy_02: how did you solved this problem? I m having exactly same problem as urs.

Mar 01, 2013 at 07:18 AM hawkeye_01
(comments are locked)
10|3000 characters needed characters left

I was getting the same problem where unity was throwing error "Cannot load cached AssetBundle. A file of the same name is already loaded from another AssetBundle". I was loading asset bundles at different stages of level but when I try to restart the level and try to load the asset bundle again I got the same error stated above. Then I tried saving the asset bundles in a list and trying to clear the list at level restart but that doesn't solved the problem. I even tried Caching.CleanCache() to remove any data from the memory but no use. I was saving the assets bundles on the disk after downloading and loading them again from the disk after level load.

Solution: private IEnumerator LoadAssetFromFile(string _name) { print("Came inside LoadAssetFromFile :" + _name); WWW www= new WWW(string.Concat("file:///", Application.dataPath, "/", _name +".unity3d"));//(m_savePath + _name);
//WWW www= new WWW(string.Concat(Application.dataPath, "/", _name));//(m_savePath + _name);

     yield return www;

     if(www.error == null)
     {
         //m_bundle =  www.assetBundle;
         AssetBundle bundle =  www.assetBundle;
         www.Dispose();
         www = null;
         string path = m_savePath + _name;
         //object [] obj = m_bundle.LoadAll();
         //for(int i=0; i< obj.Length; i++)
         //{
             //print ("Obj :"+ i +" " + obj[i].ToString());
         //}
         bundle.LoadAll();
         print ("AssetBundle is :" + bundle.mainAsset);
         print("============> " + path);
         GameObject _go =  (GameObject)Instantiate(bundle.mainAsset);
         bundle.Unload(false);
     }
     else
     {
         return false;
     }    
 }
  using bundle.Unload(false) after instantiating the gameObject have done the trick.  Not sure wether its mandatory to unload the bundle after you have instantiated it.  But this has solved my problem.

more ▼

answered May 22 at 07:22 AM

Jeet_9811 gravatar image

Jeet_9811
0

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1228
x598
x17

asked: Jan 17, 2013 at 08:20 AM

Seen: 5012 times

Last Updated: May 22 at 07:22 AM