public void UnloadUnusedBundles(int tag) { if (m_AssetBundles.Count == 0) { return; } AssetBundleReference abr = null; Stack <string> checkQueue = StackPool <string> .Get(); HashSet <string> checkings = HashSetPool <string> .Get(); Action <string> checkFun = (key) => { abr = m_AssetBundles[key]; checkings.Remove(key); if (abr.isUnused()) { //check dependencies if (abr.dependencies != null && abr.dependencies.Count > 0) { foreach (AssetBundleReference sub in abr.dependencies) { //只有同样tag和空tag的ref才需要重新检查。 if (sub.isCache && (sub.tagCount == 0 || sub.HaveTag(tag)) && !checkings.Contains(sub.name)) { checkQueue.Push(sub.name); } } } abr.Dispose(); m_AssetBundles.Remove(key); } }; foreach (string key in m_AssetBundles.Keys) { abr = m_AssetBundles[key]; if (abr.HaveTag(tag) && abr.isCache) { checkQueue.Push(key); checkings.Add(key); } } //recheck unused asset bundle while (checkQueue.Count > 0) { checkFun(checkQueue.Pop()); } StackPool <string> .Release(checkQueue); HashSetPool <string> .Release(checkings); }
protected void RemoveAssetBundlesTag(int tag) { if (m_AssetBundles.Count == 0) { return; } AssetBundleReference abr = null; var iter = m_AssetBundles.GetEnumerator(); while (iter.MoveNext()) { abr = iter.Current.Value; if (abr.isCache && abr.HaveTag(tag)) { abr.RemoveTag(tag); } } }