private T LoadAssetsSync <T>(string sPath, ResType rt) where T : Object { string sName = GetAssetName(sPath); AssetInfo ai = AssetPool.AssetGet(sName, sPath, (int)rt); if (ai != null) { return((T)ai.Obj); } ai = new AssetInfo(sName, sPath, (int)rt, null); T asset = ai.LoadSync <T>(sPath); AssetPool.AssetCache(sName, sPath, (int)rt, asset); return(asset); }
public override void DoUpdate() { base.DoUpdate(); #region 第一种方案:添加进入循环加载队列,等待加载完毕后调用 if (_listLoading.Count > 0) { for (int i = 0; i < _listLoading.Count; i++) { AssetInfo asset = _listLoading[i]; if (asset.Request == null) { throw new Exception("回调异常!未正常创建ResourceRequest"); } else { if (asset.Request.isDone) { asset.Obj = asset.Request.asset; if (asset.Obj == null) { _listLoading.RemoveAt(i); throw new Exception("加载资源<" + asset.SPath + ">异常!"); } else { if (asset.ListListener != null) { for (int j = 0; j < asset.ListListener.Count; j++) { if (asset.ListListener[j] != null) { if (asset.BInstantiate) { GameObject obj = Object.Instantiate((GameObject)asset.Obj); asset.ListListener[j](obj); } else { asset.ListListener[j](asset.Obj); } } else { _listLoading.RemoveAt(i); throw new Exception("加载资源<" + asset.SPath + ">后,回调异常!"); } } AssetPool.AssetCache(asset.SName, asset.SPath, asset.NAssetType, asset.Obj); _listLoading.RemoveAt(i); } else { _listLoading.RemoveAt(i); throw new Exception("加载资源<" + asset.SPath + ">后,无回调类型!"); } } } } } } while (_queWaitLoad.Count > 0 && _listLoading.Count < 5) { AssetInfo asset = _queWaitLoad.Dequeue(); _listLoading.Add(asset); asset.LoadAsync(asset.SPath); } #endregion }
private IEnumerator IeLoadAsync(AssetInfo asset) { //发起异步加载请求 ResourceRequest request = asset.LoadAsync(asset.SPath); if (request == null) { throw new Exception("回调异常!未正常创建ResourceRequest"); } else { //挂起等待加载完毕 yield return(request); //加载完成,判断下 if (request.isDone) { asset.Obj = request.asset; if (asset.Obj == null) { if (_dicLoading.ContainsKey(asset.SName)) { _dicLoading.Remove(asset.SName); } throw new Exception("加载资源<" + asset.SPath + ">异常!资源未成功加载!"); } else { if (asset.ListListener != null) { for (int j = 0; j < asset.ListListener.Count; j++) { if (asset.ListListener[j] != null) { if (asset.BInstantiate) { GameObject obj = Object.Instantiate((GameObject)asset.Obj); asset.ListListener[j](obj); } else { asset.ListListener[j](asset.Obj); } } else { if (_dicLoading.ContainsKey(asset.SName)) { _dicLoading.Remove(asset.SName); } throw new Exception("加载资源<" + asset.SPath + ">后,回调异常!"); } } AssetPool.AssetCache(asset.SName, asset.SPath, asset.NAssetType, asset.Obj); if (_dicLoading.ContainsKey(asset.SName)) { _dicLoading.Remove(asset.SName); } } else { if (_dicLoading.ContainsKey(asset.SName)) { _dicLoading.Remove(asset.SName); } throw new Exception("加载资源<" + asset.SPath + ">后,无回调类型!"); } } } else { if (_dicLoading.ContainsKey(asset.SName)) { _dicLoading.Remove(asset.SName); } throw new Exception("加载资源<" + asset.SPath + ">未完成!"); } } }