IEnumerator __LoadAsync(string url_) { ResourceRequest req; if (m_url2req.ContainsKey(url_)) { Log.Assert("[LoadAsync] 错误的启动: " + url_, this); req = m_url2req[url_]; } else { req = Resources.LoadAsync(url_); } m_url2req[url_] = req; Log.Info("☆ load async start: " + url_, this); while (!req.isDone) { yield return(0); //yield return new WaitForEndOfFrame(); } if (req.asset) { OnAssetLoaded(url_, req.asset); } AssetData data = GetData(url_); if (data != null) { if (req.asset) { AddAsset(url_, req.asset); Log.Info("★ load async success: " + url_, this); Notify(url_, data); //这里实际上是调用完成回调 //Notify(RES_EVT.LOAD_COMPLETE, data); //派发完成事件 LuaEvtCenter.AddEvent(RES_EVT.LOAD_COMPLETE, data); } else { Log.Assert("x 异步加载失败: " + url_, this); //Notify(url_, data); //加载失败了, 也会调用完成回调 // Notify(RES_EVT.LOAD_EXCEPTION, url_); //派发异常事件 LuaEvtCenter.AddEvent(RES_EVT.LOAD_EXCEPTION, url_); } data.Release(this); //释放之前的引用 } else { Log.Warn("加载完成但data已被卸载:" + url_, this); if (req.asset) { UnloadAsset(url_, req.asset); } //Notify(RES_EVT.LOAD_EXCEPTION, url_); //派发异常事件 LuaEvtCenter.AddEvent(RES_EVT.LOAD_EXCEPTION, url_); } m_url2req.Remove(url_); DetachByType(url_); //移除所有完成回调 }
IEnumerator __LoadAsync(LevelData data) { string url_ = data.url; bool isAdditive = data.isAdditive; LoadSceneMode mode = isAdditive ? LoadSceneMode.Additive : LoadSceneMode.Single; AsyncOperation op; float progress = 0f; float weight = 0.1f; if (!isAdditive) { op = SceneManager.LoadSceneAsync(LEVEL_NAME_EMPTY, mode); while (!op.isDone) { data._progress = progress + weight * op.progress; yield return(null); } //Log.Debug("空场景加载完成:" + LEVEL_NAME_EMPTY, this); yield return(new WaitForSeconds(0.1f)); //等一会儿 } progress += weight; weight = 0.2f; AssetData assetData = AssetCache.me.LoadAsync_Level(url_, null, this); //先加载此场景所需的资源 if (assetData != null) { while (!assetData.isDone) { data._progress = progress + weight * assetData.progress; yield return(null); } } progress += weight; weight = 0.7f; op = SceneManager.LoadSceneAsync(data.url, mode); if (op == null) { //Log.Assert("x level load fail: " + url_, this); CCApp.StopCoroutine(data.enumerator); data.enumerator = null; LuaEvtCenter.AddEvent(RES_EVT.LOAD_LEVEL_EXCEPTION, data.url); yield return(0); } Log.Info("☆ load level async start: " + url_, this); while (!op.isDone) { data._progress = progress + weight * op.progress; yield return(null); } Log.Info("★ load level async success: " + url_, this); LevelData data2 = GetData(url_); if (data2 != null) { if (data == data2) { data.__OnComplete(); if (!isAdditive) { TransSceneFinsh(); } Notify(url_, data); LuaEvtCenter.AddEvent(RES_EVT.LOAD_LEVEL_COMPLETE, data); } else { //启动了异步, 同时又启动同步 data.__OnComplete(); Notify(url_, data); LuaEvtCenter.AddEvent(RES_EVT.LOAD_LEVEL_COMPLETE, data); } } else { //已销毁 } data.enumerator = null; DetachByType(url_); }