Пример #1
0
    IEnumerator _CopyManifestFromStreamingAssets(Action callback)
    {
        DirectoryInfo dir        = new DirectoryInfo(BundleUtil.GetReadOnlyDirectory() + "/");
        Uri           baseUri    = new Uri(dir.FullName);
        IDownloader   downloader = new WWWDownloader(baseUri, false);

        // 下载 Manifest
        IProgressResult <Progress, BundleManifest> manifestResult = downloader.DownloadManifest(BundleSetting.ManifestFilename);

        yield return(manifestResult.WaitForDone());

        if (manifestResult.Exception != null)
        {
            LogManager.Log("Downloads BundleManifest failure.Error:{0}", manifestResult.Exception);
            yield break;
        }

        IResources _resources = CreateResources();

        context.GetContainer().Register <IResources>(_resources);
        if (callback != null)
        {
            callback.Invoke();
        }
    }
        public IEnumerator Download(List <string> bundleNames)
        {
            this.downloading = true;

            try
            {
                IProgressResult <Progress, BundleManifest> manifestResult = this.downloader.DownloadManifest(BundleSetting.ManifestFilename);

                yield return(manifestResult.WaitForDone());

                if (manifestResult.Exception != null)
                {
                    Debug.LogFormat("Downloads BundleManifest failure.Error:{0}", manifestResult.Exception);
                    yield break;
                }

                BundleManifest manifest = manifestResult.Result;

                IProgressResult <float, List <BundleInfo> > bundlesResult = this.downloader.GetDownloadList(manifest);

                yield return(bundlesResult.WaitForDone());

                List <BundleInfo> bundles = bundlesResult.Result.FindAll(obj => bundleNames.Contains(obj.FullName));

                if (bundles == null || bundles.Count <= 0)
                {
                    Debug.LogFormat("Please clear cache and remove StreamingAssets,try again.");
                    yield break;
                }

                IProgressResult <Progress, bool> downloadResult = this.downloader.DownloadBundles(bundles);
                downloadResult.Callbackable().OnProgressCallback(p =>
                {
                    Debug.LogFormat("Downloading {0:F2}KB/{1:F2}KB {2:F3}KB/S", p.GetCompletedSize(UNIT.KB), p.GetTotalSize(UNIT.KB), p.GetSpeed(UNIT.KB));
                });

                yield return(downloadResult.WaitForDone());

                if (downloadResult.Exception != null)
                {
                    Debug.LogFormat("Downloads AssetBundle failure.Error:{0}", downloadResult.Exception);
                    yield break;
                }

                if (this.resources != null)
                {
                    //update BundleManager's manifest
                    BundleManager manager = (this.resources as BundleResources).BundleManager as BundleManager;
                    manager.BundleManifest = manifest;
                }

#if UNITY_EDITOR
                UnityEditor.EditorUtility.OpenWithDefaultApp(BundleUtil.GetReadOnlyDirectory());
#endif
            }
            finally
            {
                this.downloading = false;
            }
        }
Пример #3
0
        /// <summary>
        /// Preloads AssetBundle.
        /// </summary>
        /// <param name="bundleNames"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        IEnumerator Preload(string[] bundleNames, int priority)
        {
            IProgressResult <float, IBundle[]> result = this.resources.LoadBundle(bundleNames, priority);

            yield return(result.WaitForDone());

            if (result.Exception != null)
            {
                Debug.LogWarningFormat("Loads failure.Error:{0}", result.Exception);
                yield break;
            }

            foreach (IBundle bundle in result.Result)
            {
                bundles.Add(bundle.Name, bundle);
            }
        }
Пример #4
0
    IEnumerator _CorDownload()
    {
        this.downloading = true;
        try
        {
            if (downloadProgressEvent != null)
            {
                downloadProgressEvent(0);
            }
            // 下载 Manifest
            IProgressResult <Progress, BundleManifest> manifestResult = this.downloader.DownloadManifest(BundleSetting.ManifestFilename);
            yield return(manifestResult.WaitForDone());

            if (manifestResult.Exception != null)
            {
                LogManager.Log("Downloads BundleManifest failure.Error:{0}", manifestResult.Exception);
                yield break;
            }

            // 下载 BundleInfo
            BundleManifest manifest = manifestResult.Result;
            IProgressResult <float, List <BundleInfo> > bundlesResult = this.downloader.GetDownloadList(manifest);
            yield return(bundlesResult.WaitForDone());

            List <BundleInfo> bundles = bundlesResult.Result;
            if (bundles == null || bundles.Count <= 0)
            {
                LogManager.Log("Please clear cache and remove StreamingAssets,try again.");
                yield break;
            }

            // 下载 Bundle
            IProgressResult <Progress, bool> downloadResult = this.downloader.DownloadBundles(bundles);
            downloadResult.Callbackable().OnProgressCallback(p =>
            {
                LogManager.Log("Downloading {0:F2}KB/{1:F2}KB {2:F3}KB/S", p.GetCompletedSize(UNIT.KB), p.GetTotalSize(UNIT.KB), p.GetSpeed(UNIT.KB));
                float percent = p.GetCompletedSize(UNIT.KB) / p.GetTotalSize(UNIT.KB);
                if (downloadProgressEvent != null)
                {
                    downloadProgressEvent(percent);
                }
            });
            yield return(downloadResult.WaitForDone());

            if (downloadResult.Exception != null)
            {
                LogManager.Log("Downloads AssetBundle failure.Error:{0}", downloadResult.Exception);
                yield break;
            }

            // 下载成功
            LogManager.Log(" 下载成功 ");
            IResources _resources = CreateResources();
            context.GetContainer().Unregister <IResources>();
            context.GetContainer().Register <IResources>(_resources);

#if UNITY_EDITOR
            UnityEditor.EditorUtility.OpenWithDefaultApp(BundleUtil.GetStorableDirectory());
#endif
        }
        finally
        {
            this.downloading = false;
        }
    }
