When you load an asset bundle it stays in memory so essentially you don’t need to load it again.
You can either save a reference to it, so you can check if you e loaded it already, or you can unload it after you’ve created the assets you need to load.
You may gut also consider keeping a reference to assets you already have instances. So you could clone those rather than having to load the asset bundle again.
All of this is something you have to write code for.
Hi I tried everthing like as myLoadedAssetBundle.Unload(false) after Instantiate but it does’t works.the first time Instantiate does work but then I reload scene (Run LoadFromCacheOrDownload again) received error :
“can’t be loaded because another assetbundle with the same files is already loaded.”
Please for me solutions .Thanks.
You can’t load the same asset bundle again, nor you can have two identically named asset bundles loaded at the same time. If bundle is already loaded - just use it. If you have multiple bundles with the same name, you have to unload the bundle then load a different one.
See documentation for AssetBundle class on how to get loaded bundles and unload them.
@AmobearGame@Aurimas-Cernius I have tried AssetBundle.UnloadAllAssetBundles(true); but not working. Whats the proper way to unload all loaded AssetBundles.
Hi,
I am using “AssetBundle.GetAllLoadedAssetBundles()” to load already loaded AssetBundles.It loads normal AssetBundles but it doesn’t load scene bundles.Any idea??
In my case for Scene Asset Bundles. I replaced my AssetBundle.GetAllLoadedAssetBundles() by a custom List I manage myself.
I put in the list every AssetBundle I’m loading.
and before each Load I check the list to see if the bundle is still present.
The list might contains some ‘null’ items. (I presume they are unloaded AssetBundles)
Make sure to clean up ‘null’ items in the list.
private static Dictionary<string, AssetBundle> loadedAssetBundleMap = new Dictionary<string, AssetBundle>();
public static AssetBundle GetAssetBundle(string assetBundleName)
{
foreach (var kv in loadedAssetBundleMap)
if (kv.Key == assetBundleName)
return kv.Value;
return null;
}
public AssetBundle AssetBundle
{
get
{
AssetBundle bundle = GetAssetBundle(assetBundleName);
if (bundle == null)
{
bundle = AssetBundle.LoadFromMemory(_bytes);
loadedAssetBundleMap[assetBundleName] = bundle;
}
return bundle;
}
}
This still does not seem well handled or explained by Unity after UnityWebRequest has replaced WWW.
This error is thrown out of the blue, it isn’t explained fully or in the manual as far as I can see. The relevant function (DownloadHandlerAssetBundle.GetContent) should return a result with an error code/message, not have the runtime log an error!
Even if you do your best to avoid duplicate bundle loads it seems if the bundle contents are the same - maybe just by names?, but a different file, it can still throw the error (what is the check?). This can be a problem in some art cases.
Either way the error should be detailed and you should be able to handle it - catch it.
I’m in the same boat as @andyz : in my code, I need to be able to handle this error. I’ve tried to wrap it in a try-catch, but that doesn’t do anything. And there doesn’t seem to be any way to query what bundles are currently loaded.
I’m downloading a bunch of asset bundles via concurrent coroutines. After I load the asset needed from the bundle, I immediately call bundle.Unload(false);. However, if I happen to be downloading the same bundle twice, in two different coroutines, I’ll sometimes get this error message. Since coroutines are all happening on the main thread, this means that there is some amount of time that bundles remain persistent in memory after calling bundle.Unload. So there doesn’t seem to be any way to avoid this bug.
Well, that is your problem.
You have to manage things in a way that you don’t load the same asset bundle twice. You can’t do random things in random places and expect consistent behavior.
Right, I’ve already been working on that part. But we should be able to load an asset bundle, unload it, and then load it again. And, really, the bigger issue is that this error is written to the console but produces no exception that we can catch and handle ourselves. As @andyz mentioned:
“This error is thrown out of the blue, it isn’t explained fully or in the manual as far as I can see. The relevant function (DownloadHandlerAssetBundle.GetContent) should return a result with an error code/message, not have the runtime log an error!”