Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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,064
    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:
    752
  3. vrezzoug

    vrezzoug

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

    iwillbenice

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

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    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