Exemplo n.º 1
0
    /// <summary>
    /// 异步加载
    /// </summary>
    IEnumerator AsyncLoader()
    {
        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++)
            {
                List <AsyncLoadResParam> loadingList = _loadingAssetList[i];
                if (loadingList.Count <= 0)
                {
                    continue;
                }
                AsyncLoadResParam loadingItem = loadingList[0];
                loadingList.RemoveAt(0);
                callBackList = loadingItem.callBackList;

                Object       obj  = null;
                ResourceItem item = null;
#if UNITY_EDITOR
                if (!_loadFromAssetBundle)
                {
                    obj = LoadAssetByEditor <Object>(loadingItem.path);
                    //模拟异步加载
                    yield return(new WaitForSeconds(0.5f));

                    item = ABMgr.Ins.FingResourceItem(loadingItem.crc);
                }
#endif
                if (obj == null)
                {
                    item = ABMgr.Ins.LoadResAssetBundle(loadingItem.crc);
                    if (item != null && item._assetBundle != null)
                    {
                        AssetBundleRequest abRequest = null;
                        if (loadingItem.isSprite)
                        {
                            abRequest = item._assetBundle.LoadAssetAsync <Sprite>(item._assetName);
                        }
                        else
                        {
                            abRequest = item._assetBundle.LoadAssetAsync(item._assetName);
                        }
                        yield return(abRequest);

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

                CacheResource(loadingItem.path, ref item, loadingItem.crc, obj, callBackList.Count);

                for (int j = 0; j < callBackList.Count; j++)
                {
                    AsyncCallBack callBack = callBackList[i];
                    if (callBack != null && callBack.dealFinish != null)
                    {
                        callBack.dealFinish(loadingItem.path, obj, callBack.param1, callBack.param2, callBack.param3);
                        callBack.dealFinish = null;
                    }
                    callBack.Reset();
                    _asyncCallBackPool.Recycle(callBack);
                }

                obj = null;
                callBackList.Clear();
                _loadingAssetDic.Remove(loadingItem.crc);
                loadingItem.Reset();
                _asyncLoadResParamPool.Recycle(loadingItem);

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

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