Пример #5
0
        protected virtual IEnumerator DoLoadAssetsToMapAsync <T>(IProgressPromise <float, Dictionary <string, T> > promise, params string[] paths) where T : Object
        {
            Dictionary <string, T> results             = new Dictionary <string, T>();
            Dictionary <string, List <string> > groups = new Dictionary <string, List <string> >();
            Dictionary <string, string>         assetNameAndPathMapping = new Dictionary <string, string>();
            List <string> bundleNames = new List <string>();

            for (int i = 0; i < paths.Length; i++)
            {
                var           path     = paths[i];
                AssetPathInfo pathInfo = this.pathInfoParser.Parse(path);
                if (pathInfo == null || pathInfo.BundleName == null)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("Not found the AssetBundle or parses the path info '{0}' failure.", path);
                    }
                    continue;
                }

                var asset = this.GetCache <T>(path);
                if (asset != null)
                {
                    if (!results.ContainsKey(path))
                    {
                        results.Add(path, asset);
                    }
                    continue;
                }

                List <string> list = null;
                if (!groups.TryGetValue(pathInfo.BundleName, out list))
                {
                    list = new List <string>();
                    groups.Add(pathInfo.BundleName, list);
                    bundleNames.Add(pathInfo.BundleName);
                }

                if (!list.Contains(pathInfo.AssetName))
                {
                    list.Add(pathInfo.AssetName);
                    assetNameAndPathMapping[pathInfo.AssetName] = path;
                }
            }

            if (bundleNames.Count <= 0)
            {
                promise.UpdateProgress(1f);
                promise.SetResult(results);
                yield break;
            }

            IProgressResult <float, IBundle[]> bundleResult = this.LoadBundle(bundleNames.ToArray(), 0);
            float weight = bundleResult.IsDone ? 0f : DEFAULT_WEIGHT;

            bundleResult.Callbackable().OnProgressCallback(p => promise.UpdateProgress(weight * p));

            yield return(bundleResult.WaitForDone());

            if (bundleResult.Exception != null)
            {
                promise.SetException(bundleResult.Exception);
                yield break;
            }

            Dictionary <string, IProgressResult <float, Dictionary <string, T> > > assetResults = new Dictionary <string, IProgressResult <float, Dictionary <string, T> > >();

            IBundle[] bundles = bundleResult.Result;
            for (int i = 0; i < bundles.Length; i++)
            {
                using (IBundle bundle = bundles[i])
                {
                    if (!groups.ContainsKey(bundle.Name))
                    {
                        continue;
                    }

                    List <string> assetNames = groups[bundle.Name];
                    if (assetNames == null || assetNames.Count < 0)
                    {
                        continue;
                    }

                    IProgressResult <float, Dictionary <string, T> > assetResult = bundle.LoadAssetsToMapAsync <T>(assetNames.ToArray());
                    assetResult.Callbackable().OnCallback(ar =>
                    {
                        if (ar.Exception != null)
                        {
                            return;
                        }

                        foreach (var kv in ar.Result)
                        {
                            string key = assetNameAndPathMapping[kv.Key];
                            var value  = kv.Value;
                            if (!results.ContainsKey(key))
                            {
                                results.Add(key, value);
                            }
                        }
                    });
                    assetResults.Add(bundle.Name, assetResult);
                }
            }

            if (assetResults.Count < 0)
            {
                promise.UpdateProgress(1f);
                promise.SetResult(results);
                yield break;
            }

            bool  finished   = false;
            float progress   = 0f;
            int   assetCount = assetResults.Count;

            do
            {
                yield return(waitForSeconds);

                finished = true;
                progress = 0f;

                var assetEnumerator = assetResults.GetEnumerator();
                while (assetEnumerator.MoveNext())
                {
                    var kv          = assetEnumerator.Current;
                    var assetResult = kv.Value;
                    if (!assetResult.IsDone)
                    {
                        finished = false;
                    }

                    progress += (1f - weight) * assetResult.Progress / assetCount;
                }

                promise.UpdateProgress(weight + progress);
            } while (!finished);

            promise.UpdateProgress(1f);
            promise.SetResult(results);
        }