Search Unity

[SOLVED] UniRx [Need Help] - Mouse drag works first time ONLY

Discussion in 'Scripting' started by hornetfire, Jun 12, 2017.

  1. hornetfire

    hornetfire

    Joined:
    Mar 24, 2017
    Posts:
    5
    I've been trying to work out why I can't get a UniRx drag stream to work more than once in my application.
    Once the mouseDown stream starts the mouseDrag stream kicks in and when mouseUp stream starts the mouseDrag stops (the first time only).


    But even with RepeatUntilDestroy (Also tried: RepeatSafe and Repeat => Danger) the drag stream never notifies it's subscribed method after this first set of action and I have spent a week trying to get, what I think should be very simple solution, to work.

    Any help would be awesome... even any tips on debugging UniRx would be great.

    Throw this class onto your camera to replicate:

    Code (CSharp):
    1. using UnityEngine;
    2. using UniRx;
    3. using UniRx.Triggers;
    4. using System;
    5.  
    6. namespace MyNameSpace
    7. {
    8.     public class MouseInputObserver : MonoBehaviour
    9.     {
    10.        
    11.         private GameObject _mainCamera;
    12.         public ReadOnlyReactiveProperty<Vector3> mouseDownOnCharacter { get; private set; }
    13.         public ReadOnlyReactiveProperty<Vector3> mouseUpFromCharacter { get; private set; }
    14.         public ReadOnlyReactiveProperty<Vector3> mouseDragFromCharacter { get; private set; }
    15.         void Awake()
    16.         {
    17.             _mainCamera = Camera.main.gameObject;
    18.  
    19.              mouseDownOnCharacter = (_mainCamera).UpdateAsObservable()
    20.                 .Where(_ => Input.GetMouseButtonDown(0))
    21.                 .Select(_ => Input.mousePosition)
    22.                 .ToReadOnlyReactiveProperty();
    23.  
    24.             mouseUpFromCharacter = (_mainCamera).UpdateAsObservable()
    25.                 .Where(_ => Input.GetMouseButtonUp(0))
    26.                 .Select(_ => Input.mousePosition)
    27.                 .ToReadOnlyReactiveProperty();
    28.  
    29.             mouseDragFromCharacter = (_mainCamera).UpdateAsObservable()
    30.                 .SkipUntil(mouseDownOnCharacter)
    31.                 .TakeUntil(mouseUpFromCharacter)
    32.                 .Select(_ =>  Input.mousePosition)
    33.                 .RepeatUntilDestroy(_mainCamera)
    34.                 .ToReadOnlyReactiveProperty();
    35.         }
    36.  
    37.         void Start()
    38.         {
    39.             observePointerInput();
    40.         }
    41.  
    42.         void observePointerInput()
    43.         {
    44.             mouseDownOnCharacter.Subscribe(hitInfo =>
    45.             {
    46.                 Debug.Log($"DOWN>>>>>{hitInfo}");
    47.             }).AddTo(_mainCamera);
    48.  
    49.             mouseUpFromCharacter.Subscribe(hitInfo =>
    50.             {
    51.                 Debug.Log($"UP>>>>>{hitInfo}");
    52.             }).AddTo(_mainCamera);
    53.  
    54.             mouseDragFromCharacter.Subscribe(hitInfo =>
    55.             {
    56.                 // This works on the first time and then never again
    57.                 // I was under the impression that RepeatUntilDestroy would solve this
    58.                 Debug.Log($"DRAG>>>>>{hitInfo}");
    59.             })
    60.             .AddTo(_mainCamera);
    61.         }
    62.     }
    63. }
     
  2. hornetfire

    hornetfire

    Joined:
    Mar 24, 2017
    Posts:
    5
    Anyone got any advise on a better place to ask this question?
     
  3. hornetfire

    hornetfire

    Joined:
    Mar 24, 2017
    Posts:
    5
    gareth_untether likes this.