Method CleanBundleCache
CleanBundleCache(IEnumerable<string>)
Removes any AssetBundles that are no longer referenced in the bundle cache. This can occur when a new, updated catalog excludes entries present in an older catalog.
Declaration
public static AsyncOperationHandle<bool> CleanBundleCache(IEnumerable<string> catalogsIds = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<string> | catalogsIds | The ids of catalogs whose bundle cache entries we want to preserve. If null, entries for all currently loaded catalogs will be preserved. |
Returns
Type | Description |
---|---|
AsyncOperationHandle<bool> | The operation handle for the request. Note, that it is user's responsibility to release the returned operation; this can be done before or after the operation completes. |
Remarks
This is used to reduce the disk usage of the app by removing AssetBundles that are not needed.
Note, that only AssetBundles loaded through UnityWebRequest are cached. If you want to purge the entire cache, use Caching.ClearCache instead.
In the Editor, calling CleanBundleCache when not using the "Use Existing Build (requires built groups)" will clear all bundles. No bundles are used by "Use Asset Database (fastest)" or "Simulate Groups (advanced)" catalogs.
See AssetBundle caching for more details.
Examples
Using CleanBundleCache to remove unused bundles from cache:
public void UsingCleanBundleCacheForAllCatalogs()
{
// clear for all currently loaded catalogs
// if catalogIds are provided, only those catalogs are used from the currently loaded
AsyncOperationHandle<bool> cleanBundleCacheHandle = Addressables.CleanBundleCache();
cleanBundleCacheHandle.Completed += op =>
{
// during caching a reference is added to the catalogs.
// release is needed to reduce the reference and allow catalog to be uncached for updating
op.Release();
};
}
The catalogIds parameter is used to set which cached bundles are preserved.Any bundles that are used in other catalogs or old bundles from the specified catalogs are removed.If null, all currently loaded catalogs will be preserved. Note, Catalogs other than the main catalog must be loaded to be used for preserving bundles, see[LoadContentCatalogAsync] for more information. Using CleanBundleCache to remove all unused bundles from cache that are not used in the current main catalog:
public void UsingCleanBundleCacheWithcatalogIds()
{
HashSet<string> catalogsIds = new HashSet<string>();
foreach (var locator in Addressables.ResourceLocators)
{
if (locator.LocatorId == "AddressablesMainContentCatalog")
{
catalogsIds.Add(locator.LocatorId);
break;
}
}
if (catalogsIds.Count == 0)
return;
var cleanBundleCacheHandle = Addressables.CleanBundleCache(catalogsIds);
cleanBundleCacheHandle.Completed += op =>
{
// during caching a reference is added to the catalogs.
// release is needed to reduce the reference and allow catalog to be uncached for updating
op.Release();
};
}