loadfromcacheordownload是u3d中从网络更新资源的一个比较快捷的方法。
这个方法封装了以下几个事情:
1、网络加载
2、资源解压(加载过来的assetbundle是压缩过的文件)
3、缓存到本地文件(将加载过来的文件解压到内存中,并且将此解压后的内容写一份到本地cache(根据不同平台,自动定位到不同目录)),windows下cache的目录是C:\Users\MUHE(你的用户名)\AppData\LocalLow\Unity\WebPlayer\Cache\DefaultCompany(公司名)__2D v3(项目名) 公司名和项目名都是在Edit->Project Setting->player里设定的。
函数原型:
static WWW LoadFromCacheOrDownload(string url, int version, uint crc = 0);
Parameters
url | The URL to download the AssetBundle from, if it is not present in the cache. Must be '%' escaped. |
version | Version 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 . |
crc | An 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. |
当此处的version不一样的时候,unity并不会直接替换就得,而是会下载一份全新的。
加载同一个文件两次,一次version传1,一次version传2.结果缓存位置有连个文件夹,而且两个文件内容一模一样。
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。
上面这段话说明,文件的替代方式完全取决于 【文件的名字+版本号】(可能是字符串连接起来,然后取个md5,也可能先去两个md5,然后连接起来,以上方式都是随口说的,其实都不是,只是为了说明这种唯一对应关系)。