Search Unity

Receive an event when Dropdown is closed?

Discussion in 'UGUI & TextMesh Pro' started by Miguel_TagaiArts, Sep 26, 2018.

  1. Miguel_TagaiArts

    Miguel_TagaiArts

    Joined:
    Jan 12, 2018
    Posts:
    39
    Hi!

    We are having a very particular issue that we are quite sure has a very easy solution, but we can't find the proper way to handle this. Here's our case scenario:

    When we navigate our menus using keyboard (or a controller), when the user interacts with a dropdown option, our menu navigation is disabled so Up/Down only works as a selector inside the dropdown list options. Everything good so far, then when an option from the dropdown is selected, the onValueChanged event is thrown and the dropdown list is closed, so we use that event to give back the control to the UI. The problem comes when the user selects the same option that was already selected in the dropdown, since the onValueChanged event is never called and then the user is stuck because the dropdown list closed but the controls were not given back to our UI system due to the lack of event.

    To solve that, we just need either to know when the dropdown list closes so we can use it instead of the onValueChanged event, or some sort of event/flag/whatever that is always called when an option of the dropdown is selected, no matter if it's the same or not.

    Is there any simple way we can achieve this? We are using Unity v2017.4.11f1 from the LTS support builds, by the way.


    Thank you very much in advance.
     
    ROBYER1 likes this.
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Have you considered writing some code that runs every frame (in an Update() call) that checks whether the dropdown is currently open or not? This is more expensive, but probably not enough to be noticeable, and it means you don't need to play whack-a-mole to make sure you've caught every possible way that the dropdown might open or close.
     
  3. Miguel_TagaiArts

    Miguel_TagaiArts

    Joined:
    Jan 12, 2018
    Posts:
    39
    Sure, I could do that, I was just looking for a less expensive way to do this, specially considering we will be releasing our game on mobile as well and performance optimization is key.
     
  4. Miguel_TagaiArts

    Miguel_TagaiArts

    Joined:
    Jan 12, 2018
    Posts:
    39
    UPDATE: It seems that we just needed to use an EventTrigger component with various selection events in it. And then just have it run the same method that onValueChange calls.

    Here's a screenshot of the setup that worked, in case anyone needs something similar in the future:

     
    ALaurain likes this.
  5. unity_1cGDSF4Bp-7p7g

    unity_1cGDSF4Bp-7p7g

    Joined:
    Jan 16, 2020
    Posts:
    4
    Create a new script and derive it from Dropdown and override CreateDropdownList and DestroyDropdownList methods to do things when dropdown opened and closed
     
    zeimhall, TK009999, fnnbrr and 7 others like this.
  6. SeerSucker69

    SeerSucker69

    Joined:
    Mar 6, 2021
    Posts:
    68
    Put these two lines in your update () to set a boolean flag marking the drop box in use.
    Code (CSharp):
    1.         if (MyDropBox.transform.childCount != 3 && !MyDropBoxDown)   { MyDropBoxDown   =  true;}    // Detect dropbox down and set a bool flag
    2.         if (MyDropBox.transform.childCount == 3 && MyDropBoxDown)   { MyDropBoxDown   =  false;}   // detect dropbox up and unset a bool flag
     
  7. zeimhall

    zeimhall

    Joined:
    Jun 30, 2017
    Posts:
    3
    In my case, I was using TMPro, so I did this did exactly what Unity_1cGDSF... said:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using TMPro;
    4.  
    5. public class ScrollDropdown : TMP_Dropdown
    6. {
    7.     protected override GameObject CreateDropdownList(GameObject template)
    8.     {
    9.         GameObject dropdownList = base.CreateDropdownList(template);
    10.         // Do your stuff here or before dropdown list is created.
    11.         return dropdownList;
    12.     }
    13.  
    14.     protected override void DestroyDropdownList(GameObject dropdownList)
    15.     {
    16.         // Do your stuff here.
    17.     }
    18. }
    19.  
    20.