UniRx的ReactiveCommand
定义
public interface IReactiveCommand<T> : IObservable<T>
{
IReadOnlyReactiveProperty<bool> CanExecute { get; }
bool Execute(T parameter);
// Excute方法是被外部使用的,外部调用的Execute就会执行Command。
// CanExcute是内部使用的,并且对外部提供了只读访问。当它为false的时候,外部调用Execute则Command不会被执行。
// 新创建的对象CanExecute默认为true。
}
示例
var stream1 =Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonDown(0))
.Select(_ => true);
var stream2 = Observable.EveryUpdate()
.Where(_ => Input.GetMouseButtonUp(0))
.Select(_ => false);
var mergedStream = Observable.Merge(stream1, stream2);
var reactiveCommand = new ReactiveCommand(mergedStream, false);
reactiveCommand.Subscribe(x =>{ Debug.Log(x); });
Observable.EveryUpdate()
.Subscribe(_ =>
{
reactiveCommand.Execute();
});
效果是按下鼠标时持续输出,松开时则停止输出。
(END)