WebGL Assetbundle Load Cache

Source: Internet
Author: User

U3d's Assetbundle really is Bo (pit) Big fine (pit) deep AH

Android words to pack into Streamingassetpath first, after installation, the first run, automatically streaming in the decompression to Persistentdatapath, because the persistent directory was run once before the creation.

This will then be downloaded directly to the persistent directory after updating the resources.

But what about the WebGL project?

There's no such thing as streaming or persistent, because the local path cannot be loaded.

So just cache, load the package with

WWW.LoadFromDownloadOrCache(url, version)

This will download the resources to the cache directory, the version is judged each time it is loaded, and the same is loaded directly from the local disk, otherwise it is downloaded from the network and updates the cache on the local disk.

Here the pit is coming, that's the version number. The first load is OK, arbitrarily set the number, because there is no local, directly downloaded. Later, how to judge the local version and the server version?

This problem, because this version number is loaded when written in the program, such as the first download, the package when the version number is also recorded in the local cache, the next time the load, the program set the version number will be in the local cache compared to the previous recorded version number, if different, it is updated, The download package overwrites the local cache, reloads it, and, if it does not, does not update, loading the local cache directly.

See a lot of examples, are simply write a 0 or 1, even if the server-side resources updated, if the version number is still used and the same as the last time, it is not updated, and will not be downloaded from the network.

So you have to find a way to dynamically get this version of the package, then

WWW.LoadFromDownloadOrCache(url, version)

This overloaded method is not appropriate, because this version did not find a way to get ...
The document says that this version to self-increase, that is, the new version is more than the old only update, but look at the online people say as long as the version number is different, update, whether it is greater or less than, this has not tried, but it does not matter.

At one glance, another overload of this loading method is:

public static WWW LoadFromCacheOrDownload(string url, Hash128 hash, uint crc);

Description: Below is just one of the current assumptions, no practice

First say the last parameter CRC, according to the document, this value is not 0 o'clock compared to the CRC, currently do not care about this, let it is 0
The most important is the second parameter hash
Document Explanation:

hash    Hash128 which is used as the version of the AssetBundle.

This can be comparable to version.
Well, try to get the Hash128 of each assetbundle bag.

From the manifest file, you will have a total manifest file after packaging, with the same name as the root directory of your package.
For example, packaged under Streamingassets, there is a streamingassets.manifest file in its directory

using(WWW www=WWW.LoadFromDownloadOrCache("..../StreamingAssets/StreamingAssets")){        yield return www;        AssetBundle ab=www.assetBundle;        AssetBundleManifest abm=ab.loadAsset<AssetBundleManifest>("AssetBundleManifest");        string[] bundleNames=abm.GetAllAssetBundles();        foreach(string item in bundleNames)        {                Hash128 hash = abm.GetAssetBundleHash(item);        }}

As above, Getallassetbundles () can get the package name of all packages
Then use Getassetbundlehash (package name) to get the packet corresponding to the hash128

Then each time the resource is loaded, the Hash128 of the package is obtained and then used

public static WWW LoadFromCacheOrDownload(string url, Hash128 hash, uint crc);

So each load compares this Hash128 value, which is equivalent to the version.

I tried, in the development environment, if the resources are not modified, repeated packaging, out of this Hash128 is unchanged, if there are changes, such as material color changes, prefab scale changes, such as the scale, and then the package, Hash128 changed.

Theoretically, this loadfromdownloadorcache can determine if there is any update.

So, every time you open a program, first load the total manifest, the package and the corresponding Hash128 to a place, such as a dictionary

Dictionary<string, Hash128> bundleHashDict = new Dictionary<string, Hash128>();using(WWW www=WWW.LoadFromDownloadOrCache("..../StreamingAssets/StreamingAssets")){        yield return www;        AssetBundle ab=www.assetBundle;        AssetBundleManifest abm=ab.loadAsset<AssetBundleManifest>("AssetBundleManifest");        string[] bundleNames=abm.GetAllAssetBundles();        foreach(string item in bundleNames)        {                Hash128 hash = abm.GetAssetBundleHash(item);                bundleHashDict.Add(item, hash);        }}

To load a package called "A.assetbundle"

string path = "...../a.assetbundle";Hash128 hash=bundleHashDict["a.assetbundle"];using(WWW www = WWW.LoadFromDownloadOrCache(path, hash, 0)){        yield return www;        .........}

As above, according to the package name obtained in the dictionary before it gets and stored in the dictionary Hash128, as the second parameter of the Loadfromdownloadorcache method, In theory, the Hash128 of the package that was stored at the time of the last update download (which was written to the cache when this method was called) is compared, and the same is not updated, but different.

Theoretically so, to be practiced

WebGL Assetbundle Load Cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.