返回介绍

WWW.LoadFromCacheOrDownload 从缓存或下载加载

发布于 2019-12-18 15:38:47 字数 5601 浏览 1356 评论 0 收藏 0

JavaScript => static function LoadFromCacheOrDownload(url: string, version: int, crc: uint = 0): WWW;
C# => static WWW LoadFromCacheOrDownload(string url, int version, uint crc = 0);

Parameters 参数

urlThe URL to download the AssetBundle from, if it is not present in the cache. Must be '%' escaped.
如果资源包在缓存不存在,那么从URL来下载AssetBundle。必须是'%'转义的
versionVersion of the AssetBundle. The file will only be loaded from the disk cache if it has previously been downloaded with the same version parameter. By incrementing the version number requested by your application, you can force Caching to download a new copy of the AssetBunlde from url.
AssetBundle的版本。如果已下载的带有相同参数的版本,该文件仅从硬盘加载。通过你的应用程序递增版本号,能强制缓存从URL下载一个新的AssetBunlde拷贝。
crcAn optional CRC-32 Checksum of the uncompressed contents. If this is non-zero, then the content will be compared against the checksum before loading it, and give an error if it does not match. You can use this to avoid data corruption from bad downloads or users tampering with the cached files on disk. If the CRC does not match, Unity will try to redownload the data, and if the CRC on the server does not match it will fail with an error. Look at the error string returned to see the correct CRC value to use for an AssetBundle.
可选,未压缩内容的CRC-32校验。如果为非零,则内容会在加载之前比较校验值,如果不匹配给出错误。你可以用这个来避免从下载或用户在硬盘的缓存文件篡改导致数据损坏。如果CRC不匹配,Unity将尝试重新下载数据,如果CRC在服务器不匹配将会报错。查看返回的错误字符串,查看正确的CRC值用于AssetBundle。

Returns 返回

WWW A WWW instance, which can be used to access the data once the load/download operation is completed.

WWW是WWW的实例,一旦加载或下载操作完成,可以用来访问数据。

Description 描述

Loads an AssetBundle with the specified version number from the cache. If the AssetBundle is not currently cached, it will automatically be downloaded and stored in the cache for future retrieval from local storage.

从缓存加载带有指定版本号的AssetBundle。如果AssetBundle不在当前缓存,它将自动下载并储存在缓存,以便以后从本地存储检索。

LoadFromCacheOrDownload() must be used in place of “new WWW (url)” in order to utilize caching functionality.

LoadFromCacheOrDownload()必须用来替代“new WWW (url),为了利用缓存功能。

Cached AssetBundles are uniquely identified solely by the filename and version number; all domain and path information in url is ignored by Caching. Since cached AssetBundles are identified by filename instead of the full URL, you can change the directory from where the asset bundle is downloaded at any time. This is useful for pushing out new versions of the game and ensuring that files are not cached incorrectly by the browser or by a CDN. For WebPlayer applications that use the shared cache, Caching adds unique identifying information to identically-named AssetBundles in order to prevent name collisions between applications.

缓存的AssetBundle通过文件名和版本号唯一标识。URL上的全部域名和路径信息被缓存忽略。由于缓存的资源包由文件名识别而不是完整的URL,在资源包下载之后,你可以在任何时候更改目录。这通常用于游戏推出新版本,确保文件是没有被浏览器或CDN错误的缓存。对于WebPlayer应用程序是使用共享缓存,缓存增加了独特的识别相同的命名资源的信息以防止应用程序之间的命名冲突。

If the cache folder does not have any space for caching additional files, LoadFromCacheOrDownload will iteratively delete the least-recently-used AssetBundles from the Cache until sufficient space is available to store the new AssetBundle. If making space is not possible (because the hard disk is full, or all files in the cache are currently in use), LoadFromCacheOrDownload() will bypass Caching and stream the file into memory like a normal “new WWW()” call.

如果缓存文件夹没有剩余空间来缓存增加的文件,LoadFromCacheOrDownload 将从缓存中迭代删除最近最少使用资源包,直到有足够的空间可用来存储新的资源包。如果剩余空间是不可能的(因为硬盘满了,或当前缓存文件都在使用),LoadFromCacheOrDownload()将绕过缓存并下载文件流到内存就像”new WWW()“ 调用。

This function can only be used to access AssetBundles. No other types or content are cacheable.

这个函数仅用于访问AssetBundles。不会有其他类型或内容被缓存。

Note: URL must be '%' escaped.

注意,URL必须是'%'转义的。

JavaScript:

function Start ()
{
	var www = WWW.LoadFromCacheOrDownload ("http://myserver.com/myassetBundle.unity3d", 5);
 
	yield www;
 
	if (!String.IsNullOrEmpty(www.error)) { 
		Debug.Log (www.error); 
		return; 
	} 
	var myLoadedAssetBundle = www.assetBundle;
 
	var asset = myLoadedAssetBundle.mainAsset; 
} 

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour{
    IEnumerator Start()
    {
        var www = WWW.LoadFromCacheOrDownload("http://myserver.com/myassetBundle.unity3d", 5);
 
        yield return www;
 
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }
        var myLoadedAssetBundle = www.assetBundle;
 
        var asset = myLoadedAssetBundle.mainAsset;
    } 
}

See Also: BuildPipeline.BuildAssetBundle, BuildPipeline.BuildStreamedSceneAssetBundle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文