Ejemplo n.º 1
0
        /// <summary>
        /// ->objecmanager
        /// </summary>
        /// <param name="path"></param>
        /// <param name="objectItem"></param>
        /// <param name="innerCallBack"></param>
        /// <param name="priority"></param>
        public void AsyncLoadResource(string path, ObjectItem objectItem, OnAsyncPrimitiveAssetFinish innerCallBack,
                                      LoadResPriority priority, bool isSprite = false)
        {
            AssetItem assetItem = GetCacheAssetItem(objectItem.Crc);

            if (assetItem != null)
            {
                objectItem.PrimitiveAssetItem = assetItem;
                innerCallBack?.Invoke(path, objectItem);
                return;
            }

            AsyncLoadAssetParam asyncLoadAssetParam = null;

            if (!asyncLoadAssetParamDic.TryGetValue(objectItem.Crc, out asyncLoadAssetParam))
            {
                asyncLoadAssetParam          = asyncLoadResParamPackPool.Spawn();
                asyncLoadAssetParam.Crc      = objectItem.Crc;
                asyncLoadAssetParam.Path     = path;
                asyncLoadAssetParam.isSprite = isSprite;
                asyncLoadAssetParam.Priority = priority;
                asyncLoadAssetParamDic.Add(objectItem.Crc, asyncLoadAssetParam);
                loadingAssetLists[(int)priority].Add(asyncLoadAssetParam);
            }

            AsyncCallBackPack callBackPack = asyncCallBackPackPool.Spawn();

            callBackPack.PrimitiveAssetFinish = innerCallBack;
            callBackPack.ObjectItem           = objectItem;
            asyncLoadAssetParam.CallBackPacks.Add(callBackPack);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 取消异步加载asset,先清空回收回调列表,在从列表和字典里移除后回收
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public bool CancelAsyncLoad(ObjectItem objectItem)
        {
            AsyncLoadAssetParam asyncParam = null;

            if (asyncLoadAssetParamDic.TryGetValue(objectItem.Crc, out asyncParam) &&
                loadingAssetLists[(int)asyncParam.Priority].Contains(asyncParam))
            {
                for (int i = asyncParam.CallBackPacks.Count; i > 0; i--)
                {
                    AsyncCallBackPack asyncCallbackPack = asyncParam.CallBackPacks[i];
                    //判定objecitem取消
                    if (asyncCallbackPack != null && asyncCallbackPack.ObjectItem == objectItem)
                    {
                        asyncCallbackPack.Reset();
                        asyncCallBackPackPool.Recycle(asyncCallbackPack);
                        asyncParam.CallBackPacks.Remove(asyncCallbackPack);
                    }
                }

                if (asyncParam.CallBackPacks.Count <= 0)
                {
                    loadingAssetLists[(int)asyncParam.Priority].Remove(asyncParam);
                    asyncLoadAssetParamDic.Remove(objectItem.Crc);
                    asyncParam.Reset();
                    asyncLoadResParamPackPool.Recycle(asyncParam);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        public void AsyncLoadResource(string path, OnAsyncFinish cb, LoadResPriority priority, bool isSprite = false,
                                      uint crc = 0,
                                      params object[] paramList)
        {
            if (crc == 0)
            {
                crc = CRC32.GetCRC32(path);
            }

            AssetItem item = GetCacheAssetItem(crc);

            if (item != null)
            {
                cb?.Invoke(path, item.AssetObject, paramList);
                return;
            }

            AsyncLoadAssetParam asyncLoadAssetParam = null;

            if (!asyncLoadAssetParamDic.TryGetValue(crc, out asyncLoadAssetParam))
            {
                asyncLoadAssetParam          = asyncLoadResParamPackPool.Spawn();
                asyncLoadAssetParam.Crc      = crc;
                asyncLoadAssetParam.Path     = path;
                asyncLoadAssetParam.isSprite = isSprite;
                asyncLoadAssetParam.Priority = priority;
                asyncLoadAssetParamDic.Add(crc, asyncLoadAssetParam);
                loadingAssetLists[(int)priority].Add(asyncLoadAssetParam);
            }

            AsyncCallBackPack callBackPack = asyncCallBackPackPool.Spawn();

            callBackPack.AssetFinish = cb;
            callBackPack.paramList   = paramList;
            asyncLoadAssetParam.CallBackPacks.Add(callBackPack);
        }
Ejemplo n.º 4
0
        IEnumerator AsyncLoadCycleCoroutine()
        {
            List <AsyncCallBackPack> asyncCallBackPacks = null;
            long lastYieldTime = System.DateTime.Now.Ticks;

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

                    List <AsyncLoadAssetParam> loadingList = loadingAssetLists[i];
                    if (loadingList.Count <= 0)
                    {
                        continue;
                    }
                    AsyncLoadAssetParam asyncLoadAssetParam = loadingList[0];
                    loadingList.RemoveAt(0);
                    asyncCallBackPacks = asyncLoadAssetParam.CallBackPacks;

                    Object    obj       = null;
                    AssetItem assetItem = null;
#if UNITY_EDITOR
                    if (!IsLoadFromAssetBundle)
                    {
                        if (asyncLoadAssetParam.isSprite)
                        {
                            obj = LoadAssetByEditor <Sprite>(asyncLoadAssetParam.Path);
                        }
                        else
                        {
                            obj = LoadAssetByEditor <Object>(asyncLoadAssetParam.Path);
                        }

//                    yield return new WaitForSeconds(0.0001f);
                        assetItem = AssetBundleManager.Instance.FindAssetItem(asyncLoadAssetParam.Crc);
                    }
#endif
                    if (obj == null)
                    {
                        assetItem = AssetBundleManager.Instance.LoadAssetItemBundle(asyncLoadAssetParam.Crc);
                        if (assetItem != null && assetItem.assetBundle != null)
                        {
                            AssetBundleRequest request = null;
                            if (asyncLoadAssetParam.isSprite)
                            {
                                request = assetItem.assetBundle.LoadAssetAsync <Sprite>(assetItem.assetName);
                            }
                            else
                            {
                                request = assetItem.assetBundle.LoadAssetAsync(assetItem.assetName);
                            }

                            yield return(request);

                            if (request.isDone)
                            {
                                obj = request.asset;
                            }
                            lastYieldTime = System.DateTime.Now.Ticks;
                        }
                    }

                    CacheResource(asyncLoadAssetParam.Path, ref assetItem, asyncLoadAssetParam.Crc, obj,
                                  asyncCallBackPacks.Count);
                    for (int j = 0; j < asyncCallBackPacks.Count; j++)
                    {
                        AsyncCallBackPack callBackPack = asyncCallBackPacks[j];

                        #region forObjectManager

                        if (callBackPack != null && callBackPack.ObjectItem != null)
                        {
                            callBackPack.ObjectItem
                            .PrimitiveAssetItem = assetItem;
                            callBackPack.PrimitiveAssetFinish?.Invoke(asyncLoadAssetParam.Path,
                                                                      callBackPack.ObjectItem, callBackPack.ObjectItem.paramList);
                            //**
                            callBackPack.PrimitiveAssetFinish = null;
                            callBackPack.ObjectItem           = null;
                        }

                        #endregion

                        callBackPack?.AssetFinish?.Invoke(asyncLoadAssetParam.Path, obj, callBackPack.paramList);
                        callBackPack?.Reset();
                        asyncCallBackPackPool.Recycle(callBackPack);
                    }

                    asyncCallBackPacks.Clear();
                    asyncLoadAssetParamDic.Remove(asyncLoadAssetParam.Crc);
                    asyncLoadAssetParam.Reset();
                    asyncLoadResParamPackPool.Recycle(asyncLoadAssetParam);
                    //editor
                    if (System.DateTime.Now.Ticks - lastYieldTime > TimeOut.AsyncLoad)
                    {
//                    Debug.Log("return1");
                        yield return(null);

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

//空转
                if (!isHaveYield || DateTime.Now.Ticks - lastYieldTime > TimeOut.AsyncLoad)
                {
//                Debug.Log("return2");

                    yield return(null);

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