Search Unity

Unity - Dropdown scroll using controller or keyboard

Discussion in 'Scripting' started by mrCharli3, Sep 25, 2019.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I have a UI dropdown (Text mesh pro in my case, but should not matter). This dropdown works perfectly with my mouse, but if I try to select values using my controller/gamepad or my keyboard, it does not scroll to the selected value.

    How can I get it to work as you expect when using a controller, i.e the scrollrect will follow the selected element.
     
  2. Steviebops

    Steviebops

    Joined:
    Apr 9, 2013
    Posts:
    132
    I also have this problem, it seems Unity doesn't have a solution.
     
  3. ceast33

    ceast33

    Joined:
    Apr 22, 2020
    Posts:
    9
    I also have this problem!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    Did you check if the navigation on the sub-items is set to AUTOMATIC ?
     
    ceast33 likes this.
  5. lgarczyn

    lgarczyn

    Joined:
    Nov 23, 2014
    Posts:
    68
    Quick and dirty solution

    Code (CSharp):
    1. using System;
    2. using Extensions.Unity;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.InputSystem;
    6. using UnityEngine.UI;
    7.  
    8. namespace UI
    9. {
    10.   [RequireComponent(typeof(ScrollRect))]
    11.   public class ScrollRectAutoScroll : MonoBehaviour
    12.   {
    13.     [Tooltip("The UI/Navigate action used by controller and keyboard to use menus")]
    14.     [SerializeField] InputActionReference navigateAction;
    15.     /// <summary>
    16.     /// The scrollRect attached to this component
    17.     /// </summary>
    18.     ScrollRect _scrollRect;
    19.     /// <summary>
    20.     /// All selectable items inside the scrollrect
    21.     /// </summary>
    22.     Selectable[] _selectables;
    23.  
    24.     public void Start()
    25.     {
    26.       _scrollRect = GetComponent<ScrollRect>();
    27.       _selectables = GetComponentsInChildren<Selectable>();
    28.       ScrollToSelection();
    29.     }
    30.  
    31.     void ScrollToSelection()
    32.     {
    33.       GameObject selection = EventSystem.current.currentSelectedGameObject;
    34.       if (!selection) return;
    35.       if (!selection.transform.IsChildOf(transform)) return;
    36.       if (!selection.TryGetComponent(out RectTransform rectTransform)) return;
    37.       _scrollRect.ScrollToCenter(rectTransform);
    38.       int index = Array.IndexOf(_selectables, rectTransform);
    39.       if (index < 0) return;
    40.       float target = 1f - (float)index / Mathf.Max(_selectables.Length - 2, 1);
    41.       _scrollRect.verticalNormalizedPosition = target;
    42.     }
    43.  
    44.     public void Update()
    45.     {
    46.       if (navigateAction.action.inProgress)
    47.         ScrollToSelection();
    48.     }
    49.   }
    50. }