static public int get_isDone(IntPtr l) { try { mg.org.AssetData self = (mg.org.AssetData)checkSelf(l); pushValue(l, true); pushValue(l, self.isDone); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int set_asset(IntPtr l) { try { mg.org.AssetData self = (mg.org.AssetData)checkSelf(l); UnityEngine.Object v; checkType(l, 2, out v); self.asset = v; pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int set_url(IntPtr l) { try { mg.org.AssetData self = (mg.org.AssetData)checkSelf(l); System.String v; checkType(l, 2, out v); self.url = v; pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
void OnLoaded(object data_) { AssetData data = data_ as AssetData; if (m_itemList.Count > 0) { LoadItem item; for (int i = m_itemList.Count - 1; i >= 0; --i) { item = m_itemList[i]; if (item.url == data.url) { CreateGo(item.url, item.onComplete); RemoveLoadItem(item); } } } }
//创建数据 protected virtual AssetData CreateData(string url_) { url_ = url_.ToLower(); AssetData data; if (m_url2data.TryGetValue(url_, out data)) { return(data); } data = new AssetData(); data.Init(url_); data.active_time = DateUtil.TimeFromStart; m_url2data[url_] = data; return(data); }
//卸载数据 protected virtual void UnloadData(AssetData data_) { if (data_.RefCount > 0) { Log.Warn("要卸载的资源还在被引用: " + data_.url, this); } string url = data_.url; Object asset = data_.asset; data_.Clear(); DeleteData(data_); if (asset) { //资源已加载 UnloadAsset(url, asset); } }
/// <summary> /// 异步加载 /// </summary> /// <param name="refer_"></param> /// <param name="url_"></param> /// <param name="onComplete_"></param> /// <returns></returns> override public AssetData LoadAsync(string url_, CALLBACK_1 onComplete_, object refer_) { url_ = url_.ToLower(); AssetData data = CreateData(url_); //先创建data, 用以记录生命周期等 data.Retain(refer_); if (data.asset != null) { //加载完成 if (onComplete_ != null) { onComplete_(data); } return(data); } if (m_url2req.ContainsKey(data.url)) { //加载中 if (onComplete_ != null) { Attach(data.url, onComplete_, refer_); } return(data); } //未启动加载, 至少保持一个引用 data.Retain(this); if (onComplete_ != null) { Attach(data.url, onComplete_, refer_); } CCApp.StartCoroutine(__LoadAsync(data.url)); return(data); }
//获取加载进度 internal override float __GetProgress(string url_) { AssetData data = GetData(url_); if (data == null) { return(0); } if (m_url2req.ContainsKey(url_)) { return(m_url2req[url_].progress); } if (data.asset != null) { return(1); } return(0); }
void OnLoaded(object data_) { AssetData data = data_ as AssetData; if (m_itemList.Count > 0) { LoadItem item; for (int i = m_itemList.Count - 1; i >= 0; --i) { item = m_itemList[i]; if (item.url == data.url) { AudioSource audio = item.audio; string url = item.url; SoundPlayType playType = item.playType; float volume = item.volume; RemoveLoadItem(item); PlaySound(audio, url, playType, volume); } } } }
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_); }
//-------∽-★-∽------∽-★-∽--------∽-★-∽数据管理∽-★-∽--------∽-★-∽------∽-★-∽--------// //删除一个data protected virtual void DeleteData(AssetData data_) { //m_dataPool.Push(data_); }