SetRequestDataFromCache() public static method

从缓存设置数据
public static SetRequestDataFromCache ( CRequest req ) : bool
req CRequest
return bool
Beispiel #1
0
        /// <summary>
        /// check load from cache
        /// </summary>
        /// <param name="req"></param>
        static bool CheckLoadAssetAsync(CRequest req)
        {
            ABDelayUnloadManager.CheckRemove(req.keyHashCode);
            if (CacheManager.SetRequestDataFromCache(req))
            {
                if (req.assetBundleRequest != null)
                {
                    AddReqToAssetCallBackList(req);
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat("<color=#15C1B2> 1.1 from cache CheckLoadAssetAsync=true Req(assetname={0},url={1})  </color>", req.assetName, req.url);
#endif
                }
                else
                {
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat("<color=#15C1B2> 1.2 from cache CheckLoadAssetAsync=false Req(assetname={0},url={1})  </color>", req.assetName, req.url);
#endif
                    LoadAssetComplate(req);
                }
                return(true);
            }
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// 判断异步加载Asset
        /// </summary>
        /// <param name="req"></param>
        static bool CheckLoadAssetAsync(CRequest req)
        {
            if (CacheManager.SetRequestDataFromCache(req))
            {
                if (req.assetBundleRequest != null)
                {
                    CacheManager.AddLock(req.keyHashCode);
                    loadingAssetQueue.Add(req);
                                        #if HUGULA_LOADER_DEBUG
                    Debug.LogFormat("<color=yellow>from cache CheckLoadAssetAsync=true Req(assetname={0},url={1})  </color>", req.assetName, req.url);
                                        #endif
                }
                else
                {
                                        #if HUGULA_LOADER_DEBUG
                    Debug.LogFormat("<color=yellow>from cache CheckLoadAssetAsync=false Req(assetname={0},url={1})  </color>", req.assetName, req.url);
                                        #endif
                    LoadAssetComplate(req);
                }
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// 加载依赖项目
        /// </summary>
        /// <param name="req"></param>
        static protected int[] LoadDependencies(CRequest req)
        {
            string[] deps = assetBundleManifest.GetDirectDependencies(req.assetBundleName);
            if (deps.Length == 0)
            {
                return(null);
            }

            string   dep_url;
            string   depAbName = "";
            CRequest item;

            int[] hashs = new int[deps.Length];
            int   keyhash;

            for (int i = 0; i < deps.Length; i++)
            {
                depAbName = deps[i];
                if (string.IsNullOrEmpty(depAbName))  // Dependency assetbundle name is empty  unity bug?
                {
#if UNITY_EDITOR
                    Debug.LogWarningFormat("the request({0},{1}) Dependencies {2} is empty ", req.assetName, req.url, i);
#endif
                    hashs[i] = 0;
                    continue;
                }
                dep_url  = RemapVariantName(depAbName);
                keyhash  = LuaHelper.StringToHash(dep_url);
                hashs[i] = keyhash;
#if UNITY_EDITOR
                CountMananger.WillAdd(dep_url);  //引用数量加1
#else
                CountMananger.WillAdd(keyhash);  //引用数量加1
#endif

                CacheData sharedCD = CacheManager.GetCache(keyhash);
                if (sharedCD != null)
                {
                    if (!sharedCD.isAssetLoaded)
                    {
                        item               = LRequest.Get();
                        item.relativeUrl   = dep_url;
                        item.isShared      = true;
                        item.async         = false;
                        item.isAssetBundle = true;

                        CacheManager.SetRequestDataFromCache(req);
                        if (_instance && _instance.OnSharedComplete != null)
                        {
                            _instance.OnSharedComplete(item);
                        }

                        LRequest.Release(item);
                    }
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" 0.3 <color=#15A0A1>Request(assetName={0}, url={1},isShared={2}) Dependencies  CacheManager.Contains(url={3},sharedCD.isAssetLoaded={4}) </color>", req.assetName, req.url, req.isShared, dep_url, sharedCD.isAssetLoaded);
#endif
                }
                else
                {
                    item                 = LRequest.Get();
                    item.relativeUrl     = dep_url;
                    item.isShared        = true;
                    item.async           = false;
                    item.isAssetBundle   = true;
                    item.allDependencies = LoadDependencies(item);
                    item.isNormal        = false;
                    item.priority        = req.priority;
                    item.uris            = req.uris;
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat("<color=#15A0A1>0.5  Request(assetname={0}) Begin Load  Dependencies Req({1},allDependencies.count={3})keyHashCode{2}, frameCount{4}</color>", req.assetName, item.assetName, item.keyHashCode, item.allDependencies == null ? 0 : item.allDependencies.Length, Time.frameCount);
#endif
                    AddReqToQueue(item);
                }
            }

            return(hashs);
        }
Beispiel #4
0
        // Update is called once per frame
        void Update()
        {
            for (int i = 0; i < freeLoader.Count;)
            {
                CCar load = freeLoader[i];
                if (load.enabled)
                {
                    load.Update();
                }

                if (load.isFree)
                {
                    freeLoader.RemoveAt(i);
                    ReleaseCCar(load);  //放回对象池
                }
                else
                {
                    i++;
                }
            }

            //1 load assetbunlde
            for (int i = 0; i < loaderPool.Count; i++)
            {
                CCar load = loaderPool[i];
                if (load.isFree && realyLoadingQueue.Count > 0)
                {
                    var req = realyLoadingQueue.Dequeue();
                    if (CheckLoadedAssetBundle(req))
                    {
                        CheckLoadAssetAsync(req);
                    }
                    else if (CheckLoadingAssetBundle(req))
                    {
                        AddReqToQueue(req);
                    }
                    else
                    {
                        downloadings[req.udKey] = load;
                        load.BeginLoad(req);
                    }
                }
                if (load.enabled)
                {
                    load.Update();
                }
            }
#if UNITY_EDITOR
            loadingAssetBundleQueueCount = loadingAssetBundleQueue.Count;
            loadingAssetQueueCount       = loadingAssetQueue.Count;
            DebugInfo = "";
#endif
            //2 wait dependencies set asset
            for (int i = 0; i < loadingAssetBundleQueue.Count;)
            {
                var item = loadingAssetBundleQueue[i];
                if (CacheManager.CheckDependenciesComplete(item))   //判断依赖项目是否加载完成
                {
                    if (CacheManager.SetRequestDataFromCache(item)) //设置缓存数据。
                    {
                        loadingAssetQueue.Add(item);                //加载资源
                    }
                    else
                    {
                        loadedAssetQueue.Enqueue(item);
#if HUGULA_LOADER_DEBUG
                        Debug.LogFormat("<color=red> 2.2 SetRequestDataFromCache false Req(assetname={0},url={1}) frame={2}loadingAssetBundleQueue.Count={3} </color>", item.assetName, item.url, Time.frameCount, loadingAssetBundleQueue.Count);
#endif
                    }
                    // if (item.assetBundleRequest != null)
                    //  CacheManager.AddLock (item.keyHashCode);//异步需要锁定
                    loadingAssetBundleQueue.RemoveAt(i);
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" 2.3 <color=#15C132>DependenciesComplete Req(assetname={0},url={1},async={2}) frameCount{3},loadingAssetBundleQueue.Count={4}</color>", item.assetName, item.url, item.async, Time.frameCount, loadingAssetBundleQueue.Count);
#endif
                }
                else
                {
                    i++;
#if UNITY_EDITOR
                    DebugInfo += string.Format(" -2.3CheckDependenciesComplete Req(assetname={0},url={1}) frameCount{2},loadingAssetBundleQueue.Count={3}</color>", item.assetName, item.url, Time.frameCount, loadingAssetBundleQueue.Count);
#endif
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" -2.3 <color=#15C132>CheckDependenciesComplete Req(assetname={0},url={1}) frameCount{2},loadingAssetBundleQueue.Count={3}</color>", item.assetName, item.url, Time.frameCount, loadingAssetBundleQueue.Count);
#endif
                }
            }

            //3 load asset
            for (int i = 0; i < loadingAssetQueue.Count; i++)
            {
                var item = loadingAssetQueue[i];
#if HUGULA_LOADER_DEBUG
                Debug.LogFormat(" 3.2.-1 <color=#A9C115> Req(assetname={0},url={1}) loadingAssetQueue.Count={2} frameCount{3} </color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount);
#endif
                if (item.assetBundleRequest != null && item.assetBundleRequest.isDone) //如果加载完成
                {
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" 3.2.0 <color=#A9C115>set Req(assetname={0},url={1}).data asnyc Count{2} frameCount{3} </color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount);
#endif
                    if (item.assetBundleRequest is AssetBundleRequest)
                    {
                        if (CacheManager.Typeof_ABAllAssets.Equals(item.assetType))
                        {
                            item.data = ((AssetBundleRequest)item.assetBundleRequest).allAssets;  //赋值
                        }
                        else
                        {
                            item.data = ((AssetBundleRequest)item.assetBundleRequest).asset;  //赋值
                        }
                    }
                    else
                    {
                        item.data = item.assetBundleRequest;
                    }
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" 3.2.1 <color=#A9C115>set Req(assetname={0},url={1} data{2}) end </color>", item.assetName, item.url, item.data);
#endif
                    loadedAssetQueue.Enqueue(item);
                    loadingAssetQueue.RemoveAt(i);
                }
                else if (item.assetBundleRequest == null)   //非异步
                {
                    loadedAssetQueue.Enqueue(item);
                    loadingAssetQueue.RemoveAt(i);
#if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" 3.2 <color=#A9C115>set Req(assetname={0},url={1}).data Async  Count{2} frameCount{3}</color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount);
#endif
                }
                else
                {
                    i++;
                }
            }

            //4 complete asset
            while (loadedAssetQueue.Count > 0)
            {
                LoadAssetComplate(loadedAssetQueue.Dequeue());
            }

            //group check
            for (int i = 0; i < allGroupRequestRecord.Count;)
            {
                var group = allGroupRequestRecord[i];
                group.Progress();
                if (group.Count > 0)
                {
                    i++;
                }
                else
                {
                    var act = group.onGroupComplate;
                    GroupRequestRecord.Release(group);
                    allGroupRequestRecord.RemoveAt(i);
                    if (act != null)
                    {
                        act(group);
                    }
                }
            }
        }
