/// <summary> /// 执行assetbundle异步加载检测 /// </summary> private void ProcessABLoadAsyn() { CMAssetBundle tempCMAB = null; string tempAbPath = null; AssetBundleCreateRequest tempABCreateRequiest = null; if (CurAsynLoadingAbNum > 0) { var enumerator = asynLoadingAbDic.GetEnumerator(); while (enumerator.MoveNext()) { if (enumerator.Current.Value.isDone) { tempStrLst.Add(enumerator.Current.Key); } } } if (tempStrLst.Count > 0) { for (int i = 0, max = tempStrLst.Count; i < max; i++) { tempAbPath = tempStrLst[i]; tempCMAB = GetCMAssetBundle(tempAbPath); if (asynLoadingAbDic.TryGetValue(tempAbPath, out tempABCreateRequiest)) { if (!tempCMAB.IsDone()) { //执行完成 tempCMAB.Done(tempABCreateRequiest.assetBundle); //给依赖资源添加引用计数 AddABDependenceRef(tempStrLst[i]); } if (asynLoadingAbDic.ContainsKey(tempAbPath)) { asynLoadingAbDic.Remove(tempAbPath); } } } tempStrLst.Clear(); } while (CanLoadNextABAsyn()) { tempAbPath = waitingABPath.Dequeue(); tempCMAB = GetCMAssetBundle(tempAbPath); if (tempCMAB.GetTaskState() != CMABTaskState.CMTaskState_None && tempCMAB.GetTaskState() != CMABTaskState.CMTaskState_Waiting) { continue; } tempABCreateRequiest = AssetBundle.LoadFromFileAsync(GetFileFullPath("ui/" + tempAbPath)); tempCMAB.OnProcessing(tempABCreateRequiest.progress); asynLoadingAbDic.Add(tempAbPath, tempABCreateRequiest); } }
/// <summary> /// 异步加载AB /// </summary> /// <param name="abPath"></param> public void LoadAssetBundleAsyn(string abPath) { CMAssetBundle cmAB = null; if (!m_dicAssetBundleDatas.TryGetValue(abPath, out cmAB)) { cmAB = CMAssetBundle.Create(abPath); m_dicAssetBundleDatas.Add(abPath, cmAB); } if (cmAB.GetTaskState() != CMABTaskState.CMTaskState_None) { //已经在加载了 return; } string[] dependenciesPath = GetABAllDependencies(cmAB.ABPath); if (null != dependenciesPath) { for (int i = 0, max = dependenciesPath.Length; i < max; i++) { if (!waitingABPath.Contains(dependenciesPath[i])) { waitingABPath.Enqueue(dependenciesPath[i]); } } } waitingABPath.Enqueue(abPath); cmAB.OnWaitingStart(); //DoNextABLoad(); ProcessABLoadAsyn(); }
/// <summary> /// 执行下一个ab加载 /// </summary> /// <returns></returns> private bool DoNextABLoad() { if (null != abCreateRequiest || !HasNextAB) { return(false); } bool doNextSuccess = false; if (waitingABPath.Count > 0) { string path = waitingABPath.Dequeue(); CMAssetBundle cmAB = null; if (m_dicAssetBundleDatas.TryGetValue(path, out cmAB) && (cmAB.GetTaskState() == CMABTaskState.CMTaskState_Processing || cmAB.GetTaskState() == CMABTaskState.CMTaskState_Done)) { return(DoNextABLoad()); } else { if (null == cmAB) { cmAB = CMAssetBundle.Create(path); m_dicAssetBundleDatas.Add(cmAB.ABPath, cmAB); } try { abCreateRequiest = AssetBundle.LoadFromFileAsync(GetFileFullPath("ui/" + path)); if (null != abCreateRequiest) { doNextSuccess = true; curLoadPath = path; cmAB.OnProcessing(abCreateRequiest.progress); } } catch (Exception e) { cmAB.Done(null); Debug.LogError(e); } } } return(doNextSuccess); }
/// <summary> /// 获取资源(同步) /// </summary> /// <param name="abPath"></param> /// <param name="assetName"></param> /// <returns></returns> public UnityEngine.Object CreateAsset(string abPath, string assetName) { CMAssetBundle cmAB = null; if (!m_dicAssetBundleDatas.TryGetValue(abPath, out cmAB)) { cmAB = CMAssetBundle.Create(abPath); m_dicAssetBundleDatas.Add(abPath, cmAB); } if (cmAB.GetTaskState() == CMABTaskState.CMTaskState_None) { string [] dependences = GetABAllDependencies(abPath); if (null != dependences) { CMAssetBundle dpCmAB = null; AssetBundle dpAB = null; for (int i = 0, max = dependences.Length; i < max; i++) { if (!m_dicAssetBundleDatas.TryGetValue(dependences[i], out dpCmAB)) { dpCmAB = CMAssetBundle.Create(dependences[i]); m_dicAssetBundleDatas.Add(dependences[i], dpCmAB); } if (dpCmAB.IsDone()) { continue; } dpAB = AssetBundle.LoadFromFile(GetFileFullPath("ui/" + dependences[i])); dpCmAB.Done(dpAB); } } cmAB.OnWaitingStart(); AssetBundle ab = AssetBundle.LoadFromFile(GetFileFullPath("ui/" + abPath)); cmAB.OnProcessing(1); cmAB.Done(ab); } return(cmAB.CreateAsset(assetName)); }