Error while downloading Asset Bundle: CRC Mismatch

The following error was encountered during game development today because the CRC verification of ab failed. 
Error while downloading Asset Bundle: CRC Mismatch. Provided d2fb9a64, calculated f1e8f13f from data. Will not load AssetBundle 'https://www.baidu.com/master/cn/0.0.30/8003915e55faa95c443a5bd31020dcdb.bund 

The reason for the bug in le' is the local There is already this file, but the local file is different from the remote file, and of course the crc is also different. The crc passed in during loading is the crc of the remote file, but the actual loading is the local file. 
So we need to make sure that when the content of the file changes, the bundleHash also changes, which means that the path of the locally cached file will also change. The above bug can be solved.

The project uses the resource management implemented by Unity Addressable. The figure below shows the loading logic of calling UnityApi in Addressable

UnityApi

 UnityWebRequestAssetBundle.GetAssetBundle(url, cachedBundle, crc);

This line of code loads the assetbundle based on the bundleName and bundleHash of the cachedBundle to determine whether there is a corresponding file in the local directory (AppData\LocalLow\Unity\companyname_producename\bundleName\bundleHash). After downloading, it will be automatically saved to (AppData\LocalLow\Unity\companyname_producename\bundlename\bundlehash) local directory.

Here is the test code:

​​​​​​​private static System.Collections.IEnumerator Test()
{
    string url = "https://www.baidu.com/master/cn/0.0.30/8003915e55faa95c443a5bd31020dcdb.bundle";
    uint crc = 3539704420U;
    string bundleName = "bundle_name_aabbccdd";
    string bundleHash = "8003915e55faa95c443a5bd31020dcdb";
    CachedAssetBundle cachedBundle = new CachedAssetBundle(bundleName, Hash128.Parse(bundleHash));
    bool cached = Caching.IsVersionCached(cachedBundle);
    UnityEngine.Debug.Log($"cached={cached}");
    var request = UnityWebRequestAssetBundle.GetAssetBundle(url, cachedBundle, crc);
    request.SendWebRequest();
    while (!request.isDone)
    {
        yield return null;
        UnityEngine.Debug.Log(request.downloadProgress);
    }
    UnityEngine.Debug.Log("error = " + request.error);
}

Guess you like

Origin blog.csdn.net/PangNanGua/article/details/130852928