public void AsyncLoadResource <T>(string resName, ResourceType type, System.Action <string, Object> cb = null) where T : Object { System.Type resType = typeof(T); if (CheckPool(resName, resType)) { if (cb != null) { cb(resName, GetInstance(resName, type, resType)); } return; } else { string realResName = resName.Replace("//", "/"); realResName = resName.Replace("\\", "/"); int index = realResName.LastIndexOf("/"); if (index != -1) { realResName = resName.Substring(index + 1); } if (DevelopSetting.IsLoadAB) { LoadingResInfo resInfo = new LoadingResInfo(); resInfo.ResName = resName; resInfo.ResType = resType; resInfo.BigType = type; resInfo.Cb = cb; resInfo.IsReference = false; AsyncLoadABResources(resInfo, (subResInfo, ab) => { try { Object obj = ab.LoadAsset(realResName); AsyncLoaded(obj, subResInfo.ResType, subResInfo.BigType, subResInfo.ResName, subResInfo.Cb); } catch (System.Exception e) { Common.UDebug.LogError("res load failed:" + resName + " message:" + e.Message); } }); } else { StringBuilder path = GetPathByResourceType(resName, type); //用来测试 ResourcesLoader.Instance.AloadResources(path.ToString(), (obj) => { AsyncLoaded(obj, resType, type, resName, cb); }); } } }
/// <summary> /// /// </summary> /// <param name="resInfo"></param> /// <param name="cb"></param> /// <param name="SpecifyPath">仅用于依赖资源的路径</param> /// <param name="srcPath">加载依赖资源的源资源路径</param> void AsyncLoadPersistentAssets(LoadingResInfo resInfo, System.Action <LoadingResInfo, AssetBundle> cb, LoadingResInfo srcResInfo = null, System.Action <LoadingResInfo> allLoadedcb = null) { System.Action afterAllLoaded = () => { if (_referenceNotLoaded.ContainsKey(resInfo.SrcPath)) { --_referenceNotLoaded[resInfo.SrcPath]; ///加载完所有依赖后执行 if (_referenceNotLoaded[resInfo.SrcPath] == 0 && allLoadedcb != null) { allLoadedcb(srcResInfo); } } }; System.Type resType = typeof(AssetBundle); if (CheckPool(resInfo.ResName, resType)) { if (cb != null) { cb(resInfo, _objPool[resType][resInfo.ResName] as AssetBundle); } afterAllLoaded(); } else { StringBuilder path = new StringBuilder(); path.Append(PathUtils.PERSISTENT_DATA_PATH); if (string.IsNullOrEmpty(resInfo.SpecifyPath)) { StringBuilder subPath = GetPathByResourceType(resInfo.ResName, resInfo.BigType); subPath.Append(GetExtensionName(resInfo.BigType)); path.Append(subPath); } else { path.Append(resInfo.SpecifyPath); } AsyncLoadDependencyAssetBundle(path.ToString(), resInfo, (subResInfo) => { ResourcesLoader.Instance.AsyncLoadAssetBundle(path.ToString(), (ab) => { ///SpecifyPath不为空表示是依赖资源,需要加入objpool中 if (string.IsNullOrEmpty(subResInfo.SpecifyPath) == false) { CheckPoolDic(resType); _objPool[resType][subResInfo.ResName] = ab; DealResAfterLoaded(subResInfo.ResName, subResInfo.BigType, subResInfo.ResType, ab); } if (cb != null) { cb(subResInfo, ab); } afterAllLoaded(); }, subResInfo.IsReference); }); } }
//Object,System.Type, ResourceType, string, System.Action<string, Object> void AsyncLoadABResources(LoadingResInfo resInfo, System.Action <LoadingResInfo, AssetBundle> cb) { if (DevelopSetting.IsUsePersistent) { AsyncLoadPersistentAssets(resInfo, cb); } else { AsyncLoadStreamingAssets(resInfo, cb); } }
/// <summary> /// /// </summary> /// <param name="path"></param> /// /// <param name="srcResInfo">目标资源的相关信息</param> /// <param name="cb">所有依赖资源加载完毕,再加载目标资源</param> void AsyncLoadDependencyAssetBundle(string path, LoadingResInfo srcResInfo, System.Action <LoadingResInfo> cb) { StringBuilder sb = new StringBuilder(); sb.Append(path.ToString()); sb.Append(".manifest"); if (Common.FileUtils.Exist(sb.ToString()) == false) { return; } ResourcesLoader.Instance.AsyncLoadTextAssets(sb.ToString(), (line) => { line = line.Substring(line.LastIndexOf("Dependencies:") + "Dependencies:".Length); line = line.Replace("\n", ""); if (line.Contains("-")) { string[] dependencyPath = line.Split('-'); string resName; string realPath; _referenceNotLoaded[path] = dependencyPath.Length - 1; if (_referenceInfo.ContainsKey(srcResInfo.ResName) == false) { List <string> reference = new List <string>(); _referenceInfo[srcResInfo.ResName] = reference; } _referenceInfo[srcResInfo.ResName].Clear(); LoadingResInfo resInfo; ///0是 空 for (int i = 1, count = dependencyPath.Length; i < count; ++i) { dependencyPath[i] = dependencyPath[i].Replace("//", "/"); resName = dependencyPath[i].Substring(dependencyPath[i].LastIndexOf("/") + 1); resName = resName.Substring(0, resName.LastIndexOf("_")); _referenceInfo[srcResInfo.ResName].Add(resName); if (_referenceCount.ContainsKey(resName) == false) { _referenceCount[resName] = 0; } ++_referenceCount[resName]; realPath = dependencyPath[i].Substring(dependencyPath[i].IndexOf("ResourceAssetBundles")); resInfo = new LoadingResInfo(); resInfo.ResName = resName; resInfo.BigType = ResourceType.AssetBundle; resInfo.SpecifyPath = realPath; resInfo.SrcPath = path; resInfo.IsReference = true; if (DevelopSetting.IsUsePersistent) { AsyncLoadPersistentAssets(resInfo, null, srcResInfo, cb); } else { AsyncLoadStreamingAssets(resInfo, null, srcResInfo, cb); } } } else { cb(srcResInfo); } }); }