2using System.Collections;
4using System.Reflection;
9 internal static class AssetBundleManagerHelper
11 private static readonly IDictionary _abCacheManifestDic;
12 private static readonly MethodInfo _abCacheUnloadMethod;
14 static AssetBundleManagerHelper()
16 var abmType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypesSafe()).FirstOrDefault(x => x.Name ==
"AssetBundleManager");
20 var mbpProp = abmType.GetProperty(
"ManifestBundlePack", BindingFlags.Static | BindingFlags.Public);
21 _abCacheManifestDic = mbpProp !=
null ? mbpProp.GetValue(
null,
null) as IDictionary :
null;
23 if (_abCacheManifestDic !=
null)
26 _abCacheUnloadMethod = abmType.GetMethod(
"UnloadAssetBundle", BindingFlags.Static | BindingFlags.Public,
null,
new[] { typeof(string), typeof(bool), typeof(string), typeof(bool) },
null);
31 public static void DrawButtonIfAvailable()
33 if (_abCacheUnloadMethod !=
null && GUILayout.Button(
"Clear AssetBundle Cache"))
37 var unloadedCount = ClearAssetBundleCache();
47 private static int ClearAssetBundleCache()
49 var unloadedCount = 0;
51 var manifestDicEnumerator = _abCacheManifestDic.GetEnumerator();
52 while (manifestDicEnumerator.MoveNext())
54 var valueType = manifestDicEnumerator.Value.GetType();
56 var loadedBundlesProp = valueType.GetProperty(
"LoadedAssetBundles", BindingFlags.Instance | BindingFlags.Public);
57 var loadedBundlesDic = loadedBundlesProp !=
null ? loadedBundlesProp.GetValue(manifestDicEnumerator.Value,
null) as IDictionary :
null;
59 if (loadedBundlesDic ==
null)
throw new InvalidOperationException(
"Failed to get LoadedAssetBundles dictionary");
62 foreach (var labsKey
in loadedBundlesDic.Keys.Cast<
string>().ToList())
64 _abCacheUnloadMethod.Invoke(
null,
new[] { labsKey,
true, manifestDicEnumerator.Key,
false });
void Log(LogLevel logLogLevel, object content)