Search code examples
c#unity-game-enginehashassetbundle

What is Hash128 and its use in unity?


WWW.LoadFromCacheOrDownload(string url, Hash128 hash) uses hash128 but there is no proper documentation regarding hash, how to use this and why this is important?

Is it related to securing the url in above function or something else?


Solution

  • The whole point is to make (reasonably) sure that you're not caching a different version of the same file.

    When you first call LoadFromCacheOrDownload, it will look into the cache, and see that the URL you're requesting hasn't been downloaded before. When you call it the second time, unless the cache has been cleared in the meantime, you're going to avoid the download and go directly to cache.

    When you release a new version of your game, the file is still (possibly) in cache. If the file changed in the meantime, you need to ensure that it will be downloaded again - that's what version is for; every time you update a resource, just increment the version.

    Finally, crc (or hash) is a way to ensure that the file in the cache is not corrupt. For example, the user might have manually changed the file, or the file might have been corrupted by a disk error (more common than you might think). Unity will see that the file doesn't match the hash and redownload. It's not hard to compute a CRC-32 hash, and you'll find plenty of tools for doing this for you.