Search Unity

Searchable Dropdown Issue

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by abedbraff, Feb 14, 2020.

  1. abedbraff

    abedbraff

    Joined:
    Oct 16, 2017
    Posts:
    6
    The project I'm working with is currently on Unity 2019.1

    So, I'd like a dropdown with the following functionality:

    -The ability to type into an input field and update the options in the dropdown accordingly
    -The dropdown list to remain open while typing so that the user can easily select with the mouse. This way they don't have to keep typing until their choice is the top one remaining, and they can simply type until their choice is easily within view to click on (or hit enter in the event that it becomes to top option) to select.

    The former is easy. The latter I can't seem to get. What happens with my current code is that the list opens on one key press, closes on the next, and repeats this process. I can confirm that the dropdown options themselves are properly searched and edited on each iteration.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.EventSystems;
    7.  
    8. public class DropdownSearch : MonoBehaviour
    9. {
    10.     [SerializeField] private Dropdown m_DropdownToSearch;
    11.     private InputField m_SearchField;
    12.     private List<Dropdown.OptionData> m_DropdownOptions;
    13.     private GameObject m_Blocker;
    14.  
    15.     private void Awake()
    16.     {
    17.         m_SearchField = GetComponent<InputField>();
    18.         m_SearchField.onValueChanged.AddListener(SearchDropdown);
    19.         m_DropdownOptions = m_DropdownToSearch.options;
    20.     }
    21.  
    22.     private void SearchDropdown(string _search)
    23.     {
    24.         m_DropdownToSearch.Hide();
    25.  
    26.         m_DropdownToSearch.options = m_DropdownOptions.FindAll(options => options.text.ToLower().IndexOf
    27.             (_search.ToLower()) >= 0);
    28.  
    29.         m_DropdownToSearch.Show();
    30.        
    31.         m_SearchField.Select();
    32.     }
    33.  
    34.     private void LateUpdate()
    35.     {
    36.         m_SearchField.MoveTextEnd(true);
    37.     }
    38. }
    39.  
    This script is on the InputField object. If I get rid of the Hide() call, then the dropdown list stays open, but the options aren't properly updated.
     
  2. abedbraff

    abedbraff

    Joined:
    Oct 16, 2017
    Posts:
    6
    Able to find a sort of solution online that just kicks the problem down the road.

    A coroutine waitforseconds between hide and show works, but this leads to one of two further problems.

    I can reselect the input field immediately. This leads to orphaned Blocker gameobjects being created by the Show() function.

    I can wait to reselect the input field after the waitforseconds delay. This, obviously, leads to a limit to the speed at which the user can type without characters being dropped.

    The first, I'm unsure how to solve. The second, I suppose I could create a simple input buffer system that would alleviate the problem to some extent at least.
     
unityunity