Search Unity

Feature Request UI Scroll using XR controller thumbstick

Discussion in 'XR Interaction Toolkit and Input' started by MaskedMouse, Oct 14, 2021.

  1. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Using the thumbsticks to control the UI Scroll Bar would be great. It doesn't seem to be built in.
    I've mentioned it before but it never got picked up I guess.
     
    vrezzoug likes this.
  2. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    755
  3. vrezzoug

    vrezzoug

    Joined:
    Feb 25, 2021
    Posts:
    3
  4. iwillbenice

    iwillbenice

    Joined:
    Jun 7, 2013
    Posts:
    22
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Since it still isn't implemented as far as I can tell with the latest XRI 2.3.1
    This is an own implementation example for anyone who needs it.
    It's quite straightforward and simple.

    Code (CSharp):
    1. public class ThumbstickScrollListener : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    2. {
    3.     public InputActionProperty ScrollActionProperty;
    4.     private int pointerID = int.MinValue;
    5.  
    6.     [SerializeField]
    7.     private Scrollbar VerticalScrollbar;
    8.  
    9.     [SerializeField]
    10.     private Scrollbar HorizontalScrollbar;
    11.  
    12.     public void OnPointerEnter(PointerEventData eventData)
    13.     {
    14.         if (pointerID != int.MinValue) return;
    15.  
    16.         // Store the pointer ID as reference
    17.         pointerID = eventData.pointerId;
    18.         ScrollActionProperty.action?.Enable();
    19.     }
    20.  
    21.     public void OnPointerExit(PointerEventData eventData)
    22.     {
    23.         // Only if the stored pointer ID is the same as this one
    24.         if (eventData.pointerId != pointerID) return;
    25.  
    26.         // Reset pointer ID reference
    27.         pointerID = int.MinValue;
    28.         ScrollActionProperty.action?.Disable();
    29.     }
    30.  
    31.     public void Update()
    32.     {
    33.         if (pointerID == int.MinValue)
    34.             return;
    35.  
    36.         if (ScrollActionProperty.action is null or { enabled: false })
    37.             return;
    38.  
    39.         var scrollDelta = ScrollActionProperty.action.ReadValue<Vector2>();
    40.         AddScrollDelta(HorizontalScrollbar, scrollDelta.x);
    41.         AddScrollDelta(VerticalScrollbar, scrollDelta.y);
    42.     }
    43.  
    44.     private void AddScrollDelta(Scrollbar bar, float value)
    45.     {
    46.         if (bar == null) return;
    47.         bar.value = Mathf.Clamp(bar.value + value, 0f, 1f);
    48.     }
    49. }
    These are the settings of the input action.

    upload_2023-4-11_14-41-36.png
     
    Last edited: Apr 11, 2023
  6. CircusCharlie

    CircusCharlie

    Joined:
    Jul 22, 2014
    Posts:
    38
    Thanks a lot