/// <summary> /// /// </summary> /// <param name="name"></param> /// <returns></returns> public XAssetInfo Find(string name) { XAssetInfo index = null; fastIndexs.TryGetValue(name, out index); return(index); }
/// <summary> /// Adds the index of the fast. /// </summary> /// <param name="name">Name.</param> /// <param name="index">Index.</param> public void AddFastIndex(string name, XAssetInfo index) { string key = name; if (System.IO.Path.HasExtension(name)) { key = System.IO.Path.ChangeExtension(name, null); } fastIndexs[key] = index; }
public XAssetInfo getAssetInfoFromCache(XAssetKey key, XLoadStatus status) { if (assets.ContainsKey(key)) { return(assets[key]); } XAssetInfo value = new XAssetInfo { res = key.res, all = key.all, tp = key.tp, status = status }; assets[key] = value; return(value); }
// 获取一个依赖包的资源 public XAssetInfo getAssetInfo(XAssetKey key) { if (assets.ContainsKey(key)) { return(assets[key]); } XAssetInfo value = new XAssetInfo { res = key.res, all = key.all, tp = key.tp, status = XLoadStatus.NONE, bundle = this }; assets[key] = value; return(value); }
// 优化协程版本,不开启更多的协程 IEnumerator coLoad(XLoadDesc desc, Action <XLoadDesc> cb, bool check) { // asset desc!= null if (check) { fillDesc(desc); if (desc.isComplete) { if (cb != null) { cb(desc); } yield break; } } // 一个一个的装载 for (int i = 0; i < desc.reses.Count; i++) { XLoadRes res = desc.reses[i]; desc.cursor = res; XAssetInfo asset = res.asset; if (asset.status == XLoadStatus.NONE) { asset.status = XLoadStatus.LOADING; XBundleInfo bundle = res.asset.bundle; #region bundle 装载流程 // 没装载好 if (bundle.status == XLoadStatus.NONE) { bundle.status = XLoadStatus.LOADING; bool childOK = true; // 拍平了的依赖 for (int j = 0; j < bundle.dependAll.Count; j++) { XBundleInfo dpab = bundle.dependAll[j]; if (dpab.status == XLoadStatus.NONE) { dpab.status = XLoadStatus.LOADING; // 真正装载的路径 dpab.www = new WWW(getPath(dpab)); // XDebug.LogError("装载依赖"+ getPath(dpab)); yield return(dpab.www); if (!string.IsNullOrEmpty(dpab.www.error) || dpab.www.assetBundle == null) { dpab.status = XLoadStatus.FAIL; } else { dpab.ab = dpab.www.assetBundle; dpab.status = XLoadStatus.SUCESS; } dpab.www.Dispose(); dpab.www = null; } if (!dpab.isComplete) { yield return(dpab); } if (dpab.isFail) { bundle.status = XLoadStatus.FAIL; childOK = false; break; } } // 装载自己 if (childOK) { bundle.www = new WWW(getPath(bundle)); // XDebug.LogError("装载自己" + getPath(bundle)); yield return(bundle.www); if (!string.IsNullOrEmpty(bundle.www.error) || bundle.www.assetBundle == null) { bundle.status = XLoadStatus.FAIL; } else { bundle.ab = bundle.www.assetBundle; bundle.status = XLoadStatus.SUCESS; } bundle.www.Dispose(); bundle.www = null; } } #endregion if (!bundle.isComplete) { yield return(bundle); } #region 装载asset // 失败了 if (bundle.isFail) { asset.status = XLoadStatus.FAIL; } else { // bundle成功了 if (asset.all) { asset.rq = bundle.ab.LoadAllAssetsAsync(asset.tp); yield return(asset.rq); if (asset.rq.isDone && asset.rq.allAssets != null) { asset.status = XLoadStatus.SUCESS; asset.obs = asset.rq.allAssets; if (asset.obs != null) { asset.obDict = new Dictionary <string, UnityEngine.Object>(); string[] assetNames = bundle.ab.GetAllAssetNames(); if (assetNames.Length == asset.obs.Length) { for (int k = 0; k < asset.obs.Length; k++) { asset.obDict[assetNames[k]] = asset.obs[k]; } } else { // 如果只有一项,本来名称需要相同的 if (assetNames.Length >= 1) { for (int k = 0; k < asset.obs.Length; k++) { asset.obDict[assetNames[0]] = asset.obs[k]; } } } } } else { asset.status = XLoadStatus.FAIL; } asset.rq = null; } else { string relname = getRelPathName(asset.res, bundle); asset.rq = bundle.ab.LoadAssetAsync(relname, asset.tp); yield return(asset.rq); if (asset.rq.isDone && asset.rq.asset != null) { asset.status = XLoadStatus.SUCESS; asset.ob = asset.rq.asset; } else { asset.status = XLoadStatus.FAIL; } asset.rq = null; } } #endregion } // 可以让自己直接返回 if (!asset.isComplete) { yield return(asset); } } desc._isComplete = true; if (cb != null) { cb(desc); } }
// 真正函数地方 public XLoadDesc realLoad(List <XLoadRes> reses, Action <XLoadDesc> cb) { // asset reses!= null // XLoad默认模块必须装载成功 if (!_isOK) { XDebug.LogError("XLoad模块并没有初始化成功就进行装载"); return(null); } // 不装载任何资源,默认模式算完成状态 XLoadDesc desc = new XLoadDesc { reses = reses }; fillDesc(desc); if (desc.isComplete) { if (cb != null) { cb(desc); } return(desc); } #if UNITY_EDITOR // 肯定是EDITOR状态 if (!isLoadFromAB) { // 因为是阻塞的,所以没其他问题 for (int i = 0; i < reses.Count; i++) { XAssetInfo assetInfo = reses[i].asset; if (!assetInfo.isComplete) { if (assetInfo.all) { assetInfo.obs = editorLoadAll(assetInfo.res, assetInfo.tp); assetInfo.status = assetInfo.obs == null ? XLoadStatus.FAIL : XLoadStatus.SUCESS; } else { assetInfo.ob = editorLoad(assetInfo.res, assetInfo.tp); assetInfo.status = assetInfo.obs == null ? XLoadStatus.FAIL : XLoadStatus.SUCESS; } } } desc._isComplete = true; if (cb != null) { cb(desc); } return(desc); } else #endif { // 开启协程整流程去装载 StartCoroutine(coLoad(desc, cb, false)); } return(desc); }