private void ProcessAbAssetLoadAsyn(float deltaTime) { var enumerator = m_dicAssetBundleDatas.GetEnumerator(); while (enumerator.MoveNext()) { enumerator.Current.Value.Process(deltaTime); if (enumerator.Current.Value.HasAssetLoadComplete()) { alreadyLoadAsset.Add(enumerator.Current.Key); } } if (alreadyLoadAsset.Count > 0) { CMAssetBundle cmab = null; for (int i = 0, max = alreadyLoadAsset.Count; i < max; i++) { if (TryGetCMAssetBundle(alreadyLoadAsset[i], out cmab)) { cmab.HandleAssetCreateDlg(); } } alreadyLoadAsset.Clear(); } }
/// <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> /// 资源引用次数减一 /// </summary> /// <param name="abPath"></param> /// <param name="assetName"></param> public void Release(string abPath, string assetName) { CMAssetBundle cmAB = null; if (m_dicAssetBundleDatas.TryGetValue(abPath, out cmAB) && cmAB.IsDone()) { cmAB.ReleaseAsset(assetName); } }
/// <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); } }
public bool IsAssetReady(string ab, string assetName) { CMAssetBundle cb = null; if (TryGetCMAssetBundle(ab, out cb) && cb.IsAssetReady(assetName)) { return(true); } return(false); }
/// <summary> /// 移除资源引用计数 /// </summary> /// <param name="assetName"></param> /// <param name="abPath"></param> public void RemoveAssetRef(string assetName, string abPath) { CMAssetBundle ab = null; if (!TryGetCMAssetBundle(abPath, out ab)) { Debug.LogError(string.Format("CMAssetBundleLoaderMgr->RemoveAssetRef failed,CmassetBundle {0} not exit", abPath)); return; } ab.ReleaseAsset(assetName); }
/// <summary> /// 获取CMAssetBundle没有就创建 /// </summary> /// <param name="abPath"></param> /// <returns></returns> private CMAssetBundle GetCMAssetBundle(string abPath) { CMAssetBundle cmAssetBundle = null; if (!TryGetCMAssetBundle(abPath, out cmAssetBundle)) { cmAssetBundle = CMAssetBundle.Create(abPath); m_dicAssetBundleDatas.Add(abPath, cmAssetBundle); } return(cmAssetBundle); }
/// <summary> /// 获取资源(异步) /// </summary> /// <param name="abPath"></param> /// <param name="assetName"></param> /// <param name="loadFinish"></param> /// <param name="param"></param> public void CreateAssetAsyn(bool preLoad, string abPath, string assetName, AssetLoadFinsh loadFinish, object param1 = null, object param2 = null, object param3 = null) { CMAssetBundle cmAB = null; if (!m_dicAssetBundleDatas.TryGetValue(abPath, out cmAB)) { cmAB = CMAssetBundle.Create(abPath); m_dicAssetBundleDatas.Add(abPath, cmAB); } cmAB.CreateAssetAsyn(preLoad, assetName, loadFinish, param1, param2, param3); LoadAssetBundleAsyn(cmAB.ABPath); }
/// <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> /// abpath AssetBundle加载完成,给依赖AB添加引用计数 /// </summary> /// <param name="abpath"></param> private void AddABDependenceRef(string abpath) { if (m_dicAssetBundleDatas.ContainsKey(abpath)) { string [] dependencies = GetABAllDependencies(abpath); if (null != dependencies) { CMAssetBundle cmAB = null; for (int i = 0, max = dependencies.Length; i < max; i++) { if (m_dicAssetBundleDatas.TryGetValue(dependencies[i], out cmAB)) { //cmAB.AddRef(); } } } } }
/// <summary> /// 是否依赖资源加载完成 /// </summary> /// <param name="abPath"></param> /// <returns></returns> public bool IsDependenceLoadComplete(string abPath) { string[] dependences = GetABAllDependencies(abPath); bool complete = true; if (null != dependences) { CMAssetBundle dpCmAB = null; for (int i = 0, max = dependences.Length; i < max; i++) { if (!TryGetCMAssetBundle(dependences[i], out dpCmAB) || !dpCmAB.IsDone()) { complete = false; break; } } } return(complete); }
/// <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)); }
/// <summary> /// 尝试获取CMAssetBundle /// </summary> /// <param name="abPath"></param> /// <param name="ab"></param> /// <returns></returns> public bool TryGetCMAssetBundle(string abPath, out CMAssetBundle ab) { return(m_dicAssetBundleDatas.TryGetValue(abPath, out ab)); }
public CMAsset(string assetName, CMAssetBundle cmAssetBundle) { this.m_asset = null; this.m_strAssetName = assetName; m_cmAB = cmAssetBundle; }