url | 当缓存中不存在该 AssetBundle 时,用于下载该 AssetBundle 的 URL。必须经过“%”转义处理。 |
version | AssetBundle 的版本。仅当文件以前被下载过并且具有相同的 version 参数时,才会从磁盘缓存加载该文件。通过递增应用程序请求的版本号,您可以强制缓存从 url 下载 AssetBundle 的新副本。 |
hash | 用作 AssetBundle 版本的 Hash128。 |
cachedBundle | 用于将给定版本的资源捆绑包下载到自定义缓存路径的结构。 类似于 UnityWebRequestAssetBundle.GetAssetBundle.</param> 的 cachedAssetBundle 参数。 |
crc | 未压缩内容的 CRC-32 校验和(可选)。如果为非零值,则加载前会将内容与校验和进行比较,如果不匹配则给出错误。使用该方法可避免因下载问题或用户篡改磁盘上的缓存文件而导致的数据损坏。如果 CRC 不匹配,Unity 将尝试重新下载数据,如果服务器上的 CRC 不匹配,则下载失败并返回错误。查看返回的错误字符串,以了解用于 AssetBundle 的正确 CRC 值。 |
WWW 一个 WWW 实例,可在加载/下载操作完成后用于访问数据。
从缓存中加载具有指定版本号的 AssetBundle。如果当前未缓存该 AssetBundle,则自动下载该 AssetBundle 并将其存储在缓存中,以便将来从本地存储检索。
必须使用 LoadFromCacheOrDownload() 代替“new WWW (url)”,以便利用缓存功能。
缓存的资源包仅由文件名和版本唯一标识。缓存过程将忽略 url
中的所有域和路径信息。由于缓存的资源捆绑包由文件名而非完整的 URL 标识,您可以随时更改资源捆绑包的下载目录。这对于推出新版本游戏并确保浏览器或 CDN 不会错误地缓存文件非常有用。
通常情况下,使用 AssetBundle 的文件名生成缓存路径是没有问题的。但如果不同的 AssetBundles 具有相同的最后文件名,则会发生缓存冲突。借助 CachedAssetBundle,用户可以使用 CachedAssetBundle.name 来自定义缓存路径,以避免缓存冲突。用户还可以利用它来组织缓存数据结构。
如果缓存文件夹没有任何空间来缓存其他文件,LoadFromCacheOrDownload 将以迭代方式从缓存中删除最近最少使用的 AssetBundle,直到有足够的空间来存储新的 AssetBundle。如果无法腾出空间(因为硬盘已满,或者缓存中的所有文件当前都处于使用状态),LoadFromCacheOrDownload() 将不使用缓存并将文件流式传输到内存中,就像普通的“new WWW()”调用一样。
根据 Caching.compressionEnabled 值,缓存数据可以以压缩格式存储。
该函数只能用于访问 AssetBundle。无法缓存其他类型或内容。
传递给该函数的 CRC 在 Asset Bundle 构建期间计算,请参阅 BuildPipeline.BuildAssetBundles。
注意: URL 必须经过“%”转义处理。
using UnityEngine; using System.Collections;
public class LoadFromCacheOrDownloadExample : MonoBehaviour { IEnumerator Start() { while (!Caching.ready) yield return null;
using (var www = WWW.LoadFromCacheOrDownload("https://myserver.com/myassetBundle.unity3d", 5)) { yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); yield return null; } var myLoadedAssetBundle = www.assetBundle;
var asset = myLoadedAssetBundle.mainAsset; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.