/// <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> /// 获取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> /// 获取资源(同步) /// </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)); }