Beispiel #5
0
        // Update is called once per frame
        void Update()
        {
            //加载
            for (int i = 0; i < loaderPool.Count; i++)
            {
                CCar load = loaderPool[i];
                if (load.isFree && realyLoadingQueue.Count > 0)
                {
                    var req = realyLoadingQueue.Dequeue();
                    if (!CheckLoadAssetBundle(req))
                    {
                        downloadings[req.udKey] = true;
                        load.BeginLoad(req);
                    }
                }
                if (load.enabled)
                {
                    load.Update();
                }
            }

            for (int i = 0; i < freeLoader.Count;)
            {
                CCar load = freeLoader [i];
                if (load.enabled)
                {
                    load.Update();
                }

                if (load.isFree)
                {
                                        #if HUGULA_LOADER_DEBUG
                    if (load.req != null)
                    {
                        Debug.LogFormat(" 1. a <color=yellow>freeLoader is free Req(assetname={0},url={1}) frameCount{2}</color>", load.req.assetName, load.req.url, Time.frameCount);
                    }
                    else
                    {
                        Debug.LogFormat(" 1. a <color=yellow>freeLoader is free Req(assetname={0},url={1}) frameCount{2}</color>", load, load, Time.frameCount);
                    }
                                        #endif
                    freeLoader.RemoveAt(i);
                    ReleaseCCar(load);                      //放回对象池
                }
                else
                {
                    i++;
                }
            }

            //ab
            for (int i = 0; i < loadingAssetBundleQueue.Count;)
            {
                var item = loadingAssetBundleQueue[i];
                if (CacheManager.CheckDependenciesComplete(item))                   //判断依赖项目是否加载完成
                {
                    CacheManager.SetRequestDataFromCache(item);                     //设置缓存数据。
                    loadingAssetQueue.Add(item);
                    if (item.assetBundleRequest != null)
                    {
                        CacheManager.AddLock(item.keyHashCode);                         //异步需要锁定
                    }
                    loadingAssetBundleQueue.RemoveAt(i);
                                        #if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" 4. <color=yellow>DependenciesComplete Req(assetname={0},url={1}) frameCount{2}</color>", item.assetName, item.url, Time.frameCount);
                                        #endif
                }
                else
                {
                    i++;
                                        #if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" -4. <color=yellow>CheckDependenciesComplete Req(assetname={0},url={1}) frameCount{2}</color>", item.assetName, item.url, Time.frameCount);
                                        #endif
                }
            }

            //asset
            for (int i = 0; i < loadingAssetQueue.Count; i++)
            {
                var item = loadingAssetQueue[i];
                if (item.assetBundleRequest != null && item.assetBundleRequest.isDone) //如果加载完成
                {
                    if (item.assetBundleRequest is AssetBundleRequest)
                    {
                        item.data = ((AssetBundleRequest)item.assetBundleRequest).asset;//赋值
                    }
                    else
                    {
                        item.data = item.assetBundleRequest;
                    }
                                        #if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" 5. <color=yellow>set Req(assetname={0},url={1}).data asnyc Count{2} frameCount{3} </color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount);
                                        #endif
                    loadedAssetQueue.Enqueue(item);
                    loadingAssetQueue.RemoveAt(i);
                }
                else if (item.assetBundleRequest == null) //非异步
                {
                    loadedAssetQueue.Enqueue(item);
                    loadingAssetQueue.RemoveAt(i);
                                        #if HUGULA_LOADER_DEBUG
                    Debug.LogFormat(" 5. <color=yellow>set Req(assetname={0},url={1}).data  Count{2} frameCount{3}</color>", item.assetName, item.url, loadingAssetQueue.Count, Time.frameCount);
                                        #endif
                }
                else
                {
                    i++;
                }
            }

            while (loadedAssetQueue.Count > 0)
            {
                LoadAssetComplate(loadedAssetQueue.Dequeue());
            }

            //组进度条
            for (int i = 0; i < allGroupRequestRecord.Count;)
            {
                var group = allGroupRequestRecord [i];
                group.Progress();
                if (group.Count > 0)
                {
                    i++;
                }
                else
                {
                    var act = group.onGroupComplate;
                    GroupRequestRecordPool.Release(group);
                    allGroupRequestRecord.RemoveAt(i);
                    if (act != null)
                    {
                        act(group);
                    }
                }
            }
        }