/// <summary> /// 回收一个资源 /// </summary> /// <param name="item"></param> /// <param name="destroyCache"></param> protected void DestoryResouceItme(ResInfo item, bool destroyCache = false) { if (item == null) { AFLogger.e("DestoryResouceItme:要释放的资源为空"); return; } if (item.IsRecycled) { AFLogger.e("资源已经被回收,请检查代码是否回收了多次:" + item.ResPath); mResDictionary.Remove(item.mCRC); return; } if (item.RefCount <= 0) { AFLogger.e("资源引用计数<=0:" + item.ResPath); return; } //释放减少引用计数 item.Release(); //如果缓存下来,移到表头 if (!destroyCache) { m_NoRefrenceAssetMapList.InsertToHead(item); return; } //不缓存,要根据引用计数判断是否释放内存 if (item.ReleaseRes()) { AFLogger.i("释放资源成功:" + item.ResPath); //资源缓存 if (!mResDictionary.Remove(item.mCRC)) { return; } m_NoRefrenceAssetMapList.Remove(item); //释放assetbundle引用 ReleaseAssetBundle(item); item.Recycle2Cache(); //在编辑器中加载,需要如此才能清除内存,因此在程序退出时要调用此方法清除内存 #if UNITY_EDITOR Resources.UnloadUnusedAssets(); #endif } //else //{ // AF_Logger.Info("释放资源失败(引用计数不为0或者并没有加载完成):" + item.ResPath); //} }
//回收资源 public bool ReleaseRes() { if (mResState != ResState.Ready) { AFLogger.i("Release Res失败:" + mResPath + " " + mResState); return(false); } if (RefCount <= 0) { AFLogger.i("Release Res:" + mResPath); OnReleaseRes(); return(true); } else { AFLogger.i("Release Res:" + RefCount); return(false); } }