check if assetbundle has already been loaded - SOLVED sortof

Like the title says; is there any way to check if a specific asset bundle has already been loaded?

I keep coming across this exception:

The asset bundle ‘file_name_here.unity3d’ can’t be loaded because another asset bundle with the same files are already loaded
UnityEngine.WWW:get_assetBundle()

We have every level of our game in separate bundles so that we only have short load times between them. The problem is, if a user goes to play the same level twice in a single session the asset bundle is loaded twice and we get the above mentioned exception.

With the way our content is loaded (every level loads the next level’s asset bundle), when I move onto the newly loaded level I no longer have reference to the bundle to keep track of what I have loaded.

All of this could be easily avoided if I could just check to see if that AssetBundle has been loaded before.

I figure if the engine can throw an exception about it, obviously it is keeping track of it somewhere. I’ve scoured the docs but I can’t seem to turn anything up.

Any ideas?


Okay I solved this by creating a playerpref for each level, and when the level wakes I call:

PlayerPrefs.SetInt("level"+(int)LEVELID+"loaded",1);

As I mentioned above, every following level is loaded upon completion of the current level. So during this load time I simply check:

if(PlayerPrefs.GetInt("level"+(int)LEVELID+"loaded") == 0)
{
   DoLoad();
}
else //already downloaded
{
   Application.LoadLevel("LevelName");
}

Of course, since PlayerPrefs are persistent, at the start of your game you will need to reset your data. I found that it worked for me by putting this in my first executing script in my main (first loaded) level.

if(Time.time < 1)
{
  //recursively set all the appropriate PlayerPrefs to 0
}

I use this method because before Time.time reaches 1 the application would have executed the Awake() method I needed to do this but the user could not have loaded anything which might cause breakage later on.

Anyway, that is my solution, however I think giving us some run-time access to whether or not a bundle has been loaded would be greatly beneficial.

Usually, when you refer to stuff that someone else did as a solution to a problem, it’s respectful to link his thread to give him the credit.

No offense if you are the writer of this post, but in that case why haven’t you put your own link? The whole context is there and interesting, so you should link yourself! :slight_smile:

Yeah that’s J’s post but I appreciate you looking out for the authors on the site and I’m sure he’s glad to know his stuff is being protected as well.

Cool, happy to hear that. As a future software engineer, we of course learn that copyrights, intellectual property and credits are really important. So, I didn’t want to offend anyone, but I feel offended when I found the source author which were not linked or mentioned :stuck_out_tongue:

I was running into the same problem and found that the best way to check if an assetBundle has already been loaded is to compare www.bytes md5 sums. It is likely Unity is doing the same thing under the hood when you reference the assetBundle, so before it gets to that the assetBundle loading, just compare it against the list of md5sums (of the other www.bytes) you have loaded. The code for Md5 sums is on the wiki at http://www.unifycommunity.com/wiki/index.php?title=MD5

I do not follow, but that’s why the rule on Q&A sites is to not only offer links, but to bring relevant content into here. Why? Well, your link is now broken and lost. That’s just how Internets works… So, big deal “giving credit” to something or someone that isn’t there!

Now, for more on this subject, if I understand it correctly, all solutions people proposed out there is to keep a friggin’ control list?! Really? Like this? http://answers.unity3d.com/questions/210763/how-do-i-use-the-contains-method-to-see-if-an-arra.html

I hope there is a better way…

3 Likes

If you can do some extra loading and unloading you can just use “www.assetBundle.Unload(false);” after your object is being used in your scene. And the next time you try to load that same asset it will work because it has been unloaded. Works like a charm for me.

This worked perfectly for me! Thanks alot!