public CollectionFromInt(IReadOnlyReactiveProperty <int> count) { _count = count; _currentCount = _count.Value; count.Subscribe(newCount => { var difference = newCount - _currentCount; if (difference > 0) { for (var i = 0; i < difference; i++) { _observeAdd.OnNext(new CollectionAddEvent <Unit>(_currentCount + i, Unit.Default)); } } else if (difference < 0) { for (var i = 0; i < difference; i++) { _observeRemove.OnNext(new CollectionRemoveEvent <Unit>(_currentCount - i - 1, Unit.Default)); } } _currentCount = newCount; }); }
private void SetHpGauge() { hpPercentage.Subscribe( x => { DOTween.To( () => hpGauge.fillAmount, num => hpGauge.fillAmount = num, x, 1.0f ) .SetLink(gameObject) .Play() ; }) .AddTo(this); hpGauge .ObserveEveryValueChanged(c => c.fillAmount) .Where(x => 0f <= x && x < 0.34) .Subscribe(_ => hpGauge.color = Color.red) ; hpGauge .ObserveEveryValueChanged(c => c.fillAmount) .Where(x => 0.34 <= x && x < 0.67) .Subscribe(_ => hpGauge.color = Color.yellow) ; hpGauge .ObserveEveryValueChanged(c => c.fillAmount) .Where(x => 0.67 <= x) .Subscribe(_ => hpGauge.color = Color.green) ; }
public static IDisposable GetValueAndSubscribe <T>(this IReadOnlyReactiveProperty <T> source, IObserver <T> observer) { var disposable = source.Subscribe(observer); observer.OnNext(source.Value); return(disposable); }
public static Task <T> WaitUntilValueChangedAsync <T>(this IReadOnlyReactiveProperty <T> source, CancellationToken cancellationToken = default(CancellationToken)) { var tcs = new CancellableTaskCompletionSource <T>(); var subscription = source.Subscribe(x => tcs.TrySetResult(x), ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled()); cancellationToken.Register(Callback, Tuple.Create(tcs, subscription), false); return(tcs.Task); }
public static IDisposable Bind <T>(this Text text, IReadOnlyReactiveProperty <T> prop) { if (text != null) { return(prop.Subscribe(x => text.text = x.ToString()).AddTo(text)); } Debug.LogError("Text field not set"); return(null); }
public void Init(ResourceId id, IReadOnlyReactiveProperty <int> amount, IReadOnlyReactiveProperty <int?> limit) { _icon.sprite = _resourcesUiData.GetResourceIcon(id); amount.Subscribe(v => { _amount = v; UpdateView(); }).AddTo(this); limit.Subscribe(v => { _limit = v; UpdateView(); }).AddTo(this); }
public static Task <T> WaitUntilValueChangedAsync <T>(this IReadOnlyReactiveProperty <T> source, CancellationToken cancellationToken = default(CancellationToken)) { var tcs = new CancellableTaskCompletionSource <T>(); var disposable = new SingleAssignmentDisposable(); if (source.HasValue) { // Skip first value var isFirstValue = true; disposable.Disposable = source.Subscribe(x => { if (isFirstValue) { isFirstValue = false; return; } else { disposable.Dispose(); // finish subscription. tcs.TrySetResult(x); } }, ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled()); } else { disposable.Disposable = source.Subscribe(x => { disposable.Dispose(); // finish subscription. tcs.TrySetResult(x); }, ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled()); } cancellationToken.Register(Callback, Tuple.Create(tcs, disposable.Disposable), false); return(tcs.Task); }
/// <summary> /// Data binding method. /// </summary> /// <typeparam name="TView">View type</typeparam> /// <typeparam name="TProperty">Property type</typeparam> /// <param name="self">View</param> /// <param name="propertySelector">Target property selector</param> /// <param name="source">Source property</param> /// <returns>Data binding token</returns> public static IDisposable SetBinding <TView, TProperty>( this TView self, Expression <Func <TView, TProperty> > propertySelector, IReadOnlyReactiveProperty <TProperty> source) where TView : View { var d = new CompositeDisposable(); var setter = AccessorCache <TView> .LookupSet(propertySelector, out var propertyName); source .Subscribe(x => setter(self, x)) .AddTo(d); return(d); }
private void SetSpecialGauge() { specialPercentage.Subscribe( x => { DOTween.To( () => specialGauge.fillAmount, num => specialGauge.fillAmount = num, x, 1.0f ) .SetLink(gameObject) .Play() ; specialGauge.color = new Color(255, 255, 0, specialPercentage.Value + 0.1f); }) .AddTo(this); specialGauge .ObserveEveryValueChanged(c => c.fillAmount) .Where(x => 0f <= x && x < 0.25f) .Subscribe(_ => SetIconUnique(-1)) ; specialGauge .ObserveEveryValueChanged(c => c.fillAmount) .Where(x => 0.25f <= x && x < 0.5f) .Subscribe(_ => SetIconUnique(0)) ; specialGauge .ObserveEveryValueChanged(c => c.fillAmount) .Where(x => 0.5f <= x && x < 1.0f) .Subscribe(_ => SetIconUnique(1)) ; specialGauge .ObserveEveryValueChanged(c => c.fillAmount) .Where(_ => playerMediator.isBuffing) .Subscribe(_ => SetIconUnique(2)) ; }
void ListenForCombo() { #if UNITY_EDITOR if (!UnityEditor.EditorApplication.isPlaying) { return; } #endif comboBindings.Clear(); if (keys.Length == 0) { return; } var keyObservables = keys .Select((key, index) => Observable .EveryUpdate() .Where(_ => Input.GetKey(key)) .Select(_ => Time.time) .ToReadOnlyReactiveProperty() ).ToArray(); IReadOnlyReactiveProperty <float> last = keyObservables[0]; for (int i = 1; i < keys.Length; i++) { var before = last; last = keyObservables[i].SkipUntil(last).Where(_ => Time.time - before.Value <= inbetweenTime).ToReadOnlyReactiveProperty(); } last.Subscribe(_ => { satisfactionProperty.Value = true; Observable.NextFrame().Subscribe(__ => { satisfactionProperty.Value = false; ListenForCombo(); }); }).AddTo(comboBindings); }
public void Initialize(IItem item, bool previewable, IReadOnlyReactiveProperty <string> tagName) { _toggle.interactable = true; _thumbnail.enabled = true; _outline.enabled = true; _tagField.enabled = true; _tagText.enabled = true; _background.sprite = _initializedBackgroundSprite; Item = item; _thumbnail.sprite = item.Thumbnail; tagName.Subscribe(tag => _tagText.text = tag); _inventory.OnStockUpdated += (id, stock) => { if (id == item.Id) { _nonHolding.enabled = stock <= 0; } }; _nonHolding.enabled = _inventory.GetStock(item) <= 0; }
/// <summary> /// UI設定周りのセットアップを行う。 /// </summary> /// <param name="uiConfig">UI設定値。</param> /// <param name="processes">VOICEROIDプロセスコレクション。</param> private void SetupUIConfig( IReadOnlyReactiveProperty <UIConfig> uiConfig, IReadOnlyCollection <IProcess> processes) { // 設定変更時に選択中プロセス反映 Observable .CombineLatest( this.VisibleProcesses, uiConfig .ObserveInnerProperty(c => c.VoiceroidId) .DistinctUntilChanged(), (vp, id) => vp.FirstOrDefault(p => p.Id == id) ?? vp.First()) .DistinctUntilChanged() .Subscribe(p => this.SelectedProcess.Value = p) .AddTo(this.CompositeDisposable); // 選択中プロセス変更時処理 this.SelectedProcess .Where(p => p != null) .Subscribe(p => uiConfig.Value.VoiceroidId = p.Id) .AddTo(this.CompositeDisposable); this.SelectedProcess .Where(p => p == null) .ObserveOnUIDispatcher() .Subscribe( _ => this.SelectedProcess.Value = processes.First(p => p.Id == uiConfig.Value.VoiceroidId)) .AddTo(this.CompositeDisposable); // 実行ファイルパス反映用デリゲート Action <VoiceroidId, string> pathSetter = (id, path) => { // パスが有効な場合のみ反映する if (!string.IsNullOrEmpty(path) && File.Exists(path)) { uiConfig.Value.VoiceroidExecutablePathes[id].Path = path; } }; // UI設定変更時に実行ファイルパスを反映する uiConfig .Subscribe( c => { foreach (var process in processes) { pathSetter(process.Id, process.ExecutablePath); } }) .AddTo(this.CompositeDisposable); // VOICEROIDプロセスの実行ファイルパスが判明したらUI設定に反映する foreach (var process in processes) { var id = process.Id; // 現在値を設定 pathSetter(id, process.ExecutablePath); // 変更時に反映する process .ObserveProperty(p => p.ExecutablePath) .Subscribe(path => pathSetter(id, path)) .AddTo(this.CompositeDisposable); } }
public IDisposable Subscribe(IObserver <TData> observer) => valueData.Subscribe(observer);