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="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); } }
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); } }
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); } }