Dropdown Automatically update/select option on button up.
I wanted the Dropdown List to work with 1 button and the joypad/arrow keys. The user would hold press the button to expand the dropdown. Use the joypad or arrow keys to select a new value. On release of the button the new value is selected. I have a hard time obtaining the Dropdown.Dataoption index.
My approach is to use the name of the current selected object in the EventSystem. The Toggle seems to have a name "Item 0: Option A." where 0 is the index. Then Trimming the name I can obtain the index. This seems as a limited solution and I wonder if there is a better way to get the index of the highlighted Dropdown item using the EventSystem and/or Dropdown object.
here is my code and workaround:
public string ControllerButtonName = "ActionBtn01"; public MenuItemList MenuItems; public TextAsset MenuOptionsFile; public int downDelay=20; private int downDelayCount; private Dropdown ActionDropMenu;
void Update() { if (Input.GetButtonUp(ControllerButtonName)) { //Hide Options ActionDropMenu.Hide(); downDelayCount = 0; ActionDropMenu.RefreshShownValue(); } if (Input.GetButton(ControllerButtonName)) { //Show Options downDelayCount++; if (downDelay <= downDelayCount) { ActionDropMenu.Show(); } //split string of selected object and parse the index. if (EventSystem.current.currentSelectedGameObject != null) { int index = int.Parse(EventSystem.current.currentSelectedGameObject.name.Split(':')[0].Replace("Item ", "")); ActionDropMenu.value = index; } } }