//第二种方案:协程挂起加载 private T LoadAssetsAsyncByIEnumerator <T>(string sPath, ResType rt, Action <T> fun, bool bInstantiate = false) where T : Object { string sName = GetAssetName(sPath); //正在被加载 还没加载完成 foreach (AssetInfo item in _listLoading) { if (item.SName == sName) { Action <Object> action = o => fun((T)o); item.AddListener(action, bInstantiate); return(null); } } AssetInfo ai = AssetPool.AssetGet(sName, sPath, (int)rt); if (ai != null) { var obj = (T)ai.Obj; return(obj); } else { //都没有 先创建 AssetInfo asset = new AssetInfo(sName, sPath, (int)rt, null); Action <Object> action = o => fun((T)o); //T 转为Object类型 asset.AddListener(action, bInstantiate); //添加回调 _dicLoading.Add(sName, asset); //缓存 StartCoroutine(IeLoadAsync(asset)); //挂起 return(null); } }
//第一种方案:添加到异步加载队列中 private T LoadAssetsAsyncByUpdate <T>(string sPath, ResType rt, Action <T> fun, bool bInstantiate = false) where T : Object { string sName = GetAssetName(sPath); //正在被加载 还没加载完成 foreach (AssetInfo item in _listLoading) { if (item.SName == sName) { Action <Object> action = o => fun((T)o); item.AddListener(action, bInstantiate); return(null); } } //等待的队列里面有 foreach (AssetInfo item in _queWaitLoad) { if (item.SName == sName) { Action <Object> action = o => fun((T)o); item.AddListener(action, bInstantiate); return(null); } } AssetInfo ai = AssetPool.AssetGet(sName, sPath, (int)rt); if (ai != null) { var obj = (T)ai.Obj; return(obj); } else { //都没有 先创建 AssetInfo asset = new AssetInfo(sName, sPath, (int)rt, null); //T 转为Object类型 Action <Object> action = o => fun((T)o); asset.AddListener(action, bInstantiate); //添加到等待队列 _queWaitLoad.Enqueue(asset); return(null); } }