AssetBundle caching
Use Unity's built-in caching system to prevent redundant downloads.
阅读时间2 分钟最后更新于 21 天前
Unity's built-in disk-based caching system prevents redundant downloads by storing AssetBundles downloaded via .
UnityWebRequestAssetBundleUnity converts AssetBundles in the cache to LZ4 for optimal performance. If is , AssetBundles are stored uncompressed. Use the class to manage cached AssetBundles, for example to clear or check cache presence.
Caching.compressionEnabledfalseCachingIf you use the Web platform, refer to Web caching for more information on how to handle caching.
If you use , only those that accept a or argument use the built-in cache.
UnityWebRequestAssetBundle.GetAssetBundleHash128uint versionWhen downloading without caching, Unity downloads the AssetBundle into a memory file then loads it. If the downloaded file is in LZMA format then Unity recompresses it into a memory file in LZ4 format and loads that instead.
Location of the AssetBundle cache
The local AssetBundle cache exists at a root folder (for example, ), with a structure like the following:
Application.temporaryCachePathRootCacheFolder- BundleName1 - Hash1 __data (AssetBundle content) - Hash1__info (cache metadata)- BundleName2 - Hash2 __data - Hash2 __info
During download, Unity uses a temporary directory for the incoming data, and then establishes the recompressed AssetBundle in this structure.
By default, the last component of the download URL is used as the AssetBundle name for the cache. To change the name, you can specify it in the structure, which can be passed to some signatures. Note that the cache is based on the AssetBundle name, not the full URL.
CachedAssetBundle.nameUnityWebRequestAssetBundle.GetAssetBundleCache version hash
The AssetBundle cache uses hash values as the version of an AssetBundle. You can provide a 128-bit hash () or 32-bit value to distinguish versions. This can be a build-generated hash, a numeric version, or a custom value. It's crucial to update this version when a new AssetBundle build is released so devices download the correct new version rather than using a stale cached one.
Hash128uintYou can also use to perform a full rebuild, and to prevent changes to the hash between Unity versions.
BuildAssetBundleOptions.ForceRebuildAssetBundleBuildAssetBundleOptions.AssetBundleStripUnityVersionCache clearing
Unity performs cache clearing automatically on desktop systems. It checks the cache on startup, and clears any AssetBundles that haven't been accessed in in the last 150 days.
Cache.expirationDelayThe platform providers for mobile devices maintain their caches. To explicitly clear the cache on mobile platforms use .
Cache.ClearCache