/// <summary> /// 加载场景 /// </summary> /// <param name="mapResourceId">场景ID</param> /// <param name="callBack">场景加载完成回调</param> /// <returns>场景加载对象</returns> public AssetBundleLoader LoadSceneLevel(uint mapResourceId, LoadAssetFinish <GameObject> callBack) { string sceneFileName = "Scene/" + mapResourceId + ".assetbundle"; string sceneName = mapResourceId.ToString(); if (!loadAssetbundle) { AssetBundleLoader s = new AssetBundleLoader("", "", typeof(GameObject)); StartCoroutine(LoadSceneInResource(mapResourceId, callBack)); return(s); } int[] subTypeList = { PRTypeConst.ST_SKILL, PRTypeConst.ST_SOUND }; for (int i = 0; i < subTypeList.Length; i++) { IList <SysReadyLoadVo> preloadList = BaseDataMgr.instance.GetScenePreLoadList(mapResourceId, subTypeList[i]); Singleton <StartLoadingView> .Instance.PreLoadNum += preloadList.Count; } SysMapVo mapVo = BaseDataMgr.instance.GetMapVo(MeVo.instance.mapId); //获取要加载的怪物队列,并计算预加载数 if (mapVo.type == MapTypeConst.COPY_MAP) { MonsterMgr.Instance.PreMonloadList = BaseDataMgr.instance.GetMonPreLoadList(MeVo.instance.mapId); if (MonsterMgr.Instance.PreMonloadList.Count > 0) { Singleton <StartLoadingView> .Instance.PreLoadNum += MonsterMgr.Instance.PreMonloadList.Count; } } return(LoadSceneLevel(sceneFileName, sceneName, callBack)); }
/// <summary> /// 加载并缓存共享资源包里的资源 /// </summary> /// <param name="path">路径</param> /// <param name="fileName">资源包名称</param> /// <param name="callBack">加载结束回调</param> /// <param name="assetName">资源名,默认为null则加载主资源</param> /// <returns>资源包加载类</returns> public AssetBundleLoader LoadAndCacheSharedAssets(string fileName, LoadAssetFinish <UnityEngine.Object> callBack) { if (!loadAssetbundle) { callBack(null); return(new AssetBundleLoader(fileName, "", typeof(GameObject))); } string path = getAssetBundleLoaderPath(fileName); AssetBundleLoader assetBundleLoader = new AssetBundleLoader(path, fileName, null); if (!sharedAssets.ContainsKey(fileName)) { //未缓存则缓存 Log.info(this, "加载" + path + fileName); StartCoroutine(LoadAndCachedAllAssetInAssetBundle(assetBundleLoader, callBack)); } else { if (callBack != null) { callBack(null); } } return(assetBundleLoader); }
//由于多次实验发现,资源依赖的所有资源包括第二次以及更多层的资源都包含在dependence中所以类型1可以直接加入loading链表中,有错误再修改,需要同时修改依赖加载完成的函数 private void AddPathToLoadingList(LoadResourceInfo path, LoadAssetFinish obj) { if (string.IsNullOrEmpty(path.orialPath)) { Log.Debug("path.orialPathpath.orialPathpath.orialPathpath.orialPathpath.orialPathpath.orialPathpath.orialPath"); } if (haveLoadDic.ContainsKey(path.orialPath)) { if (obj != null) { obj(haveLoadDic[path.orialPath], path.orialPath); } } else if (LoadWaitingDic.ContainsKey(path.orialPath)) { if (obj != null) { LoadAssetFinish call = LoadWaitingDic[path.orialPath]; if (call == null) { LoadWaitingDic[path.orialPath] = obj; } else { LoadWaitingDic[path.orialPath] += obj; } } } else if (loadingDic.ContainsKey(path.orialPath)) { if (obj != null) { LoadAssetFinish call = loadingDic[path.orialPath]; if (call == null) { loadingDic[path.orialPath] = obj; } else { loadingDic[path.orialPath] += obj; } } } else { if (currentLoadNum < MaxLoadWwwNum) { currentLoadNum++; loadingDic.Add(path.orialPath, obj); LoadAbFix(path.orialPath); } else { LoadWaitingDic.Add(path.orialPath, obj); } } }
/// <summary> /// 创建类型 /// </summary> public void CreateModel(ModelType type, string id, LoadAssetFinish <GameObject> loadInitCallBack) { if (id.Equals("0")) { id = "10004"; } string path = "Model/" + type + "/" + id + "/Model/" + id + ".assetbundle"; AssetManager.Instance.LoadAsset <GameObject>(path, loadInitCallBack); }
private IEnumerator LoadSceneInResource(uint mapResourceId, LoadAssetFinish <GameObject> callBack) { AssetBundleLoader s = new AssetBundleLoader("", "", typeof(GameObject)); yield return(StartCoroutine(LoadSceneLevel(s, mapResourceId.ToString()))); if (callBack != null) { addCallBack <GameObject>(mapResourceId.ToString(), callBack); } LoadScenePreAsset(mapResourceId, mapResourceId.ToString()); }
void LoadWaiting() { if (LoadWaitingDic.Count > 0 && currentLoadNum < MaxLoadWwwNum) { string next = GetNextWaitingAsset(); if (!string.IsNullOrEmpty(next)) { LoadAssetFinish back = LoadWaitingDic[next]; LoadWaitingDic.Remove(next); AddPathToLoadingList(ResourceInfos[next], back); } } }
private void addCallBack <T>(string assetKey, LoadAssetFinish <T> callBack) where T : UnityEngine.Object { lock (cachedAssetsCallBack) { if (!cachedAssetsCallBack.ContainsKey(assetKey)) { cachedAssetsCallBack[assetKey] = callBack; } else { LoadAssetFinish <T> call = cachedAssetsCallBack[assetKey] as LoadAssetFinish <T>; call += callBack; cachedAssetsCallBack[assetKey] = call; } } }
//路径分为两类,一类原来的resouce下的路径存在大写,为name做准备;、、 //一类是stream下面的只有小写为加载资源做准备(依赖的资源的路径都是此类),还有一类是加载资源路径,需要加头路径的 /// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="backAction"></param> /// <param name="type">0表示普通的资源加载,1表示图集prefab的加载</param> public void LoadAsset(string path, LoadAssetFinish backAction, int type = 0) { LoadResourceInfo info; if (ResourceInfos.ContainsKey(path)) { info = ResourceInfos[path]; } else { switch (type) { case 1: info = new LoadResourceInfo(path, 2); break; case 4: info = new LoadResourceInfo(path, 4); break; case 3: info = new LoadResourceInfo(path, 3); break; case 5: info = new LoadResourceInfo(path, 5, true); break; default: info = new LoadResourceInfo(path, 0); break; } ResourceInfos.Add(path, info); } if (haveLoadDic.ContainsKey(info.orialPath)) { if (backAction != null) { backAction(haveLoadDic[info.orialPath], info.orialPath); } return; } DellDepenceAssent(info, backAction); }
void LoadFinishHandler(string _path, UnityEngine.Object obj = null) { currentLoadNum--; if (loadingDic.ContainsKey(_path)) { LoadAssetFinish actiona = loadingDic[_path]; if (actiona != null) { actiona(obj, _path); } } loadingDic.Remove(_path); LoadWaiting(); }
IEnumerator LoadLocalAsset(LoadResourceInfo path, LoadAssetFinish callbaAction) { Object oo = null; string temp = path.orialPath.Replace(ABExtension, ""); ResourceRequest request = Resources.LoadAsync(temp); yield return(request); oo = request.asset; if (!haveLoadDic.ContainsKey(path.orialPath)) { haveLoadDic.Add(path.orialPath, oo); } callbaAction(oo, path.orialPath); yield break; }
/// <summary> /// 加载资源包中的资源 /// </summary> /// <typeparam name="T">资源类型</typeparam> /// <param name="path">路径</param> /// <param name="fileName">资源包名称</param> /// <param name="callBack">加载结束回调</param> /// <param name="assetName">资源名,默认为null则加载主资源</param> /// <returns>资源包加载类</returns> public AssetBundleLoader LoadAsset <T>(string path, string fileName, LoadAssetFinish <T> callBack, string assetName = null, bool cache = false, bool scene = false, bool asynload = false) where T : UnityEngine.Object { //共享中存在,直接返回缓存数据 if (sharedAssets.ContainsKey(fileName)) { UnityEngine.Object asset = LoadAsset(sharedAssets[fileName], assetName, typeof(T)); if (callBack != null) { T result = asset as T; callBack(result); } return(sharedAssets[fileName]); } //缓存中存在 string assetKey = getAssetKey(fileName, assetName, typeof(T)); if (cachedAssets.ContainsKey(assetKey)) { if (callBack != null) { T asset = cachedAssets[assetKey] as T; callBack(asset); } return(new AssetBundleLoader(path, fileName, typeof(T), assetName, cache, DownLoadState.Cached, scene, asynload)); } //添加回调 if (callBack != null) { addCallBack <T>(assetKey, callBack); } lock (loadingAssetsBundles){ //检查是否正在加载 if (loadingAssetsBundles.ContainsKey(fileName)) { return(loadingAssetsBundles[fileName]); } } AssetBundleLoader assetBundleLoader = new AssetBundleLoader(path, fileName, typeof(T), assetName, cache, DownLoadState.Init, scene, asynload); addWaitAssetBundleLoader(assetBundleLoader); return(assetBundleLoader); }
/// <summary> /// 多个资源一起加载, 注意当要返回加载进度的时候,只能同时调用一次这个函数 /// </summary> /// <param name="paths"></param> /// <param name="backAction"></param> /// <param name="returnBackPer">是否返回加载进度</param> public void LoadMutileAssets(string[] paths, Action <float> backAction, bool returnBackPer = false, int sourceType = 0) { int allLenth = 0; int curr = 0; int all = 0; List <LoadResourceInfo> infos = new List <LoadResourceInfo>(); for (int i = 0; i < paths.Length; i++) { string tempPath = paths[i]; LoadResourceInfo infoOr; if (ResourceInfos.ContainsKey(tempPath)) { infoOr = ResourceInfos[tempPath]; } else { infoOr = new LoadResourceInfo(tempPath, sourceType); ResourceInfos.Add(tempPath, infoOr); } string[] dependences = abDepenceInfos.GetDirectDependencies(infoOr.unityDepedencePath); if (!infos.Contains(infoOr)) { for (int j = 0; j < dependences.Length; j++) { string dependPath = dependences[j]; if (fileSizeDic.ContainsKey(dependPath)) { allLenth += fileSizeDic[dependPath]; } } all += 1; #if UNITY_EDITOR if (IsReadFromAB) { all += dependences.Length; } #else all += dependences.Length; #endif infos.Add(infoOr); if (fileSizeDic.ContainsKey(infoOr.unityDepedencePath)) { allLenth += fileSizeDic[infoOr.unityDepedencePath]; } } } LoadAssetFinish innerCallBack = (o, p) => { curr++; if (returnBackPer) { LoadResourceInfo info = ResourceInfos[p]; if (fileSizeDic.ContainsKey(info.unityDepedencePath)) { float per = 100 * (1.0f * fileSizeDic[info.unityDepedencePath] / allLenth); backAction(per); } } if (curr >= all) { if (backAction != null) { backAction(100); } } }; for (int i = 0; i < infos.Count; i++) { DellDepenceAssent(infos[i], innerCallBack, true); } }
void DellDepenceAssent(LoadResourceInfo path, LoadAssetFinish callbaAction, bool isReturnDepence = false, int LoadNum = 0) { if (string.IsNullOrEmpty(path.orialPath)) { callbaAction(null, path.orialPath); } #if UNITY_EDITOR if (IsReadFromAB == false) { if (haveLoadDic.ContainsKey(path.orialPath)) { callbaAction(haveLoadDic[path.orialPath], path.orialPath); return; } if (path.type == 5) { AddPathToLoadingList(path, callbaAction); } else { StartCoroutine(LoadLocalAsset(path, callbaAction)); } return; } #endif if (haveLoadDic.ContainsKey(path.orialPath)) { if (callbaAction != null && isReturnDepence) { callbaAction(haveLoadDic[path.orialPath], path.orialPath); string depencePath1 = path.unityDepedencePath; string[] dependences1 = abDepenceInfos.GetDirectDependencies(depencePath1); for (int i = 0; i < dependences1.Length; i++) { callbaAction(haveLoadDic[path.orialPath], dependences1[i]); } } else if (callbaAction != null) { callbaAction(haveLoadDic[path.orialPath], path.orialPath); } return; } string depencePath = path.unityDepedencePath; string[] dependences = abDepenceInfos.GetDirectDependencies(depencePath); int length = dependences.Length; if (length == 0) { AddPathToLoadingList(path, callbaAction); } else { int all = length; int curr = 0; LoadAssetFinish innerCallBack = (o, p) => { curr++; if (isReturnDepence) { callbaAction(o, p); } if (curr >= all) { AddPathToLoadingList(path, callbaAction); } }; LoadNum = LoadNum + 1; for (int i = 0; i < dependences.Length; i++) { string dependPath = dependences[i]; if (string.IsNullOrEmpty(dependPath)) { innerCallBack(null, dependPath); continue; } string[] itemdependences = abDepenceInfos.GetDirectDependencies(dependPath); int itemlength = itemdependences.Length; if (itemlength > 0) { continue; } LoadResourceInfo info; if (ResourceInfos.ContainsKey(dependPath)) { info = ResourceInfos[dependPath]; if (LoadNum <= 1 && info.type == 1) { info.type = 3; } } else { int type = 3; if (dependPath.Contains(AtlasPrefabName)) { type = 2; } else if (dependPath.Contains(AtlasTexturebName)) { type = 1; } else if (LoadNum > 1 && !dependPath.Contains("shader.assetbundle") && !dependPath.StartsWith("lib/effect")) { type = 1; } info = new LoadResourceInfo(dependPath, type, true); ResourceInfos.Add(dependPath, info); } AddPathToLoadingList(info, innerCallBack); } for (int i = 0; i < dependences.Length; i++) { string dependPath = dependences[i]; string[] itemdependences = abDepenceInfos.GetDirectDependencies(dependPath); int itemlength = itemdependences.Length; if (itemlength == 0) { continue; } LoadResourceInfo info; if (ResourceInfos.ContainsKey(dependPath)) { info = ResourceInfos[dependPath]; if (LoadNum <= 1 && info.type == 1) { info.type = 3; } } else { int type = 3; if (dependPath.Contains(AtlasPrefabName)) { type = 2; } else if (dependPath.Contains(AtlasTexturebName)) { type = 1; } else if (LoadNum > 1 && !dependPath.Contains("shader.assetbundle") && !dependPath.StartsWith("lib/effect")) { type = 1; } info = new LoadResourceInfo(dependPath, type, true); ResourceInfos.Add(dependPath, info); } DellDepenceAssent(info, innerCallBack, false, LoadNum); //AddPathToLoadingList(info, innerCallBack); } } }
/// <summary> /// 加载并缓存资源包里的所有资源 /// </summary> /// <param name="assetBundleLoader">资源包下载类</param> /// <param name="path">路径</param> /// <param name="fileName">资源包名</param> /// <param name="assetName">资源名</param> /// <param name="callBack">加载结束回调</param> /// <returns></returns> private IEnumerator LoadAndCachedAllAssetInAssetBundle(AssetBundleLoader assetBundleLoader, LoadAssetFinish <UnityEngine.Object> callBack) { yield return(StartCoroutine(assetBundleLoader.LoadAssetBundle(false, false))); //加载完成 if (assetBundleLoader.state == DownLoadState.Loaded) { //assetBundleLoader.LoadAllAssets(); lock (sharedAssets) { if (!sharedAssets.ContainsKey(assetBundleLoader.fileName)) { sharedAssets.Add(assetBundleLoader.fileName, assetBundleLoader); } } } if (callBack != null) { callBack(null); } }
public AssetBundleLoader LoadSceneLevel(string sceneFileName, string sceneName, LoadAssetFinish <GameObject> callBack) { string path = getAssetBundleLoaderPath(sceneFileName); return(LoadAsset <GameObject>(path, sceneFileName, callBack, sceneName, false, true)); }
IEnumerator LoadABRequest(LoadResourceInfo info, string path) { string _path = path; AssetBundleCreateRequest _www = AssetBundle.LoadFromFileAsync(info.loadPath); yield return(_www); UnityEngine.Object obj = null; if (_www.isDone && _www.assetBundle != null) { AssetBundle assetBundle = _www.assetBundle; if (assetBundle != null) { obj = assetBundle.LoadAsset(info.name); } if (obj == null) { obj = assetBundle; } if (info.type == 0) { UnloadDependce(info); } else if (info.type == 5) { //Do Nothing } else { UnLoadAbLists.Add(info.orialPath, assetBundle); if (info.type == 2) { SetAtlasPrefab(info); } } if (info.type != 4 && !haveLoadDic.ContainsKey(info.orialPath)) { haveLoadDic.Add(info.orialPath, obj); } if (info.type == 0) { if (assetBundle != null) { assetBundle.Unload(false); } } } else { string msg = string.Format("Error Path:{0}||Error Msg:{1}", path, _www.ToString()); Log.Error(msg); } //_www.Dispose(); _www = null; currentLoadNum--; if (loadingDic.ContainsKey(_path)) { LoadAssetFinish actiona = loadingDic[_path]; if (actiona != null) { actiona(obj, _path); } } loadingDic.Remove(_path); LoadWaiting(); }
public AssetBundleLoader LoadAssetFromResources <T>(string fileName, LoadAssetFinish <T> callBack, string assetName = null, bool cache = false) where T : UnityEngine.Object { fileName = fileName.Replace(".assetbundle", ""); callBack(ResMgr.instance.load(fileName, typeof(T)) as T); return(new AssetBundleLoader(fileName, assetName, typeof(T))); }
/// <summary> /// 加载资源包中的资源 /// </summary> /// <typeparam name="T">资源类型</typeparam> /// <param name="path">路径</param> /// <param name="fileName">资源包名称</param> /// <param name="callBack">加载结束回调</param> /// <param name="assetName">资源名,默认为null则加载主资源</param> ///<param name="cache">是否缓存</param> /// <param name="asynLoad">是否异步加载asset</param> /// <returns>资源包加载类</returns> public AssetBundleLoader LoadAsset <T>(string fileName, LoadAssetFinish <T> callBack, string assetName = null, bool cache = false, bool asynLoad = true) where T : UnityEngine.Object { string path = getAssetBundleLoaderPath(fileName); return(LoadAsset <T>(path, fileName, callBack, assetName, cache, false, asynLoad)); }
/// <summary> /// 初始化加载 /// </summary> /// <param name="callback">回调信息</param> /// <returns>加载列表</returns> public void init(LoadAssetFinish <UnityEngine.Object> callback) { initCall = callback; initFont(); }