コード例 #1
0
        private void CancelAllCoroutines()
        {
            if (yieldCancel != null)
            {
                yieldCancel.Dispose();

                // キャンセルしたので再生成.
                yieldCancel = new YieldCancel();

                assetBundleManager.RegisterYieldCancel(yieldCancel);
            }
        }
コード例 #2
0
        public void CancelAll()
        {
            if (yieldCancel != null)
            {
                yieldCancel.Dispose();

                // キャンセルしたので再生成.
                yieldCancel = new YieldCancel();

                assetBundleManager.RegisterYieldCancel(yieldCancel);
            }
        }
コード例 #3
0
        private IEnumerator LoadMasterInternal(IObserver <bool> observer, Dictionary <IMaster, string> versionTable, IProgress <float> progress)
        {
            #if UNITY_EDITOR
            if (!Prefs.checkVersion)
            {
                UnityConsole.Event(ConsoleEventName, ConsoleEventColor, "Use CachedMasterFile.");
            }
            #endif

            var result = true;

            var updateLog = new StringBuilder();
            var loadLog   = new StringBuilder();

            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            if (versionTable != null)
            {
                if (progress != null)
                {
                    progress.Report(0f);
                }

                var yieldCancel       = new YieldCancel();
                var cancellationToken = yieldCancel.Token;

                var amount         = 1f / versionTable.Count;
                var progressAmount = 0f;

                // 並行で処理.
                var observers = new List <IObservable <Unit> >();

                foreach (var element in versionTable)
                {
                    var master        = element.Key;
                    var masterVersion = element.Value;

                    var masterType = master.GetType();
                    var masterName = masterType.Name;

                    var masterFileName = masterFileNames.GetValueOrDefault(masterType);

                    var localVersion = master.LoadVersion();

                    IObservable <Unit> observable = null;

                    Action <bool, double> onUpdateFinish = (state, time) =>
                    {
                        if (state)
                        {
                            var localVersionText = string.IsNullOrEmpty(localVersion) ? "---" : localVersion;
                            var message          = string.Format("{0} ({1:F1}ms) : {2} >> {3}", masterName, time, localVersionText, masterVersion);

                            updateLog.AppendLine(message);
                        }
                        else
                        {
                            Debug.LogErrorFormat("Update master failed.\nClass : {0}\nFile : {1}\n", masterType.FullName, masterFileName);
                        }

                        result         &= state;
                        progressAmount += amount;

                        if (progress != null)
                        {
                            progress.Report(progressAmount);
                        }

                        if (onUpdateMaster != null)
                        {
                            onUpdateMaster.OnNext(Unit.Default);
                        }
                    };

                    Action <bool, double> onLoadFinish = (state, time) =>
                    {
                        if (state)
                        {
                            loadLog.AppendFormat("{0} ({1:F1}ms)", masterName, time).AppendLine();
                        }
                        else
                        {
                            Debug.LogErrorFormat("Load master failed.\nClass : {0}\nFile : {1}\n", masterType.FullName, masterFileName);
                        }

                        result         &= state;
                        progressAmount += amount;

                        if (progress != null)
                        {
                            progress.Report(progressAmount);
                        }
                    };

                    if (master.CheckVersion(masterVersion))
                    {
                        // 読み込み.
                        observable = Observable.Defer(() =>
                        {
                            return(MasterLoad(master, onLoadFinish).AsUnitObservable());
                        });

                        observers.Add(observable);
                    }
                    else
                    {
                        // ダウンロード + 読み込み.
                        observable = Observable.Defer(() =>
                        {
                            return(MasterUpdate(master, masterVersion, onUpdateFinish, cancellationToken)
                                   .SelectMany(x => x ? MasterLoad(master, onLoadFinish) : Observable.Return(false))
                                   .AsUnitObservable());
                        });

                        observers.Add(observable);
                    }
                }

                // 実行.
                var loadYield = observers.WhenAll().ToYieldInstruction(false, cancellationToken);

                yield return(loadYield);

                if (loadYield.HasError)
                {
                    Debug.LogException(loadYield.Error);
                    result = false;
                }

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

                yieldCancel.Dispose();
            }

            stopwatch.Stop();

            if (result)
            {
                var logBuilder = new StringBuilder();

                logBuilder.AppendLine(string.Format("Master : ({0:F1}ms)", stopwatch.Elapsed.TotalMilliseconds));
                logBuilder.AppendLine();

                if (0 < updateLog.Length)
                {
                    logBuilder.AppendLine("---------- Download ----------");
                    logBuilder.AppendLine();
                    logBuilder.AppendLine(updateLog.ToString());
                }

                if (0 < loadLog.Length)
                {
                    logBuilder.AppendLine("------------ Load ------------");
                    logBuilder.AppendLine();
                    logBuilder.AppendLine(loadLog.ToString());
                }

                UnityConsole.Event(ConsoleEventName, ConsoleEventColor, logBuilder.ToString());

                if (onLoadFinish != null)
                {
                    onLoadFinish.OnNext(Unit.Default);
                }
            }
            else
            {
                if (onError != null)
                {
                    onError.OnNext(Unit.Default);
                }
            }

            observer.OnNext(result);
            observer.OnCompleted();
        }