Exemplo n.º 1
0
        /// <summary>
        /// 取消异步加载资源
        /// </summary>
        /// <returns></returns>
        internal bool CancleLoad(ResouceObj res)
        {
            AsyncLoadResParam para = null;

            if (_LoadingAssetDic.TryGetValue(res.Crc, out para) &&
                _LoadingAssetList[(int)para.Priority].Contains(para))
            {
                for (int i = para.CallBackList.Count - 1; i >= 0; i--)
                {
                    AsyncCallBack tempCallBack = para.CallBackList[i];
                    if (tempCallBack != null && res == tempCallBack.ResObj)
                    {
                        para.CallBackList.Remove(tempCallBack);
                        tempCallBack.Reset();
                        _AsyncCallBackPool.Recycle(tempCallBack);
                    }
                }

                if (para.CallBackList.Count <= 0)
                {
                    _LoadingAssetList[(int)para.Priority].Remove(para);
                    para.Reset();
                    _AsyncLoadResParamPool.Recycle(para);

                    _LoadingAssetDic.Remove(res.Crc);
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 异步加载
        /// </summary>
        /// <returns></returns>
        IEnumerator AsyncLoadCor()
        {
            List <AsyncCallBack> callBackList = null;
            //上一次yield的时间
            long lastYieldTime = System.DateTime.Now.Ticks;

            while (true)
            {
                bool haveYield = false;
                for (int i = 0; i < (int)LoadResPriority.Res_Num; i++)
                {
                    if (_LoadingAssetList[(int)LoadResPriority.Res_Hight].Count > 0)
                    {
                        i = (int)LoadResPriority.Res_Hight;
                    }
                    else if (_LoadingAssetList[(int)LoadResPriority.Res_Middle].Count > 0)
                    {
                        i = (int)LoadResPriority.Res_Middle;
                    }

                    List <AsyncLoadResParam> loadingList = _LoadingAssetList[i];
                    if (loadingList.Count <= 0)
                    {
                        continue;
                    }

                    AsyncLoadResParam loadingItem = loadingList[0];
                    loadingList.RemoveAt(0);
                    callBackList = loadingItem.CallBackList;

                    Object  obj  = null;
                    ResItem item = null;
#if UNITY_EDITOR
                    if (IsEditorLoadMode)
                    {
                        if (item == null)
                        {
                            item     = new ResItem();
                            item.Crc = loadingItem.Crc;
                        }

                        if (loadingItem.LoadMode == ResKit.LoadResMode.Resource)
                        {
                            ResourceRequest resRequest = null;
                            if (loadingItem.ResType == typeof(Sprite))
                            {
                                resRequest = Resources.LoadAsync <Sprite>(loadingItem.Path);
                            }
                            else
                            {
                                resRequest = Resources.LoadAsync <Object>(loadingItem.Path);
                            }

                            yield return(resRequest);

                            if (resRequest.isDone)
                            {
                                obj = resRequest.asset;
                            }

                            lastYieldTime = System.DateTime.Now.Ticks;
                        }
                        else
                        {
                            if (loadingItem.ResType == typeof(Sprite))
                            {
                                obj = LoadAssetByEditor <Sprite>(loadingItem.Path);
                            }
                            else
                            {
                                obj = LoadAssetByEditor <Object>(loadingItem.Path);
                            }
                            //模拟异步加载
                            yield return(new WaitForSeconds(0.2f));
                        }
                    }
#endif
                    if (obj == null)
                    {
                        if (loadingItem.LoadMode == ResKit.LoadResMode.Resource)
                        {
                            if (item == null)
                            {
                                item     = new ResItem();
                                item.Crc = loadingItem.Crc;
                            }

                            ResourceRequest resRequest = null;
                            if (loadingItem.ResType == typeof(Sprite))
                            {
                                resRequest = Resources.LoadAsync <Sprite>(loadingItem.Path);
                            }
                            else
                            {
                                resRequest = Resources.LoadAsync <Object>(loadingItem.Path);
                            }

                            yield return(resRequest);

                            if (resRequest.isDone)
                            {
                                obj = resRequest.asset;
                            }

                            lastYieldTime = System.DateTime.Now.Ticks;
                        }
                        else
                        {
                            item = AssetBundleMgr.Instance.LoadResourceAssetBundle(loadingItem.Crc);
                            if (item != null && item.AssetBundle != null)
                            {
                                AssetBundleRequest abRequest = null;
                                if (loadingItem.ResType == typeof(Sprite))
                                {
                                    //是图片资源
                                    abRequest = item.AssetBundle.LoadAssetAsync <Sprite>(item.AssetName);
                                }
                                else
                                {
                                    //不是图片资源
                                    abRequest = item.AssetBundle.LoadAssetAsync(item.AssetName);
                                }

                                yield return(abRequest);

                                if (abRequest.isDone)
                                {
                                    obj = abRequest.asset;
                                }

                                lastYieldTime = System.DateTime.Now.Ticks;
                            }
                            else
                            {
                                Debug.LogErrorFormat("加载AssetBundle失败 : 不能从AssetBundleConfig中找到 Path :{0}", loadingItem.Path);
                            }
                        }
                    }

                    item.Crc  = loadingItem.Crc;
                    item.Obj  = obj;
                    item.Guid = obj.GetInstanceID();
                    CacheResource(loadingItem.Path, ref item, loadingItem.Crc, callBackList.Count);

                    for (int j = 0; j < callBackList.Count; j++)
                    {
                        AsyncCallBack callBack = callBackList[j];

                        if (callBack != null && callBack.OnFinish != null && callBack.ResObj != null)
                        {
                            ResouceObj tempResObj = callBack.ResObj;
                            tempResObj.ResItem = item;
                            callBack.OnFinish(loadingItem.Path, tempResObj, tempResObj.ParamValues);
                            callBack.OnFinish = null;
                            tempResObj        = null;
                        }

                        if (callBack != null && callBack.OnObjFinish != null)
                        {
                            callBack.OnObjFinish(loadingItem.Path, obj, callBack.ParamValues);
                            callBack.OnObjFinish = null;
                        }

                        callBack.Reset();
                        _AsyncCallBackPool.Recycle(callBack);
                    }

                    obj = null;
                    callBackList.Clear();
                    _LoadingAssetDic.Remove(loadingItem.Crc);

                    loadingItem.Reset();
                    _AsyncLoadResParamPool.Recycle(loadingItem);

                    if (System.DateTime.Now.Ticks - lastYieldTime > _MaxLoadResTime)
                    {
                        yield return(null);

                        lastYieldTime = System.DateTime.Now.Ticks;
                        haveYield     = true;
                    }
                }

                //大于 _MaxLoadResTime 时间才等待一帧
                if (!haveYield || System.DateTime.Now.Ticks - lastYieldTime > _MaxLoadResTime)
                {
                    lastYieldTime = System.DateTime.Now.Ticks;
                    yield return(null);
                }
            }
        }