Esempio 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);
        }
Esempio n. 2
0
        /// <summary>
        /// 异步加载资源 (仅仅是不需要实例化的资源)
        /// </summary>
        private void AsyncLoadResource <T>(string path, OnAsyncObjFinish onFinish, uint crc = 0, ResLoadMode mode = ResLoadMode.Null,
                                           LoadResPriority priority = LoadResPriority.Res_Slow, params object[] paramValues)
            where T : UnityEngine.Object
        {
            if (mode == ResLoadMode.Null)
            {
                mode = LoadMode(ref path);
            }
            if (crc == 0)
            {
                crc = Crc32.GetCrc32(path);
            }

            ResItem item = GetCacheResouceItem(crc);

            if (item != null)
            {
                if (onFinish != null)
                {
                    onFinish(path, item.Obj, paramValues);
                }

                return;
            }

            //判断是否在加载中
            AsyncLoadResParam param = null;

            if (!_LoadingAssetDic.TryGetValue(crc, out param) || param == null)
            {
                param          = _AsyncLoadResParamPool.Spawn(true);
                param.Crc      = crc;
                param.Path     = path;
                param.Priority = priority;
                param.ResType  = typeof(T);
                param.LoadMode = JudgeLoadMode(mode);
                _LoadingAssetDic.Add(crc, param);
                _LoadingAssetList[(int)priority].Add(param);
            }

            //回调列表里面加回调
            AsyncCallBack callBack = _AsyncCallBackPool.Spawn(true);

            callBack.OnObjFinish = onFinish;
            if (paramValues != null && paramValues.Length > 0)
            {
                foreach (object value in paramValues)
                {
                    callBack.Params.Add(value);
                }
            }

            param.CallBackList.Add(callBack);
        }
Esempio n. 3
0
        /// <summary>
        /// 针对ObjectManager的异步加载接口
        /// </summary>
        /// <param UIName="path"></param>
        /// <param UIName="resObj"></param>
        /// <param UIName="dealFinish"></param>
        /// <param UIName="priority"></param>
        internal void AsyncLoadResource(string path, ResLoadMode mode, ResouceObj resObj, OnAsyncFinish dealFinish,
                                        LoadResPriority priority)
        {
            ResItem item = GetCacheResouceItem(resObj.Crc);

            if (item != null)
            {
                resObj.ResItem = item;
                if (dealFinish != null)
                {
                    dealFinish(path, resObj);
                }
                return;
            }

            //判断是否在加载中
            AsyncLoadResParam param = null;

            if (!_LoadingAssetDic.TryGetValue(resObj.Crc, out param) || param == null)
            {
                param     = _AsyncLoadResParamPool.Spawn(true);
                param.Crc = resObj.Crc;
                if (mode == ResLoadMode.Ab)
                {
                    param.LoadMode = LoadResMode.AssetBundle;
                }
                else
                {
                    param.LoadMode = LoadResMode.Resource;
                }
                param.Path     = path;
                param.Priority = priority;
                _LoadingAssetDic.Add(resObj.Crc, param);
                _LoadingAssetList[(int)priority].Add(param);
            }

            //回调列表里面加回调
            AsyncCallBack callBack = _AsyncCallBackPool.Spawn(true);

            callBack.OnFinish = dealFinish;
            callBack.ResObj   = resObj;
            param.CallBackList.Add(callBack);
        }
Esempio n. 4
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);
                }
            }
        }