public T LoadExistsAsset <T>(string path, bool showError = true) where T : Object { if (string.IsNullOrEmpty(path)) { return(null); } if (!ResourceInfos.ContainsKey(path)) { if (showError) { string msg = "找不到路径:" + path; Log.Error(msg); } return(null); } LoadResourceInfo info = ResourceInfos[path]; if (haveLoadDic.ContainsKey(info.orialPath)) { return(haveLoadDic[info.orialPath] as T); } if (showError) { string msg = "找不到原始路径:" + info.orialPath; Log.Error(msg); } return(null); }
public void RemoveAsset(string path) { #if UNITY_EDITOR if (IsReadFromAB == false) { return; } #endif if (ResourceInfos.ContainsKey(path) == false) { return; } LoadResourceInfo info = ResourceInfos[path]; if (UnLoadAbLists.ContainsKey(info.orialPath)) { UnLoadAbLists.Remove(info.orialPath); } if (haveLoadDic.ContainsKey(info.orialPath)) { haveLoadDic.Remove(info.orialPath); } if (ManulUnloadDic.ContainsKey(info.orialPath)) { ManulUnloadDic.Remove(info.orialPath); } }
void UnloadDependce(LoadResourceInfo path) { string[] dependences = abDepenceInfos.GetAllDependencies(path.unityDepedencePath); for (int i = 0; i < dependences.Length; i++) { if (string.IsNullOrEmpty(dependences[i])) { continue; } LoadResourceInfo info = ResourceInfos[dependences[i]]; if (info.type == 1) { if (UnLoadAbLists.ContainsKey(info.orialPath)) { UnLoadAbLists[info.orialPath].Unload(false); UnLoadAbLists.Remove(info.orialPath); haveLoadDic.Remove(info.orialPath); } } if (info.type == 2) { SetAtlasPrefab(info); } } }
//由于多次实验发现,资源依赖的所有资源包括第二次以及更多层的资源都包含在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); } } }
private void SetAtlasPrefab(LoadResourceInfo info) { if (UnLoadAbLists.ContainsKey(info.orialPath)) { if (CompleteAtlasDic.ContainsKey(info.orialPath) == false) { } } }
public void MualAddAsset(string path, Object o, int sourceType) { if (ResourceInfos.ContainsKey(path) == false) { LoadResourceInfo infoOr = new LoadResourceInfo(path, sourceType); ResourceInfos.Add(path, infoOr); } haveLoadDic[path] = o; }
void LoadAbFix(string path) { string _path = path; LoadResourceInfo info = ResourceInfos[_path]; #if UNITY_IOS StartCoroutine(LoadAB(info, _path)); #else StartCoroutine(LoadAB(info, _path)); // if (info.type == 4 || info.type == 5) // StartCoroutine(LoadAB(info, _path)); // else // { // StartCoroutine(LoadABRequest(info, _path)); // } #endif }
//路径分为两类,一类原来的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); }
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; }
public void UnloadManulDependce(string path) { #if UNITY_EDITOR if (IsReadFromAB == false) { return; } #endif if (ResourceInfos.ContainsKey(path) == false) { return; } LoadResourceInfo info = ResourceInfos[path]; if (ManulUnloadDic.ContainsKey(info.orialPath)) { return; } ManulUnloadDic.Add(info.orialPath, 1); string[] dependences = abDepenceInfos.GetAllDependencies(info.unityDepedencePath); for (int i = 0; i < dependences.Length; i++) { if (string.IsNullOrEmpty(dependences[i])) { continue; } LoadResourceInfo temp = ResourceInfos[dependences[i]]; if (UnLoadAbLists.ContainsKey(temp.orialPath)) { UnLoadAbLists[temp.orialPath].Unload(false); UnLoadAbLists.Remove(temp.orialPath); haveLoadDic.Remove(temp.orialPath); } } }
IEnumerator LoadAB(LoadResourceInfo info, string path) { string _path = path; if (info.type != 5 && versionDic.ContainsKey(info.unityDepedencePath) == false) { LoadFinishHandler(_path); string msg = "找不到路径:" + path; Log.Error(msg); yield break; } WWW _www; if (info.type == 4 || info.type == 5) { _www = new WWW(info.loadPath); } else { //_www = WWW.LoadFromCacheOrDownload(info.loadPath, versionDic[info.unityDepedencePath]); _www = new WWW(info.loadPath); } yield return(_www); UnityEngine.Object obj = null; if (_www.isDone && string.IsNullOrEmpty(_www.error)) { AssetBundle assetBundle = null; if (info.type == 4) { byte[] bytes = _www.bytes; Util._MaskData(bytes); assetBundle = AssetBundle.LoadFromMemory(bytes); } else { assetBundle = _www.assetBundle; } if (assetBundle != null) { obj = assetBundle.LoadAsset(info.name); } else if (info.type == 5) { obj = _www.texture; } if (obj == null) { obj = assetBundle; } if (info.type == 0) { UnloadDependce(info); } else if (info.type == 5 || info.type == 4) { //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.error); Log.Error(msg); } _www.Dispose(); _www = null; LoadFinishHandler(_path, obj); }
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(); }
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="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); } }