Error while downloading Asset Bundle: CRC Mismatch

今天游戏开发中遇到了下面一个报错,原因就是ab的CRC验证不通过。
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.bundle'

出现bug的原因就是本地已经有这个文件,但本地文件与远端文件不一样,当然crc也不一样。加载时候传入的crc是远端文件的crc,但实际加载的时本地的文件。
所以我们要确保:文件内容变化的时候,bundleHash也要变化,也就是说本地缓存的文件的路径也会变化。就可以解决上述bug。

​ 项目用的是Unity Addressable​实现的资源管理,下图是Addressable中调用UnityApi的加载逻辑

UnityApi中

 UnityWebRequestAssetBundle.GetAssetBundle(url, cachedBundle, crc);

这行代码加载assetbundle是根据cachedBundle的bundleName和bundleHash判断本地目录(AppData\LocalLow\Unity\companyname_producename\bundleName\bundleHash)下是否存在相应文件,如果存在就直接从本地加载,如果不存在再去远端下载,下载完会自动存到(AppData\LocalLow\Unity\companyname_producename\bundlename\bundlehash)本地这个目录。

以下是测试代码:

​​​​​​​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);
}

猜你喜欢

转载自blog.csdn.net/PangNanGua/article/details/130852928