Asset Bundles - WebGL - The AssetBundle can't be loaded because it was not built with the right version or build target.

Hello everyone,

I’ve been stuck on this issue for the past few days.

I build out the AssetBundle with the following tool:
alt text

Here are the build settings.

I then upload the AssetBundle to an AWS bucket.

I use the following code to build the asset bundles:

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string assetBundleDirectory = "Assets/StreamingAssets";
        if (!Directory.Exists(Application.streamingAssetsPath))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
    }
}

I then use the following code to pull the asset bundles:

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

public class BundleWebLoader : MonoBehaviour
{

    public string bundleUrl = "https://aws-bucket-url/AssetBundles/WebGL/resources";

    // Start is called before the first frame update
    IEnumerator Start()
    {
        //DontDestroyOnLoad(this.gameObject);
        UnityWebRequest web = UnityWebRequestAssetBundle.GetAssetBundle(bundleUrl, 0, 0);
        yield return web.SendWebRequest();

        if (web.isNetworkError || web.isHttpError)
        {
            Debug.Log(web.error);
        } else {
            AssetBundle remoteAssetBundle = DownloadHandlerAssetBundle.GetContent(web);// web.assetBundle;
            if (remoteAssetBundle == null)
            {
                Debug.LogError("Failed to download AssetBundle!");
                yield break;
            }

            string[] rootAssetPaths = remoteAssetBundle.GetAllAssetNames();

            MonsterDataManager.resourcesAssets = remoteAssetBundle;
            SceneManager.LoadScene("MainMenu");
        }
    }

}

As you can see in the image and with the code, this is being built out to WebGL.

I thought the issue might be related to the bundle size as I’ve read the max size is 4GB. I ran the bundle uncompressed which output a 3.4GB file (107MB when compressed).

The following code and setup works successfully within the Unity editor. This fails when deployed an Angular app.

201106-assetbundleerror.jpg

I suspect it may come down to this line of code: (URI, version, crc)

UnityWebRequest web = UnityWebRequestAssetBundle.GetAssetBundle(bundleUrl, 0, 0);

When I remove the 2nd and 3rd param this no longer properly works in the Unity editor, however, I no longer get the Asset Bundle version error (though the assets still do not load).

Any help would be greatly appreciated! Using Unity 2021.3.0f1.

So it seems that there are a number of factors.


1: Asset Bundles cannot exceed 4GB.
Compressed the bundle may look small (about 100MB in my case), when exporting uncompressed my bundle was 4.4GB. I broke down the spritesheets into small bundles and I no longer received the error message about “right version or build target”. Sadly Unity does not give an error message for this, you have to figure it out on your own.

2: Passing a version param seems to pull an old outdated cached version of the bundle.

UnityWebRequest web = UnityWebRequestAssetBundle.GetAssetBundle(bundleUrl, 0, 0);

should be

UnityWebRequest web = UnityWebRequestAssetBundle.GetAssetBundle(bundleUrl);

3: At some point, my asset paths changed. Combine this with the max size issue, and an old cached version of the bundle I was really lost.

You can use the following code to check the paths to make sure they are correct.

string[] rootAssetPaths = remoteAssetBundle.GetAllAssetNames();
Debug.Log(rootAssetPaths[0]);

Fixing these 3 issues, along with using crunch compression to reduce the bundle size, resolved the issue for me.