Search Unity

Dropdown not working as desired

Discussion in 'UGUI & TextMesh Pro' started by hollym16, Aug 30, 2016.

  1. hollym16

    hollym16

    Joined:
    Feb 21, 2013
    Posts:
    13
    I have a dropdown menu that selects the language for the App. However, if you press and slightly drag when its on a device, it doesn't select the option, it only highlights it.
    So I tried to add an End Drag Event Trigger in order to make the function work as the user stopped pressing.
    With the OnClick() function, the public void is a Dynamic int as opposed to a Static Parameter. This means the value is assigned at runtime rather that manually preselecting it.
    With the End Drag Event Trigger, I'm only given the option to go for the Static Parameter, therefore, whatever I click on in runtime, it will always default to whatever it assigned in the Inspector.
    Can anyone help with a solution? Or even suggest a different way of doing this.
    Here's the script I'm using:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Events;
    4.  
    5. public class DropDownMenu : MonoBehaviour {
    6.    
    7. public Dropdown myDropdown;
    8. public static string key = null;
    9. public CanvasGroup ok;
    10.  
    11. void Start() {
    12.     myDropdown.onValueChanged.AddListener(delegate {});
    13.         ok.alpha = 0f;
    14.         ok.blocksRaycasts = false;
    15. }
    16. void Destroy() {
    17.     myDropdown.onValueChanged.RemoveAllListeners();
    18. }
    19.  
    20.     public void SetDropdownIndex(int index) {
    21.         myDropdown.value = index;
    22.         if (index == 1) {
    23.             key = "English";
    24.             Debug.Log ("English");
    25.             TextLocalization.SelectLanguage ("English");
    26.             ok.alpha = 1f;
    27.             ok.blocksRaycasts = true;
    28.         }
    29.         if (index == 2) {
    30.             key = "German";
    31.             Debug.Log ("German");
    32.             TextLocalization.SelectLanguage ("German");
    33.             ok.alpha = 1f;
    34.             ok.blocksRaycasts = true;
    35.         }
    36.         if (index == 3) {
    37.             key = ("Dutch");
    38.             Debug.Log ("Dutch");
    39.             TextLocalization.SelectLanguage ("Dutch");
    40.             ok.alpha = 1f;
    41.             ok.blocksRaycasts = true;
    42.         }
    43.         if (index == 4) {
    44.             key = ("French");
    45.             Debug.Log ("French");
    46.             TextLocalization.SelectLanguage ("French");
    47.             ok.alpha = 1f;
    48.             ok.blocksRaycasts = true;
    49.         }
    50.     }
    51. }
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I'm a little confused about what your asking. How are you setting up this dropdown? It sounds like your settiing somethings up in the editor and then setting some items at run time? It would help if you specifically laid out what is being setup where. Specifically how are you setting up the drop down items, the OnClickHandler, and the DragEventHandler.

    This line here:
    Implies your setting up some items in the dropdown in the inpsector, then later at runtime changing them?
     
  3. hollym16

    hollym16

    Joined:
    Feb 21, 2013
    Posts:
    13
    I create the dropdown menu by going to Gameobject > UI > Dropdown. This is basically like a preset. Then the script I added was one I create myself, this is then added to the OnValueChanged section in the Inspector for the Dropdown, specifying the SetDropDownIndex which is under the heading of Dynamic int.

    The issue comes when I put it onto a device with a small screen. Because the buttons are so small, if you don't tap quickly and precisely, it only highlights the option rather than selecting it. You can reproduce the issue by pressing on an option, holding and dragging away. Thats effectively whats happening when the user doesn't tap precisely.

    The Event Trigger was an idea to fix this, so the function would pass as they released the button but I don't think this is necessarily the right way of doing it.
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I see. If they hold the button down too long it triggers putting them in a Drag mode. Then they release and drag ends, but no ButtonClick Events are generated from a Drag Event.

    Where are you setting the EndDrag event. On the DropDown itself? If so you could go into DropDown->Template->ViewPort->Content->Item and add your own custom script that handes OnDragEnd. Then you could compare ItemLabel component of the object that called OnDragEnd to see which label was dragged. Then set DropDown.value based on that. Kind of clunky.

    The other option is to do some research on handling LongPress events in Unity. I believe thats the term used when a user holds the button long enough to start a drag.
     
  5. hollym16

    hollym16

    Joined:
    Feb 21, 2013
    Posts:
    13
    Yes, I ended up using a workaround way.

    I manually made a drop down menu using buttons, this way I could add the DragEnd function with no problem.
    Like you said, it's clunky. But at least I got it working, just a shame the Dropdown preset wasn't usable for this purpose.

    Thank you for the help
     
  6. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    this is for touch?

    So... User can press and it would show the dropdown options?

    And all the while finger is depressed?
    And if they moved the still depressed finger, it highlights the option finger is presently over?
    And if the finger touch Ends (user raised their finger), while over another of the options, it gets selected and the dropdown is gone?

    Thinking to myself:
    Hmmm... so does the main Drop Down act as a Drag Source... and the Drop Down Listed Items act as Drop Targets?
    Well, it might be Drag and Drop as far as events are concerned.. but No image is following the finger drag. ;)
     
  7. hollym16

    hollym16

    Joined:
    Feb 21, 2013
    Posts:
    13
    The dropdown is a tap function that brings up the options, the issue comes if someone presses and holds one of the options and drags even a little. Obviously it's not a natural instinct to press and drag but when the drop down menu is on smaller screens, this can be done by accident. This is where I'm seeing the problem.