Ejemplo n.º 1
0
        /// <summary>
        /// 取消异步加载资源
        /// </summary>
        /// <returns></returns>
        public bool CancleAsyncLoad(MResourceObjectItem mResourceObjectItem)
        {
            AsyncLoadResParam asyncLoadResParam = null;

            if (m_asyncLoadingAssetDic.TryGetValue(mResourceObjectItem.m_crc, out asyncLoadResParam)
                //异步进行中有取的操作 所以有这个判断
                && m_asyncAssetLoadingList[(int)asyncLoadResParam.m_loadResPriority].Contains(asyncLoadResParam))
            {
                for (int i = asyncLoadResParam.m_asyncCallBacks.Count - 1; i >= 0; i--)
                {
                    AsyncCallBack asyncCallBack = asyncLoadResParam.m_asyncCallBacks[i];
                    if (asyncCallBack != null &&
                        mResourceObjectItem == asyncCallBack.m_resourceObjectItem)
                    {
                        asyncCallBack.Reset();
                        m_asyncCallBackPool.Recycle(asyncCallBack);
                        asyncLoadResParam.m_asyncCallBacks.Remove(asyncCallBack);
                    }
                }
                //如果中间类中已经没有回调类了 那么回收中间类
                if (asyncLoadResParam.m_asyncCallBacks.Count < 0)
                {
                    asyncLoadResParam.Reset();
                    m_asyncAssetLoadingList[(int)asyncLoadResParam.m_loadResPriority].Remove(asyncLoadResParam);
                    m_asyncLoadResParamPool.Recycle(asyncLoadResParam);
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 异步加载资源 仅加载不需要实例化的资源 音频 图片等等
        /// </summary>
        public void AsyncLoadResource(string resPath, OnAsyncLoadFinished onAsyncLoadFinished, LoadResPriority loadResPriority, object[] parms = null)
        {
            if (string.IsNullOrEmpty(resPath))
            {
                return;
            }

            uint          crc           = MCrcHelper.GetCRC32(resPath);
            MResourceItem mResourceItem = GetCacheResourceItem(crc);

            if (mResourceItem != null && mResourceItem.m_object != null)
            {
                if (onAsyncLoadFinished != null)
                {
                    onAsyncLoadFinished(resPath, mResourceItem.m_object, parms);
                }
                return;
            }

            //判断下对象是不是在加载中
            AsyncLoadResParam asyncLoadResParam = null;

            if (!m_asyncLoadingAssetDic.TryGetValue(crc, out asyncLoadResParam) || asyncLoadResParam == null)
            {
                asyncLoadResParam                   = m_asyncLoadResParamPool.Spawn(true);
                asyncLoadResParam.m_crc             = crc;
                asyncLoadResParam.m_resPath         = resPath;
                asyncLoadResParam.m_loadResPriority = loadResPriority;
                //结果保存
                m_asyncAssetLoadingList[(int)loadResPriority].Add(asyncLoadResParam);
                m_asyncLoadingAssetDic.Add(crc, asyncLoadResParam);
            }

            //添加回调
            AsyncCallBack m_asyncCallBack = m_asyncCallBackPool.Spawn(true);

            m_asyncCallBack.m_onAsyncLoadFinished = onAsyncLoadFinished;
            m_asyncCallBack.m_parms = parms;
            asyncLoadResParam.m_asyncCallBacks.Add(m_asyncCallBack);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 异步加载的携程
        /// </summary>
        /// <returns></returns>
        IEnumerator AsyncLoader()
        {
            List <AsyncCallBack> callBackList;
            //用于记录上次的加载时间
            long lastReturnTime = System.DateTime.Now.Ticks;

            while (true)
            {
                //标志位 用于判读在For循环中是否已经return过了
                bool isYieldReturn = false;

                for (int i = 0; i < (int)LoadResPriority.RES_LOAD_LEVEL_COUNT; i++)
                {
                    List <AsyncLoadResParam> cAsyncLoadResList = m_asyncAssetLoadingList[i];
                    if (m_asyncAssetLoadingList[i] == null)
                    {
                        continue;
                    }
                    if (cAsyncLoadResList.Count <= 0)
                    {
                        continue;
                    }

                    AsyncLoadResParam asyncLoadResParam = cAsyncLoadResList[0];
                    cAsyncLoadResList.RemoveAt(0);
                    callBackList = asyncLoadResParam.m_asyncCallBacks;

                    Object        obj           = null;
                    MResourceItem mResourceItem = null;
#if UNITY_EDITOR
                    if (!m_isLoadFormAssetBundle)
                    {
                        obj = LoadAssetFormEditor <Object>(asyncLoadResParam.m_resPath);
                        //模拟异步
                        yield return(new WaitForSeconds(0.2f));

                        mResourceItem = MAssetBundleManager.singleton.FindResourceItem(asyncLoadResParam.m_crc);
                        if (mResourceItem == null)
                        {
                            mResourceItem       = new MResourceItem();
                            mResourceItem.m_crc = asyncLoadResParam.m_crc;
                        }
                    }
#endif
                    if (obj == null)
                    {
                        mResourceItem = MAssetBundleManager.singleton.LoadResourcesAssetBundle(asyncLoadResParam.m_crc);
                        if (mResourceItem != null && mResourceItem.m_assetBundle != null)
                        {
                            AssetBundleRequest assetBundleRequest = null;
                            if (asyncLoadResParam.m_isSprite)
                            {
                                assetBundleRequest = mResourceItem.m_assetBundle.LoadAssetAsync <Sprite>(mResourceItem.m_assetName);
                            }
                            else
                            {
                                assetBundleRequest = mResourceItem.m_assetBundle.LoadAssetAsync(mResourceItem.m_assetName);
                            }
                            yield return(assetBundleRequest);

                            if (assetBundleRequest.isDone)
                            {
                                obj = assetBundleRequest.asset;
                            }
                            lastReturnTime = System.DateTime.Now.Ticks;
                        }
                    }
                    //资源缓存
                    CacheResource(asyncLoadResParam.m_resPath, ref mResourceItem, asyncLoadResParam.m_crc, obj, callBackList.Count);
                    //处理加载完成的回调
                    for (int z = 0; z < callBackList.Count; z++)
                    {
                        AsyncCallBack callBack = callBackList[z];

                        //------------------------异步加载处理需要实例化的资源----------------------------
                        if (callBack != null && callBack.m_onAsyncLoadObjectFinished != null && callBack.m_resourceObjectItem != null)
                        {
                            MResourceObjectItem mResourceObjectItem = callBack.m_resourceObjectItem;
                            callBack.m_resourceObjectItem.m_resItem = mResourceItem;
                            callBack.m_onAsyncLoadObjectFinished(asyncLoadResParam.m_resPath, callBack.m_resourceObjectItem, callBack.m_resourceObjectItem.m_parms);
                            callBack.m_onAsyncLoadObjectFinished = null;
                        }

                        //------------------------异步加载处理不需要实例化的Object资源----------------------------
                        if (callBack != null && callBack.m_onAsyncLoadFinished != null)
                        {
                            callBack.m_onAsyncLoadFinished(asyncLoadResParam.m_resPath, obj, callBack.m_parms);
                            callBack.m_onAsyncLoadFinished = null;
                        }
                        //异步加载CallBack对象回收
                        callBack.Reset();
                        m_asyncCallBackPool.Recycle(callBack);
                    }

                    obj = null;
                    callBackList.Clear();
                    //从正在异步加载的Dic里面移除
                    m_asyncLoadingAssetDic.Remove(mResourceItem.m_crc);

                    //异步加载中间对象回收
                    asyncLoadResParam.Reset();
                    m_asyncLoadResParamPool.Recycle(asyncLoadResParam);

                    //上下写了两个同样的Return逻辑 是因为如果加载比较大的资源 可能在这个For循环内就需要多帧 所以这里也做了一个判定
                    if (!isYieldReturn || System.DateTime.Now.Ticks - lastReturnTime > MAX_WAITTIME)
                    {
                        lastReturnTime = System.DateTime.Now.Ticks;
                        yield return(null);

                        isYieldReturn = true;
                    }
                }

                if (!isYieldReturn || System.DateTime.Now.Ticks - lastReturnTime > MAX_WAITTIME)
                {
                    lastReturnTime = System.DateTime.Now.Ticks;
                    yield return(null);
                }
            }
        }