我想使用UniRx获取输入并还向UI触发一些事件。所以我创建了该输入系统和行为。所有这些都正常工作,但我不确定我是否正确使用了此UniRX?我没有使用IObservable,因为我无法确定如何为其设置值。
我还应该在以后处置此订阅吗?
我的输入类别示例:
public override void RegisterListeners()
{
Movement = Observable.EveryFixedUpdate()
.Select(_ =>
{
var x = Input.GetAxis(horizontalAxisName);
var y = Input.GetAxis(verticalAxisName);
return new Vector2(x, y);
}).Where(x => x != Vector2.zero).ObserveOn(Scheduler.MainThread);
CameraMovement = this.UpdateAsObservable()
.Select(_ =>
{
var x = Input.GetAxis(cameraHorizontalAxisName);
var y = Input.GetAxis(cameraVerticalAxisName);
return new Vector2(x, y).normalized;
}).ObserveOn(Scheduler.MainThread);
CrunchButtonDown = this.UpdateAsObservable()
.Select(_ =>
{
var x = Input.GetKeyDown(crunchKey);
return x;
}).Where(x => x == true).Distinct(); ...
在步枪类上的用法:
private void RegisterListeners()
{
if (_subscription == null)
{
return;
}
_subscription.Add(gameManager.activeInputManager.UseButtonPress.Subscribe(x => Use()));
_subscription.Add(gameManager.activeInputManager.ReloadButtonDown.Subscribe(x => Reload()));
}
我的其他用法:
private BehaviorSubject<int> currentHealth;
... 设置默认参数
currentHealth = new BehaviorSubject<int>(playerInfo.playerSetting.startHealth);
设置值:
currentHealth.OnNext(currentHealth.Value + value);