u3d WWW.LoadFromCacheOrDownload 詳情

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,然後連接起來,以上方式都是隨口說的,其實都不是,只是爲了說明這種唯一對應關係)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章