WWW.LoadFromCacheOrDownload 加载自缓存或下载

static function LoadFromCacheOrDownload (url : String, version : int) : WWW



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



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.


LoadFromCacheOrDownload() must be used in place of "new WWW (url)" in order to utilize caching functionality. 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. 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. This function can only be used to access AssetBundles. No other types or content are cacheable.

LoadFromCacheOrDownload()取代"new WWW (url)"为了充分利用缓存功能。缓存的资源包完全由文件名和版本号唯一标识;URL所有域名和路径信息在缓存被忽略。由于缓存的资源包通过文件名而不是完整的URL来识别,在任何时候资源包被下载,你可以更改目录。对于推出了游戏的新版本确保文件通过浏览器或CDN不是错误的缓存,这是很有用的。对于网络播放器应用程序使用共享缓存,缓存增加了唯一标识信息为了防止应用程序间同名的资源包名称冲突。如果缓存文件夹中没有任何空间来缓存额外的文件,LoadFromCacheOrDownload会反复从缓存中删除最近最少使用资源包,直到有足够的空间可用来存储新的资源包。如果利用空间是不可能的(因为硬盘已满,或所有缓存文件在使用),LoadFromCacheOrDownload()将绕过缓存并流传输文件到内存,就像一个普通的"new WWW()"调用。这个函数只能访问资源包。无其他类型或内容可缓存。

参见: BuildPipeline.BuildAssetBundle, BuildPipeline.BuildStreamedSceneAssetBundle.

function Start ()
    var www = WWW.LoadFromCacheOrDownload ("http://myserver.com/myassetBundle.unity3d", 5);

    yield www;

    if (www.error != null)
        Debug.Log (www.error);
    var myLoadedAssetBundle = www.assetBundle;

    var asset = myLoadedAssetBundle.mainAsset;
Page last updated: 2012-4-7