解决:Unity : Error while downloading Asset Bundle: Couldn‘t move cache data 问题

目录

问题:

尝试

问题得到解决

我的解释


问题:


         最近游戏要上线,发现一个现象,部分机型在启动的时候闪退或者黑屏,概率是5%左右,通过Bugly只有个别机型才有这个现象,其实真实情况比这严重的多,bugly有时候并不能捕捉到这种情况,我们各种怀疑和查找,最终发现问题的根源。

问题日志如下:

2-18 15:04:10.924 28696 30223 E Unity : Unable to open archive file for writing: '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Temp/0b2da51ac602117b08efce711b40391f/__data'
12-18 15:04:10.924 28696 30144 E Unity : Error while downloading Asset Bundle: Failed to decompress data for the AssetBundle 'jar:file:///data/app/~~mqBnkrTVeHx0R5PQj6Zlhw==/com.xfp.hnsz.ks.xwdxy.cs-ujHBQxE6h_IEHhAMfYKUNA==/base.apk!/assets/assets-art-atlas-achievement.spriteatlasv2_assets_all_fb282f19f9eb9a06452ff40988043469.bundle'.
12-18 15:04:10.927 28696 30148 E Unity : Unable to move cache file '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Temp/1678b268885c708b6b23968c60116b1a' to '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Shared/c145b22e3c49b6b6064231a90d5e684c/48c0472eecd6b114bfc238bd99e509c4': error code - 1
12-18 15:04:10.927 28696 30148 E Unity : Error while downloading Asset Bundle: Couldn't move cache data '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Temp/1678b268885c708b6b23968c60116b1a' into place '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Shared/c145b22e3c49b6b6064231a90d5e684c/48c0472eecd6b114bfc238bd99e509c4' when caching AssetBundle 'jar:file:///data/app/~~mqBnkrTVeHx0R5PQj6Zlhw==/com.xfp.hnsz.ks.xwdxy.cs-ujHBQxE6h_IEHhAMfYKUNA==/base.apk!/assets/icon_fr_eye.png_48c0472eecd6b114bfc238bd99e509c4.bundle'.
12-18 15:04:10.927 28696 30149 E Unity : Unable to move cache file '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Temp/eda9c7b7cbb31ddf2be58ac700e478d6' to '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Shared/c64d4e8d8e84733e1a9638e93f484830/1627a372980ad47ae64c100b39449f60': error code - 1
12-18 15:04:10.927 28696 30149 E Unity : Error while downloading Asset Bundle: Couldn't move cache data '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Temp/eda9c7b7cbb31ddf2be58ac700e478d6' into place '/storage/emulated/0/Android/data/com.xfp.hnsz.ks.xwdxy.cs/files/UnityCache/Shared/c64d4e8d8e84733e1a9638e93f484830/1627a372980ad47ae64c100b39449f60' when caching AssetBundle 'jar:file:///data/app/~~mqBnkrTVeHx0R5PQj6Zlhw==/com.xfp.hnsz.ks.xwdxy.cs-ujHBQxE6h_IEHhAMfYKUNA==/base.apk!/assets/icon_mca_up.png_1627a372980ad47ae64c100b39449f60.bundle'.
12-18 15:04:11.222 28696 30025 E Unity : UnityWebRequestException: Data Processing Error, see Download Handler error
12-18 15:04:11.222 28696 30025 E Unity : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0
12-18 15:04:11.222 28696 30025 E Unity : at Cysharp.Threading.Tasks.UnityAsyncExtensions+UnityWebRequestAsyncOperationConfiguredSource.GetResult (System.Int16 token) [0x00000] in <00000000000000000000000000000000>:0

 通过日志我们发现,在unityActivity一启动的时候,我们就启动下载更新文件(也就是在awake函数中调用UnityWebRequest函数将StreamingAssets的初始包的assetbundle拷贝到可写的沙盒目录下),小概率爆出如上问题。       

尝试

        我们怀疑是unity的storage的存储权限问题,所以我们将安装目录放到 external(SDCard)扩展卡中,默认情况是internal(沙盒目录):

解决:Unity : Error while downloading Asset Bundle: Couldn‘t move cache data 问题_第1张图片

        

并在导出android工程的清单文件(AndroidManifest.xml)下开扩展卡权限:

        READ_EXTERNAL_STORAGE(读取外部存储器 )

        WRITE_EXTERNAL_STORAGE(写外部存储器

但是由于发行方说,游戏已启动就申请扩展卡的存储权限,不合规;也不利于用户安装和导量的转化,最终我们将该办法摒弃掉了。

问题得到解决

从头再来,最终我们在官网发现Caching.ready

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

public class Example : MonoBehaviour
{
    IEnumerator IsCachingReadyExample(string uri)
    {
        //Using this conditional says we want to wait for our Caching system to be ready before trying to download bundles
        while (!Caching.ready)
        {
            yield return null;
        }
        //Download the bundle
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
        yield return request.SendWebRequest();
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);

        //Do something with the AssetBundle...
    }
}

当我们在UnityWebRequest我们的assetbundle之前加上Caching.ready这句的时候,最终问题得到解决。

        其实日志已经告诉我们,在写文件的时候无法将文件写入到缓存中。由于unity对于cache的机制以及什么时候cache.ready准备好了并没有写的很清楚(或许官方写了,我并没有找到出处)

我的解释

        对于cache,unity应该unityActivity一启动的时候需要申请cache,但一定是异步或者在awake之后,而我们的下载更新就在awake第一行就启动下载并是异步的操纵,这时cache有一定概率并没有初始化完成。所有造成有一定概率爆出以上错误的原因。

        如果小伙伴找到cache的详细文档以及cache.ready=true的时机,请告知我,不胜感激!!!

你可能感兴趣的:(工作笔记,bug,unity,unity,游戏引擎)