Exemple #1
0
        /// <summary>
        /// 针对ObjectManager的异步加载接口
        /// </summary>
        /// <param name="path"></param>
        /// <param name="resObj"></param>
        /// <param name="dealfinish"></param>
        /// <param name="priority"></param>
        public void AsyncLoadResource(string path, ResouceObj resObj, OnAsyncFinsih dealfinish, LoadResPriority priority)
        {
            ResouceItem item = GetCacheResouceItem(resObj.m_Crc);

            if (item != null)
            {
                resObj.m_ResItem = item;
                if (dealfinish != null)
                {
                    dealfinish(path, resObj);
                }
                return;
            }

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

            if (!m_LoadingAssetDic.TryGetValue(resObj.m_Crc, out para) || para == null)
            {
                para            = m_AsyncLoadResParamPool.Spawn(true);
                para.m_Crc      = resObj.m_Crc;
                para.m_Path     = path;
                para.m_Priority = priority;
                m_LoadingAssetDic.Add(resObj.m_Crc, para);
                m_LoadingAssetList[(int)priority].Add(para);
            }

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

            callBack.m_DealFinish = dealfinish;
            callBack.m_ResObj     = resObj;
            para.m_CallBackList.Add(callBack);
        }
Exemple #2
0
        /// <summary>
        /// 取消异步加载资源
        /// </summary>
        /// <returns></returns>
        public bool CancleLoad(ResouceObj res)
        {
            AsyncLoadResParam para = null;

            if (m_LoadingAssetDic.TryGetValue(res.m_Crc, out para) && m_LoadingAssetList[(int)para.m_Priority].Contains(para))
            {
                for (int i = para.m_CallBackList.Count; i >= 0; i--)
                {
                    AsyncCallBack tempCallBack = para.m_CallBackList[i];
                    if (tempCallBack != null && res == tempCallBack.m_ResObj)
                    {
                        tempCallBack.Reset();
                        m_AsyncCallBackPool.Recycle(tempCallBack);
                        para.m_CallBackList.Remove(tempCallBack);
                    }
                }

                if (para.m_CallBackList.Count <= 0)
                {
                    para.Reset();
                    m_LoadingAssetList[(int)para.m_Priority].Remove(para);
                    m_AsyncLoadResParamPool.Recycle(para);
                    m_LoadingAssetDic.Remove(res.m_Crc);
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
        /// <summary>
        /// 异步加载资源(仅仅是不需要实例化的资源,例如音频,图片等等)
        /// </summary>
        public void AsyncLoadResource(string path, OnAsyncObjFinish dealFinish, LoadResPriority priority, bool isSprite = false, object param1 = null, object param2 = null, object param3 = null, uint crc = 0)
        {
            if (crc == 0)
            {
                crc = CRC32.GetCrc32(path);
            }

            ResouceItem item = GetCacheResouceItem(crc);

            if (item != null)
            {
                if (dealFinish != null)
                {
                    dealFinish(path, item.m_Obj, param1, param2, param3);
                }
                return;
            }

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

            if (!m_LoadingAssetDic.TryGetValue(crc, out para) || para == null)
            {
                para            = m_AsyncLoadResParamPool.Spawn(true);
                para.m_Crc      = crc;
                para.m_Path     = path;
                para.m_Sprite   = isSprite;
                para.m_Priority = priority;
                m_LoadingAssetDic.Add(crc, para);
                m_LoadingAssetList[(int)priority].Add(para);
            }

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

            callBack.m_DealObjFinish = dealFinish;
            callBack.m_Param1        = param1;
            callBack.m_Param2        = param2;
            callBack.m_Param3        = param3;
            para.m_CallBackList.Add(callBack);
        }
Exemple #4
0
        /// <summary>
        /// 异步加载
        /// </summary>
        /// <returns></returns>
        IEnumerator AsyncLoadCor()
        {
            List <AsyncCallBack> callBackList = null;
            //上一次yield的视角
            long lastYiledTime = System.DateTime.Now.Ticks;

            while (true)
            {
                bool haveYield = false;
                for (int i = 0; i < (int)LoadResPriority.RES_NUM; i++)
                {
                    if (m_LoadingAssetList[(int)LoadResPriority.RES_HIGHT].Count > 0)
                    {
                        i = (int)LoadResPriority.RES_HIGHT;
                    }
                    else if (m_LoadingAssetList[(int)LoadResPriority.RES_MIDDLE].Count > 0)
                    {
                        i = (int)LoadResPriority.RES_MIDDLE;
                    }

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

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

                    Object      obj  = null;
                    ResouceItem item = null;
#if UNITY_EDITOR
                    if (!m_LoadFormAssetBundle)
                    {
                        if (loadingItem.m_Sprite)
                        {
                            obj = LoadAssetByEditor <Sprite>(loadingItem.m_Path);
                        }
                        else
                        {
                            obj = LoadAssetByEditor <Object>(loadingItem.m_Path);
                        }
                        //模拟异步加载
                        yield return(new WaitForEndOfFrame());

                        item = AssetBundleManager.Instance.FindResourceItme(loadingItem.m_Crc);
                        if (item == null)
                        {
                            item       = new ResouceItem();
                            item.m_Crc = loadingItem.m_Crc;
                        }
                    }
#endif
                    if (obj == null)
                    {
                        item = AssetBundleManager.Instance.LoadResouceAssetBundle(loadingItem.m_Crc);
                        if (item != null && item.m_AssetBundle != null)
                        {
                            AssetBundleRequest abRequest = null;
                            if (loadingItem.m_Sprite)
                            {
                                abRequest = item.m_AssetBundle.LoadAssetAsync <Sprite>(item.m_AssetName);
                            }
                            else
                            {
                                abRequest = item.m_AssetBundle.LoadAssetAsync(item.m_AssetName);
                            }
                            yield return(abRequest);

                            if (abRequest.isDone)
                            {
                                obj = abRequest.asset;
                            }
                            lastYiledTime = System.DateTime.Now.Ticks;
                        }
                    }

                    CacheResource(loadingItem.m_Path, ref item, loadingItem.m_Crc, obj, callBackList.Count);

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

                        if (callBack != null && callBack.m_DealFinish != null && callBack.m_ResObj != null)
                        {
                            ResouceObj tempResObj = callBack.m_ResObj;
                            tempResObj.m_ResItem = item;
                            callBack.m_DealFinish(loadingItem.m_Path, tempResObj, tempResObj.m_Param1, tempResObj.m_Param2, tempResObj.m_Param3);
                            callBack.m_DealFinish = null;
                            tempResObj            = null;
                        }

                        if (callBack != null && callBack.m_DealObjFinish != null)
                        {
                            callBack.m_DealObjFinish(loadingItem.m_Path, obj, callBack.m_Param1, callBack.m_Param2, callBack.m_Param3);
                            callBack.m_DealObjFinish = null;
                        }

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

                    obj = null;
                    callBackList.Clear();
                    m_LoadingAssetDic.Remove(loadingItem.m_Crc);

                    loadingItem.Reset();
                    m_AsyncLoadResParamPool.Recycle(loadingItem);

                    if (System.DateTime.Now.Ticks - lastYiledTime > MAXLOADRESTIME)
                    {
                        yield return(null);

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

                if (!haveYield || System.DateTime.Now.Ticks - lastYiledTime > MAXLOADRESTIME)
                {
                    lastYiledTime = System.DateTime.Now.Ticks;
                    yield return(null);
                }
            }
        }