Unity3D AssetBundle缓存如何工作?

时间:2017-06-10 18:42:24

标签: unity3d

我有点困惑:

第一次下载资产捆绑后,Unity知道我已经下载了它并直接从缓存(磁盘)第二次加载?

是否使用url映射到本地存储?如果在这种情况下,如果我使用相同的名称在服务器上更新我的assetbundle,那么第二次,它仍将从缓存加载,因为网址没有变化?

示例代码:

UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0);
yield return request.Send();
//only download at the first time, at the second time, it can be loaded from cache
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
GameObject cube = bundle.LoadAsset<GameObject>("Cube");

1 个答案:

答案 0 :(得分:1)

要进行缓存;

而不是打电话:

UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0);

您必须使用版本号调用GetAssetBundle:

UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 1, 0);

请参阅此处的文档: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.GetAssetBundle.html

您也可以通过调用new DownloadHandlerAssetBundle(string url, uint version, uint crc);来实现此目的。请参阅示例代码: https://docs.unity3d.com/ScriptReference/Networking.DownloadHandlerAssetBundle-ctor.html 和文档在这里: https://docs.unity3d.com/ScriptReference/Networking.DownloadHandlerAssetBundle-ctor.html

请注意,当您使用缓存时,request.responseCode将不再是值200(成功),但是当从缓存中检索数据时,它将为0!