/// <summary> /// 缓存加载的资源 /// </summary> void CacheResource(string path, ref ResourceItem item, uint crc, Object obj, int addrefcount = 1) { if (m_NoRefrenceAssetMapList.Find(item)) { m_NoRefrenceAssetMapList.Remove(item); } //缓存太多,清除最早没有使用的资源 WashOut(); if (item == null) { ZLogger.Error("ResourceItem is null, path : {0}", path); } if (obj == null) { ZLogger.Error("ResourceLoad Fail : {0}", path); } item.m_Obj = obj; item.m_Guid = obj.GetInstanceID(); item.m_LastUseTime = Time.realtimeSinceStartup; item.RefCount += addrefcount; ResourceItem oldItem = null; if (AssetDic.TryGetValue(item.m_Crc, out oldItem)) { AssetDic[item.m_Crc] = item; } else { AssetDic.Add(item.m_Crc, item); } }
/// <summary> /// 回收一个资源 /// </summary> protected void DestoryResourceItem(ResourceItem item, bool destoryCache = false) { if (item == null || item.RefCount > 0) { return; } if (!destoryCache) { m_NoRefrenceAssetMapList.InsertToHead(item); return; } if (!AssetDic.Remove(item.m_Crc)) { return; } m_NoRefrenceAssetMapList.Remove(item); //释放assetbundel引用 AssetBundleMgr.Instance.ReleaseAsset(item); //清空资源对应的对象池 ObjectPoolMgr.Instance.ClearPoolObject(item.m_Crc); if (item.m_Obj != null) { item.m_Obj = null; #if UNITY_EDITOR Resources.UnloadUnusedAssets(); #endif } }
/// <summary> /// 清除资源,需要删除的在AssetBundle里卸载,不需要删除的放到NoReferenceAssetMapList /// </summary> /// <param name="item"></param> /// <param name="is_destory"></param> protected void DestoryResourceItme(ResourceItem item, bool is_destory = false) { if (item == null || item.RefCount > 0) { return; } if (!is_destory) { m_NoReferenceAssetMapList.InsertToHead(item); return; } if (!AssetDic.Remove(item.m_Crc)) { return; } m_NoReferenceAssetMapList.Remove(item); //释放assetbundle引用 AssetBundleManager.Instance.ReleaseAsset(item); //清空对象池 ObjectManager.Instance.CleanObjectPoolByCrc(item.m_Crc); if (item.m_Obj != null) { item.m_Obj = null; #if UNITY_EDITOR //编译器下卸载资源 Resources.UnloadUnusedAssets(); #endif } }
/// <summary> /// 缓存资源 /// </summary> /// <param name="path"></param> /// <param name="item"></param> /// <param name="obj"></param> /// <param name="addRefCount"></param> void CacheResource(string path, ref ResourceItem item, Object obj, int addRefCount = 1) { if (item == null) { Debug.LogError("ResourceManager CacheResource:ResoutceItem is null" + " path:" + path); return; } if (obj == null) { Debug.LogError("ResourceManager CacheResource:Object is null" + " path:" + path); return; } item.m_Obj = obj; item.m_Guid = obj.GetInstanceID(); item.RefCount += addRefCount; item.m_LastUseTime = Time.realtimeSinceStartup; if (AssetDic.ContainsKey(item.m_Crc)) { AssetDic[item.m_Crc] = item; } else { AssetDic.Add(item.m_Crc, item); } }
public override T LoadAsset <T>(string path) { //已加载过 if (AssetDic.ContainsKey(path)) { AssetDic[path].Retain(); return(AssetDic[path].obj as T); } string abName = GetAbName(path); if (!IsLoadBundle(abName)) { //依赖 LoadAssetBundleDepends(abName); BundleInfo.CreateInfo(abName, LoadAssetBundle(abName)); } else { bundleDic[abName].Retain(); } T t = null; if (bundleDic[abName].bundle != null) { t = bundleDic[abName].bundle.LoadAsset <T>(path); } AssetInfo.CreateInfo(path, t); return(t); }
/// <summary> /// 根据路径减少引用计数 /// </summary> /// <param name="crc"></param> /// <param name="count"></param> /// <returns></returns> public int DecreaseResourceRef(uint crc, int count = 1) { ResourceItem item = null; if (!AssetDic.TryGetValue(crc, out item) || item == null) { return(0); } item.RefCount -= count; return(item.RefCount); }
public int DecreaseResourceRef(uint crc, int refCount = 1) { AssetItem assetItem = null; if (!AssetDic.TryGetValue(crc, out assetItem)) { return(0); } assetItem.RefCount -= refCount; return(assetItem.RefCount); }
//减少引用计数 public int ReduceResRef(uint crc, int count = 1) { ResourceItem item = null; if (!AssetDic.TryGetValue(crc, out item) || item == null || item.m_RefCount <= 0) { return(0); } item.m_RefCount -= count; return(item.m_RefCount); }
public int IncreaseResourceRef(uint crc, int refCount = 1) { AssetItem assetItem = null; if (!AssetDic.TryGetValue(crc, out assetItem)) { return(0); } assetItem.RefCount += refCount; assetItem.lastUseTime = Time.realtimeSinceStartup; return(assetItem.RefCount); }
/// <summary> /// 根据path增加引用计数 /// </summary> public int IncreaseResourceRef(uint crc = 0, int count = 1) { ResourceItem item = null; if (!AssetDic.TryGetValue(crc, out item) || item == null) { return(0); } item.RefCount += count; item.m_LastUseTime = Time.realtimeSinceStartup; return(item.RefCount); }
/// <summary> /// 从资源池获取缓存资源 /// </summary> ResourceItem GetCacheResourceItem(uint crc, int addrefcount = 1) { ResourceItem item = null; if (AssetDic.TryGetValue(crc, out item)) { if (item != null) { item.RefCount += addrefcount; item.m_LastUseTime = Time.realtimeSinceStartup; } } return(item); }
public bool ReleaseResource(ResourceObject resObj, bool is_destory = false) { uint crc = resObj.m_Crc; ResourceItem item = null; if (!AssetDic.TryGetValue(crc, out item)) { Debug.LogError("ResourceManager ReleaseResource(resObj):AssetDic中没有crc"); return(false); } GameObject.Destroy(resObj.m_CloneObj); item.RefCount--; DestoryResourceItme(item, is_destory); return(true); }
/// <summary> /// 根据ResourceObj卸载资源,针对给ObjectPoolMgr接口 /// </summary> public bool ReleaseResource(ResourceObj resObj, bool destoryObj = false) { if (resObj == null) { return(false); } ResourceItem item = null; if (!AssetDic.TryGetValue(resObj.m_Crc, out item) || item == null) { ZLogger.Error("AssetDic里不存在该资源:{0} ,可能释放了多次", resObj.m_CloneObj.name); } GameObject.Destroy(resObj.m_CloneObj); item.RefCount--; DestoryResourceItem(item, destoryObj); return(true); }
IEnumerator LoadAssetCoroutineHelper <T>(string path) where T : Object { if (IsLoadAsset(path)) { if (prepareLoadDic.ContainsKey(path)) { while (prepareLoadDic.ContainsKey(path)) { //Debug.Log("while(AssetDic[path].obj == null)"); yield return(null); } if (AssetDic.ContainsKey(path)) { AssetDic[path].Retain(); yield break; } //这里有个极特殊情况,最开始没考虑到,同时异步加载两个资源时,目前的写法是加载第二个资源时进行等待 //但是有可能第一个资源加载完之后立刻卸载了,导致第二个资源等待完之后全都没了,所以需要触发重新加载 else { yield return(LoadAssetCoroutineHelper <T>(path)); } } else { AssetDic[path].Retain(); yield break; } //AssetDic[path].Retain(); //yield break; } AddPrepareLoadDic(path); AssetInfo.CreateInfo(path, null); var request = Resources.LoadAsync <T>(GetResourceLoadPath(path)); yield return(request); AssetInfo.SetObject(path, request.asset); //移除加载中列表 RemovePrepareLoadDic(path); //在异步加载时进来一个卸载,都加载完后在这里统一删除 if (unLoadCacheDic.ContainsKey(path)) { RemoveUnLoadCacheDic(path); } }
/// <summary> /// 不需要实例化的资源的卸载,根据路径 /// </summary> public bool ReleaseResource(string path, bool destoryObj = false) { if (string.IsNullOrEmpty(path)) { return(false); } uint crc = CRC32.GetCRC32(path); ResourceItem item = null; if (!AssetDic.TryGetValue(crc, out item) || item == null) { ZLogger.Error("AssetDic里不存在该资源:{0} ,可能释放了多次", path); } item.RefCount--; DestoryResourceItem(item, destoryObj); return(true); }
/// <summary> /// 外部方法释放资源 /// </summary> /// <param name="path"></param> /// <param name="is_destory"></param> /// <returns></returns> public bool ReleaseResource(string path, bool is_destory = false) { if (string.IsNullOrEmpty(path)) { Debug.LogError("ResourceManager.ReleaseResource path is null"); return(false); } uint crc = Crc32.GetCrc32(path); ResourceItem item = null; if (!AssetDic.TryGetValue(crc, out item)) { Debug.LogError("ResourceManager ReleaseResource:AssetDic中没有path:" + path); return(false); } item.RefCount--; DestoryResourceItme(item, is_destory); return(true); }
IEnumerator LoadAssetCoroutineHelper <T>(string path) where T : Object { //Debug.Log("LoadAssetCoroutineHelper begin"); if (IsLoadAsset(path)) { //如果重复对同样的资源进行异步加载,需要等待 if (prepareLoadDic.ContainsKey(path)) { while (prepareLoadDic.ContainsKey(path)) { yield return(null); } if (AssetDic.ContainsKey(path)) { AssetDic[path].Retain(); yield break; } //这里有个极特殊情况,最开始没考虑到,同时异步加载两个资源时,目前的写法是加载第二个资源时进行等待 //但是有可能第一个资源加载完之后立刻卸载了,导致第二个资源等待完之后全都没了,所以需要触发重新加载 else { yield return(LoadAssetCoroutineHelper <T>(path)); } } else { AssetDic[path].Retain(); yield break; } } AddPrepareLoadDic(path); //先添加进容器 AssetInfo.CreateInfo(path, null); string abName = GetAbName(path); if (!IsLoadBundle(abName)) { //先添加进容器,防止重复调用 BundleInfo.CreateInfo(abName, null); yield return(LoadAssetBundleDependsAsync(abName)); //Debug.Log("LoadAssetCoroutineHelper abName=" + abName); var bundleRequest = AssetBundle.LoadFromFileAsync(GetLoadPath(abName)); yield return(bundleRequest); bundleDic[abName].bundle = bundleRequest.assetBundle; } else { //Debug.Log("LoadAssetCoroutineHelper IsLoadAbName=" + abName); bundleDic[abName].Retain(); } AssetBundle bundle = bundleDic[abName].bundle; //Debug.Log("LoadAssetCoroutineHelper path=" + path); AssetBundleRequest request = bundle.LoadAssetAsync <T>(path); yield return(request); AssetInfo.SetObject(path, request.asset); //Debug.Log("LoadAssetCoroutineHelper over"); //移除加载中列表 RemovePrepareLoadDic(path); //在异步加载时进来一个卸载,都加载完后在这里统一删除 if (unLoadCacheDic.ContainsKey(path)) { Debug.Log("unLoadCacheDic=" + path); RemoveUnLoadCacheDic(path); UnLoadAsset(path); } }