- Home /
Save Hash128 from Asset Bundle Manifest
I am trying to build an asset bundle loading solution that will work both online and offline. I am able to download asset bundles with the WWW.LoadFromCacheOrDownload function but I'm having trouble loading them when there is no internet access.
First I download the main manifest file from the online location that contains the asset bundles. I then extract the names and Hash128s of the asset bundles from the manifest. I then use these values as the parameters in a WWW.LoadFromCacheOrDownload call to download the asset bundles from the internet. I am then storing these values in a local text file.
The plan is that when there is no internet connection I can load the names and Hash128s from the saved text file and use them as the parameters in a WWW.LoadFromCacheOrDownload call, it will recognise that the asset bundle is already downloaded and load it from the cache.
I have this all set up but the problem is with the Hash128 values. After retrieving them from the manifest I am converting them to a string in order to save them to the text file. When these Hash128s (that have been converted to strings) are read from the text file and parsed back into Hash128s they do not contain the original Hash128 value anymore. This means that the WWW.LoadFromCacheOrDownload function is not looking for the right asset bundle in the cache because the Hash128 that is used as the version is not the same, so it thinks there is a new version to download, which obviously doesn't work when offline.
My question is this: is there a way to save a Hash128 value to a local file without converting it to something else first?
Or is my solution for this way off?
I am using Unity 5.0.2f1.
Thanks for any help!
when there is no internet( if you sure ) get manifest file from cache , and you can get current hash ?
Answer by Peter_Reflection · Jun 08, 2015 at 10:17 AM
I ended up finding the Hash128 function GetHashCode() that is not shown on the scripting API page. This returns an int which I am able to convert to a string and back again without the value changing. I am using this int as the version code in the WWW.LoadFromCacheOrDownload call and it all seems to work fine.
Answer by ntchung · Jul 22, 2015 at 11:06 AM
GetHashCode might be a little risky because of collision, just use Hash128.ToString() and Hash128.Parse().
Save:
Hash128 hash = assetBundleManifest.GetAssetBundleHash(assetBundlePath);
streamWriter.WriteLine(hash.ToString());
Load:
string textLine = streamReader.ReadLine();
Hash128 hash = Hash128.Parse(textLine);