//--释放asset //--注意这里需要保证外面没有引用这些path的inst了,不然会出现材质丢失的问题 //--不要轻易调用,除非你对内部的资源的生命周期有了清晰的了解 //--@param includePooledGo: 是否需要将预设也释放 //--@param patharray: 需要释放的资源路径数组 public static void CleanupWithPathArray(this GameObjectPoolComponent self, bool includePooledGo = true, List <string> patharray = null) { Debug.Log("GameObjectPool Cleanup "); Dictionary <string, bool> dict_path = null; if (patharray != null) { dict_path = new Dictionary <string, bool>(); for (int i = 0; i < patharray.Count; i++) { dict_path[patharray[i]] = true; } } foreach (var item in self.__instCache) { if (dict_path.ContainsKey(item.Key)) { for (int i = 0; i < item.Value.Count; i++) { var inst = item.Value[i]; if (inst != null) { GameObject.Destroy(inst); self.__goInstCountCache[item.Key]--; } self.__instPathCache.Remove(inst); } } } for (int i = 0; i < patharray.Count; i++) { self.__instCache.Remove(patharray[i]); } if (includePooledGo) { List <string> keys = self.__goPool.Keys.ToList(); for (int i = keys.Count - 1; i >= 0; i--) { var path = keys[i]; if (patharray != null && dict_path.ContainsKey(path) && self.__goPool.TryOnlyGet(path, out var pooledGo)) { if (pooledGo != null && self.__CheckNeedUnload(path)) { ResourcesComponent.Instance.ReleaseAsset(pooledGo); self.__goPool.Remove(path); } } } } }
// 释放资源 public static void __ReleaseAsset(this GameObjectPoolComponent self, string path) { if (self.__instCache.ContainsKey(path)) { for (int i = self.__instCache[path].Count - 1; i >= 0; i--) { self.__instPathCache.Remove(self.__instCache[path][i]); GameObject.Destroy(self.__instCache[path][i]); self.__instCache[path].RemoveAt(i); } self.__instCache.Remove(path); self.__goInstCountCache.Remove(path); } if (self.__goPool.TryOnlyGet(path, out var pooledGo) && self.__CheckNeedUnload(path)) { ResourcesComponent.Instance.ReleaseAsset(pooledGo); self.__goPool.Remove(path); } }