Ejemplo n.º 1
0
        /// <summary>
        /// 加载常驻bundle
        /// </summary>
        /// <param name="complete"></param>
        /// <param name="progress"></param>
        /// <returns></returns>
        public IEnumerator LoadResidentBundles(Action complete = null, BundleBatchLoadingDelegate progress = null)
        {
            ResPackConfig config = ResService.GetInstance().PackConfig;

            if (config == null)
            {
                JW.Common.Log.LogE("In LoadResidentBundles(), but ResPackConfig is null.");

                if (complete != null)
                {
                    complete();
                }

                yield break;
            }

            JWObjList <BundlePackInfo> bundleList = new JWObjList <BundlePackInfo>();

            for (int i = 0; i < config.PackInfo.Count; i++)
            {
                BundlePackInfo p = config.PackInfo[i] as BundlePackInfo;
                if (p != null && p.Life == EBundleLife.Resident)
                {
                    bundleList.Add(p);
                }
            }

            yield return(StartCoroutine(BundleService.GetInstance().BatchLoadAsync(bundleList, complete, progress)));
        }
Ejemplo n.º 2
0
        public IEnumerator BatchLoadAsync(JWObjList <BundlePackInfo>[] bundleLists, Action complete, BundleBatchLoadingDelegate progress = null)
        {
            if (bundleLists == null || bundleLists.Length == 0)
            {
                if (progress != null)
                {
                    progress(1f);
                }

                if (complete != null)
                {
                    complete();
                }

                yield break;
            }

            int totalBundles = 0;

            for (int i = 0; i < bundleLists.Length; ++i)
            {
                JWObjList <BundlePackInfo> bundles = bundleLists[i];
                if (bundles != null)
                {
                    for (int j = 0; j < bundles.Count; ++j)
                    {
                        if (bundles[j] != null)
                        {
                            ++totalBundles;
                        }
                    }
                }
            }

            // 并发加载多个bundle
            JWObjList <string>         failedBundles = null;
            Dictionary <string, float> progressDict  = new Dictionary <string, float>(totalBundles); // 因为是并发,所以计算进度稍微复杂一些,需要每个bundle的进度收集计算
            float step = 1f / totalBundles;                                                          // 大致平分每个bundle的加载时间

            for (int i = 0; i < bundleLists.Length; ++i)
            {
                JWObjList <BundlePackInfo> bundles = bundleLists[i];
                StartCoroutine(LoadAsync(bundles, delegate(BundleRef bundleRef)
                {
                    // complete one
                    if (bundleRef != null)
                    {
                        // 已加载完成某一个bundle
                        if (progressDict.ContainsKey(bundleRef.Path))
                        {
                            progressDict[bundleRef.Path] = 1f;
                        }
                        else
                        {
                            progressDict.Add(bundleRef.Path, 1f);
                        }
                    }
                }, delegate(BundlePackInfo packInfo, string error)
                {
                    if (failedBundles == null)
                    {
                        failedBundles = new JWObjList <string>();
                    }

                    if (!failedBundles.Contains(packInfo.Path))
                    {
                        failedBundles.Add(packInfo.Path);
                    }
                }, delegate(BundlePackInfo packInfo, float prog)
                {
                    // 加载进度回调,会有多次
                    if (progress != null)
                    {
                        // 记录每次每个bundle的进度
                        if (progressDict.ContainsKey(packInfo.Path))
                        {
                            progressDict[packInfo.Path] = prog;
                        }
                        else
                        {
                            progressDict.Add(packInfo.Path, prog);
                        }

                        // 计算总进度
                        float totalProgress = 0;
                        for (int bundleListIndex = 0; bundleListIndex < bundleLists.Length; ++bundleListIndex)
                        {
                            JWObjList <BundlePackInfo> bundlePackInfos = bundleLists[bundleListIndex];
                            if (bundlePackInfos != null)
                            {
                                for (int bundleIndex = 0; bundleIndex < bundlePackInfos.Count; ++bundleIndex)
                                {
                                    if (bundlePackInfos[bundleIndex] != null)
                                    {
                                        float eachProg = 0;
                                        if (progressDict.TryGetValue(bundlePackInfos[bundleIndex].Path, out eachProg))
                                        {
                                            totalProgress += eachProg * step;
                                        }
                                    }
                                }
                            }
                        }

                        progress(totalProgress);
                    }
                }));
            }

            // 检查是否全部完成
            while (true)
            {
                bool completed = true;
                for (int bundleListIndex = 0; bundleListIndex < bundleLists.Length; ++bundleListIndex)
                {
                    JWObjList <BundlePackInfo> bundlePackInfos = bundleLists[bundleListIndex];
                    if (bundlePackInfos != null)
                    {
                        for (int bundleIndex = 0; bundleIndex < bundlePackInfos.Count; ++bundleIndex)
                        {
                            BundlePackInfo bundleInfo = bundlePackInfos[bundleIndex];
                            if (_bundleDict.ContainsKey(bundleInfo.Path))
                            {
                                continue;
                            }

                            if (failedBundles != null && failedBundles.Contains(bundleInfo.Path))
                            {
                                continue;
                            }

                            completed = false;
                            yield return(null);

                            break;
                        }
                    }

                    if (!completed)
                    {
                        break;
                    }
                }

                if (completed)
                {
                    if (complete != null)
                    {
                        complete();
                    }

                    yield break;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 加载状态相关bundle
        /// </summary>
        /// <param name="resourceList">资源路径列表</param>
        /// <param name="complete">加载完成回调</param>
        /// <param name="progress">加载进度回调</param>
        /// <param name="loaded">单个bundle加载完成回调</param>
        /// <param name="singleSync">true:当只有一个bundle时,使用同步加载;否则使用并发异步加载</param>
        /// <returns></returns>
        public IEnumerator LoadBundle(JWObjList <string> resourceList, Action complete = null, BundleBatchLoadingDelegate progress = null, BundleLoadedOneDelegate loaded = null, bool singleSync = true)
        {
            bool record = loaded != null;

            // 剔除已有资源
            for (int i = 0; i < resourceList.Count;)
            {
                string path = resourceList[i];
                if (ResService.GetInstance().Exists(path))
                {
                    if (record)
                    {
                        _unbundledResources.Add(path);
                    }

                    resourceList.RemoveAt(i);
                    continue;
                }

                i++;
            }

            // bundle list
            JWObjList <BundlePackInfo> bundleList = GetBundlePackInfoListForResources(resourceList, record);

            // 异步返回,在处理完源数据之后避免resourceList被reset
            yield return(null);

            // 未打包资源
            if (_unbundledResources.Count > 0)
            {
                if (loaded != null)
                {
                    loaded(_unbundledResources, true);
                }

                _unbundledResources.Clear();
            }

            // load
            if (bundleList != null && bundleList.Count > 0)
            {
                if (singleSync && bundleList.Count == 1)
                {
                    // 只有一个bundle,使用同步加载方式
                    BundleService.GetInstance().LoadSync(bundleList[0]);

                    if (progress != null)
                    {
                        progress(1f);
                    }

                    if (loaded != null)
                    {
                        AssetBundle        bundle           = BundleService.GetInstance().GetBundle(bundleList[0].Path);
                        JWObjList <string> relatedResources = GetLoadedBundleResources(bundleList[0].Path);
                        if (relatedResources != null && relatedResources.Count > 0)
                        {
                            loaded(relatedResources, bundle != null);
                            relatedResources.Clear();
                        }

                        BundleService.GetInstance().Unload(bundleList[0]);
                    }

                    if (complete != null)
                    {
                        complete();
                    }
                }
                else
                {
                    // 多个bundle,使用并发加载
                    yield return(StartCoroutine(BundleService.GetInstance().BatchLoadAsync(bundleList, delegate()
                    {
                        if (complete != null)
                        {
                            complete();
                        }
                    }, progress, delegate(BundleRef bundle)
                    {
                        if (bundle != null)
                        {
                            if (loaded != null)
                            {
                                JWObjList <string> relatedResources = GetLoadedBundleResources(bundle.Path);
                                if (relatedResources != null && relatedResources.Count > 0)
                                {
                                    loaded(relatedResources, true);
                                    relatedResources.Clear();
                                }

                                BundleService.GetInstance().Unload(bundle.PackInfo);
                            }
                        }
                    }, delegate(BundlePackInfo pi, string error)
                    {
                        if (loaded != null)
                        {
                            JWObjList <string> relatedResources = GetLoadedBundleResources(pi.Path);
                            if (relatedResources != null && relatedResources.Count > 0)
                            {
                                //bool succ = (pi.IsNoBundle());

                                //loaded(relatedResources, succ);
                                //relatedResources.Clear();
                            }
                        }
                    })));
                }
            }
            else
            {
                if (complete != null)
                {
                    complete();
                }
            }
        }