void Start() { Button button = transform.Find("Button").GetComponent <Button>(); Toggle toggle = transform.Find("Toggle").GetComponent <Toggle>(); button.OnClickAsObservable() .Subscribe(_ => { Debug.Log("Button on clicked"); }) .AddTo(this); toggle.OnValueChangedAsObservable() .Subscribe(on => { if (on) { Debug.Log("toggle is on"); } }); // toggle 通过Where过滤 简化 toggle.OnValueChangedAsObservable() .Where(on => !on) .Subscribe(on => { Debug.Log("toggle is off"); }); // 对带有Ragcast Target标签的Graphic类型如(Text,Image,Button等),进行拖拽监听 Graphic imgGraphic = transform.Find("Image").GetComponent <Graphic>(); imgGraphic.OnBeginDragAsObservable().Subscribe(_ => Debug.Log("开始拖拽了!")); imgGraphic.OnDragAsObservable().Subscribe(_ => Debug.Log("dragging")); imgGraphic.OnEndDragAsObservable().Subscribe(_ => Debug.Log("end drag")); imgGraphic.OnPointerClickAsObservable().Subscribe(clickEvent => { }); mEvent.AsObservable() .Subscribe(_ => { // do something }) .AddTo(this); }
void Start() { stopped = true; MenuController.LoseEvent.AddListener(() => { ResetplayerObject(); }); StartGameEvent.AddListener(() => stopped = false); var input = this.gameObject.AddComponent <InputScript>(); input.InputScriptInit(UpInput, DownInput, RightInput, LeftInput); UpInput.AsObservable().Where(_ => moveDir != MovementDir.down).Subscribe(_ => { transform.LookAt(transform.position + Vector3.forward); moveDir = MovementDir.up; }); DownInput.AsObservable().Where(_ => moveDir != MovementDir.up).Subscribe(_ => { transform.LookAt(transform.position + -Vector3.forward); moveDir = MovementDir.down; }); RightInput.AsObservable().Where(_ => moveDir != MovementDir.left).Subscribe(_ => { transform.LookAt(transform.position + Vector3.right); moveDir = MovementDir.right; }); LeftInput.AsObservable().Where(_ => moveDir != MovementDir.right).Subscribe(_ => { transform.LookAt(transform.position + -Vector3.right); moveDir = MovementDir.left; }); transform.UpdateAsObservable().Where(_ => stopped == false) .Subscribe(x => transform.position += transform.forward * Time.deltaTime * baseMovementSpeed * movementSpeedMultiplier); MenuController.LoseEvent.AddListener(() => movementSpeedMultiplier = 1); }