【Unity】【UniRx】AsyncReactiveCommand で処理中は複数のボタンを押せなくする
using System; using UnityEngine; using UnityEngine.UI; using UniRx; public class Sample : MonoBehaviour { // ボタン 1 [SerializeField] private Button _button1; // ボタン 2 [SerializeField] private Button _button2; // ボタンの押下許可 private BoolReactiveProperty _sharedCanExecuteSource = new BoolReactiveProperty(true); private void Start() { var command1 = new AsyncReactiveCommand(this._sharedCanExecuteSource); command1.BindToOnClick(this._button1, _ => { return Observable.Timer(TimeSpan.FromSeconds(1)) .ForEachAsync(__ => Debug.Log($"button1")); }); var command2 = new AsyncReactiveCommand(this._sharedCanExecuteSource); command2.BindToOnClick(this._button2, _ => { return Observable.Timer(TimeSpan.FromSeconds(1)) .ForEachAsync(__ => Debug.Log($"button2")); }); } }
省略して下記のように書くこともできる。
using System; using UnityEngine; using UnityEngine.UI; using UniRx; public class Sample : MonoBehaviour { // ボタン 1 [SerializeField] private Button _button1; // ボタン 2 [SerializeField] private Button _button2; // ボタンの押下許可 private BoolReactiveProperty _sharedCanExecuteSource = new BoolReactiveProperty(true); private void Start() { this._button1 .BindToOnClick(this._sharedCanExecuteSource, _ => { return Observable.Timer(TimeSpan.FromSeconds(1)) .ForEachAsync(__ => Debug.Log($"button1")); }); this._button2 .BindToOnClick(this._sharedCanExecuteSource, _ => { return Observable.Timer(TimeSpan.FromSeconds(2)) .ForEachAsync(__ => Debug.Log($"button2")); }); } }