Search in Dropdown options
Hi, I have a dropdown an there are a few options in it. I want click to inputfield and type something, inputfield shows me is there any words in dropdown. For example I typed ""am"" and finds me americo vespuci in dropdown. İs it possible?
Answer by Hellium · Nov 08, 2018 at 09:48 AM
1. Create a new script called DropdownFilter.cs
2. Copy-paste the following script
public class DropdownFilter : MonoBehaviour
{
[SerializeField]
private InputField inputField;
[SerializeField]
private Dropdown dropdown;
private List<Dropdown.OptionData> dropdownOptions;
private void Start()
{
dropdownOptions = dropdown.options;
}
public void FilterDropdown( string input )
{
dropdown.options = dropdownOptions.FindAll( option => option.text.IndexOf( input ) >= 0 );
}
}
3. Create the input field and dropdown UI elements
4. Attach the script to the gameObject you want (an empty in your scene or the dropdown, or the input field, ...) you just have to make sure the gameObject is enabled when the scene starts
5. Drag & drop the inputfield and dropdown in the appropriate fields in the inspector, when the gameObject holding the script is selected
6. Add a new event in the OnValueChanged
of the input field. Drag & drop the gameObject created step #5 (holding the DropdownFilter
script) and select the FilterDropdown
function (`DynamicString`)
Hello @Hellium . This script works well... however there are no control if "no items is found". There is a way to check to avoid this issue?
For example : FindAll cannot find string. I want to force Dropdown to stay at position 1. How to do this check and force position?
Thank you.
public void FilterDropdown( string input )
{
List<Dropdown.OptionData> filteredOptions = dropdownOptions.FindAll( option => option.text.IndexOf( input ) >= 0 );
if(filteredOptions.Count > 0)
dropdown.options = filteredOptions;
}
Hi, This is amazing, but I'm facing an important problem.
Let's say I have these items on my dropdown:
Item 10 Item 23 Item 33
If I type "3" on the input Field, it will show: Item 23 Item 33
But when I select the first result (item 23) it loads Item 10. If I select Item 33, it loads Item 23.
It's like changing the names, but keeping the old index.
Any guess on how to solve this?
Well, how can I work with this without case sensitivity?
Answer by Jaeger47 · Jul 24, 2020 at 10:09 AM
Watch This https://www.youtube.com/watch?v=ncu_GdhPNVQ
I wrote this script to use in Editor. Maybe you can use as an example and make some changes.
#if UNITY_EDITOR
using UnityEngine;
using TMPro;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
[CreateAssetMenu(menuName = "DropDownPopulator")]
public class PopulateDropDownSO : ScriptableObject
{
public string DropdownName;
[SerializeField] private List<c_OptionData> countries = new List<c_OptionData>();
[ContextMenu("Populate CountryOptionData With Names")]
public void PopulateCountryOptionDataWithNames()
{
var nameList = GetCountryNames();
for (int i = 0; i < nameList.Count; i++)
{
c_OptionData data = new c_OptionData();
data.Text = nameList[i];
countries.Add(data);
}
}
[ContextMenu("Populate Dropdown With Countries")]
public void PopulateWithCountryNames()
{
if (string.IsNullOrEmpty(DropdownName))
{
Debug.Log("DropdownName field is empty");
return;
}
var dropdownGO = GameObject.Find(DropdownName);
TMP_Dropdown dropdown;
var result = false;
if(dropdownGO != null)
{
var text = "Dropdown found. Parent name : " + dropdownGO.transform.parent.name;
result = UnityEditor.EditorUtility.DisplayDialog("Dropdown Found", text, "Continue", "Cancel");
}
else
{
UnityEditor.EditorUtility.DisplayDialog("", "Couldnt find dropdown named : " + "dropdown", "Ok");
return;
}
if (result)
{
dropdown = dropdownGO.GetComponent<TMP_Dropdown>();
dropdown.ClearOptions();
dropdown.AddOptions(CreateOptionDataFromCustom());
}
}
private List<TMP_Dropdown.OptionData> CreateOptionDataFromCustom()
{
var optionDataList = new List<TMP_Dropdown.OptionData>();
foreach (var item in countries)
{
var data = new TMP_Dropdown.OptionData(item.Text, item.Flag);
optionDataList.Add(data);
}
return optionDataList;
}
private List<string> GetCountryNames()
{
var list = new List<string>();
var countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(c =>
new RegionInfo(c.LCID)).Distinct().OrderBy(c => c.EnglishName).ToList();
list.Add("None");
for (int i = 0; i < countries.Count; i++)
{
list.Add(countries[i].DisplayName);
}
return list;
}
[System.Serializable]
public struct c_OptionData
{
public string Text;
public Sprite Flag;
}
}
#endif
--For some reason i couldnt edit previous reply.-- After creating a script (make sure script name matches with the class name) you need to create an Scriptable Object in Somewhere in Assets folder. For creating the SO go to Assets and right click, you will see an option named "DropDownPopulator". Then you can use methods that has [ContextMenu] attribute on it. To use methods select the SO and in the inspector right click to the name of it and you will see the described names of methods like "Populate Dropdown With Countries". To create an example you need an active TMP_Dropdown in your scene and you have to correctly type the name of it to Dropdown Name field.
Your answer
Follow this Question
Related Questions
Autocomplete Search (Search Suggestion) 5 Answers
How to put value on dropdown search like normal dropdown 0 Answers
FPS drop down after some time (Android) 1 Answer
Disable a dropdown option. 1 Answer
Dropdown.ClearOptions does not work 1 Answer