Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

A working, stylable combo box (drop down list)

Discussion in 'UGUI & TextMesh Pro' started by jbooth, Aug 24, 2014.

  1. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    I wanted to implement something very important, but forgot what exactly it is :D Will upload an update as soon, as will remember.
    By the way, if anyone is interested, I am currently working on Calendar. Almost finished it. Then will additionally implement DatePicker.
     
  2. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Got it!
    Fixed overlay on screen size change. You should manually call UpdateGraphics(). Also, made some methods private.

    Please let me know if You would like to have any other features.
     

    Attached Files:

    soportelsp likes this.
  3. soportelsp

    soportelsp

    Joined:
    Sep 10, 2014
    Posts:
    2
    Great Job!!!!!, Thanks
     
  4. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
    When I click on this combobox, it changes all my hierarchy order (I'm using some layout elements). Do you know why @Kender ?
     
  5. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Hello, @daybson.paisante. Sory for a long response.
    Yes, that's done for making dropdown list above all of the UI elements, so it forces all dropdown parents to be the last in their hierarchy.
     
    Last edited: Jan 23, 2015
  6. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
    @Kender
    Yeah, but doing this the rendering order of my ui objects are affected. The items should be in front of my image by default, but your component is changing it :

     
  7. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
  8. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,961
    Never had that issue, must be something with your GUI setup i think.
     
  9. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    I believe, this may happen because image is lower then combobox. I just don't know how to sort root items. If You put image over combobox's root, this issue will disappear. Please let me know if this helps.
     
  10. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
    I imported all combobox package again and the problem is gone. Don't know why.
    But this issue persists: by the sort order of my UI groups (Layout Elements components), the combobox needs to be before the other image, causing the effect above.
    Need to have some way to set an rendering priority on a UI item without affecting the hierarchy...
     
  11. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,961
    Any way to fix this issue? https://db.tt/334xqUif
    Detect if if goes out of the screen and instead draw it upwards?
     
  12. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    I'll think about fixes.
     
  13. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Fixed both issues.
     

    Attached Files:

  14. jeff-smith

    jeff-smith

    Joined:
    Feb 24, 2012
    Posts:
    19
    Very nice combobox. One suggestion I would make is to put your C# code in your own namespace. Maybe something like:

    namespace Kender
    {
    ...
    }

    The ComboBox class name conflicts with another ComboBox in GameAnalytics.
     
  15. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Sure. Here is a new one:
     

    Attached Files:

  16. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Fixed elements offset bug, caused by the last update.
     

    Attached Files:

    davis252 likes this.
  17. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    406
    I'm giving it a try on Unity 5.0 RC1 and it doesn't appear to be working for me.

    When I click on the button in play mode, nothing happens, and in the scene mode the ScrollPanel just shows up with a red X
     
  18. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Sorry, but I don't have Unity 5 to test it :(
     
  19. dkely

    dkely

    Joined:
    Mar 3, 2014
    Posts:
    22
    Thanks for all the updates. This is an amazing package!
    Is there an existing way to set an item as selected and have the event fire?

    ItemClicked is almost what i want but if the 0 index item is the one you wish to set, it will not fire the event as the index has not changed. Also it calls ToggleComboBox(true). To make it work i added this function which i call instead:

    public void MimicItemClicked(int index)
    {
    SelectedIndex = index;
    //ToggleComboBox(true);
    if (OnSelectionChanged != null)
    OnSelectionChanged(index);
    }

    I want this because in my program, depending on which item is selected, it will redraw the scene. The function to deal with that is triggered by the OnSelectionChanged(). It would be neater if the default selection that gets loaded from the save file at the program start also triggered this.
     
    Last edited: Feb 13, 2015
  20. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    I tried to use your combobox but when I click on it I get following error message:
    I may add that I try to use your combobox as custom control in this listview:
    https://www.assetstore.unity3d.com/en/#!/content/21430

    I have no idea if the cause of the problem is in your combobox or in that listview but it would be great if I could use it together
     
  21. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Sorry for a long response. I have implemented a new version, that now contains:

    Action<int> OnItemSelected
    Fires when item was selected​
    void SelectItem(int index)
    Selects item​

    Also, I have implemented a bugfix for cases when combobox has non-standard pivot. Also, scroll handle does not break now.
     

    Attached Files:

  22. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Sorry, but I have no availability for checking this bug, since I do not have ListView plugin paid-up. Anyway, I guessed which problem could apply, so my previous message contains an update of ComboBox, that may solve this issue. Please let me know if this helps. Thanks in advance!
     
  23. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    406
    Either whatever you changed, or the bump up to Unity 5.0RC2 seems to have fixed it, it is now working correctly in Unity 5
     
  24. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
    Glad to know it!
     
  25. mechangel

    mechangel

    Joined:
    Nov 6, 2012
    Posts:
    73
    Hello, I have tried importing the latest package into a new project, but immediately get a lot of errors (pasted below).
    I am using 4.6 on Windows, is there a missing step I need to do to run the test scene? Thank you!

    (Filename: Assets/ComboBox/Scripts/ComboBox.cs Line: 683)

    (Filename: Assets/ComboBox/Scripts/ComboBox.cs Line: 685)

    Assets/ComboBox/Scripts/ComboBox.cs(686,57): error CS1061: Type `UnityEngine.RectTransform' does not contain a definition for `SetSizeWithCurrentAnchors' and no extension method `SetSizeWithCurrentAnchors' of type `UnityEngine.RectTransform' could be found (are you missing a using directive or an assembly reference?)

    (Filename: Assets/ComboBox/Scripts/ComboBox.cs Line: 686)

    Assets/ComboBox/Scripts/ComboBox.cs(697,85): error CS1061: Type `UnityEngine.UI.ScrollRect' does not contain a definition for `scrollSensitivity' and no extension method `scrollSensitivity' of type `UnityEngine.UI.ScrollRect' could be found (are you missing a using directive or an assembly reference?)
     
  26. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    284
  27. mechangel

    mechangel

    Joined:
    Nov 6, 2012
    Posts:
    73
    Still having the same issues, but 4.6.0b17 is what my version says, I'll try a recent patch release to see if that fixes it. Hope it does, this Combobox looks pretty awesome!
     
  28. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    406
    It works for me on 4.6.2, and on 5.0rc2

    I think scrollbar sensitivity wasn't added until 4.6 b18 ...
     
  29. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,961
    Why work with an old beta version anyways, thats begging for trouble : p
     
  30. mechangel

    mechangel

    Joined:
    Nov 6, 2012
    Posts:
    73
    I upgraded to 4.6.2p2 and the errors went away, however I am not displaying any boxes in the Overlay object that gets created in the Canvas while running the scene. I wasn't sure whether to assign a script to the Canvas or not, I tried putting the test script on it.
     

    Attached Files:

  31. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    406
    When you select the overlay in the scene view, do you see a red X where the scroll view should be in the Scene view?
     
  32. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,961
    What about this bug then? : p

    https://db.tt/lF5KSSkS

    When i put the Game screen in the editor to full screen this happens, works ok otherwise.
    Im pretty sure it worked before i upgraded to one of your newer packages.
     
  33. mechangel

    mechangel

    Joined:
    Nov 6, 2012
    Posts:
    73
    I do not, it toggles active and not when I click the button that's displaying in the Play view but doesn't actually draw for me. Does the TestComboBox.cs go on the Canvas?
     
  34. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    406
    It should go on it's own RectTransform I think.

    I had better results creating a new one using the GameObject... UI... ComboBox from the menu rather than creating a rect transform and adding the script.
     
  35. mechangel

    mechangel

    Joined:
    Nov 6, 2012
    Posts:
    73
    I don't know how you'd create just a RectTransform with no object. I did try making one using the UI - Editor script way. It just still won't display the Overlay. =/
     
  36. subtledark

    subtledark

    Joined:
    Feb 11, 2014
    Posts:
    4
    @mechangel: I'm stuck as well using Unity 4.6.3f1. The example test scene does not have a functioning ComboBox--received various reference errors which I tried to resolve but, not know what to wire to what I gave up.

    I then tried DanSuperGP's suggestion and created ComboBox from editor menu. Add two items to ComboBox: "Foo" and "Bar"

    Based on other comments, I tired adding TestComboBox on an empty gameobject with a RectTransform and told it to reference the ComboBox.

    Note: if you create an empty GameObject as child of Canvas it automatically has RectTransform component instead of just Transform.

    Hope this is a newer version issue and not an I'm dumb issue. :)
     
  37. dkely

    dkely

    Joined:
    Mar 3, 2014
    Posts:
    22
    I have my "CBOverlay" object come in on my main canvas... but it no longer appears at the right location just below the list. I guess this is because of the deep hierarchy that needs to be traversed to get to the comboBox.
    I tried setting the parent to be the combo box object which fixed the position problem but now there are layer issues with it coming behind other objects - i guess that is why you put it to the outer most canvas in the first place?
    Am i experiencing a known issue or should it not matter where the combo box is? I am assuming there is no code to account for it and i am currently trying to work on my own solution. Will post back when i get one.
     
  38. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    406
    One way is to create a new game object and then add a rect transform component.

    Also if you right click on a canvas (or any child of a canvas) in the hierarchy and create a new game object as a child... it will automatically be created with a rect transform
     
  39. koekeritis

    koekeritis

    Joined:
    Feb 25, 2015
    Posts:
    2
    I dont't really understand how to get the selected item and it's index or name. May you give me a code example please?
     
  40. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,961
    Super easy:
    selectedIndex = myComboBoxRef.selectedIndex;
    selectedname = myComboBoxRef.items[selectedIndex].Caption;
     
  41. stijnvdbBiodroid

    stijnvdbBiodroid

    Joined:
    Nov 20, 2014
    Posts:
    10
    Thanks a lot for this, @DanSuperGP ! Saved me so much time :) The code is really well-written too. You'd be surprised at the kind of horrible coding I've encountered in some (paid!) asset store products.

    I had some resolution issues with this, the menu would pop up in funky places sometimes (just in height). Not sure if anyone else has that (I changed some stuff in the code, might well have caused it myself). Either way here's an easy fix, in case anyone else has it:

    In Combobox.cs:ToggleComboBox(bool directClick):
    Code (csharp):
    1.  
    2. if (overlayGO.activeSelf)
    3.             {
    4.                 float yRatio = Screen.height / 920.0f;
    5.                 var buttonHeight = buttonRectTransform.sizeDelta.y*yRatio;
    6.                 ...
    7.  
    This would only apply if you have your canvas scaling with screen size of course :)
     
  42. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    406
    I didn't make it... I think it's @Kender
     
  43. CodingStudios

    CodingStudios

    Joined:
    Apr 19, 2010
    Posts:
    12
    The last one I can get to work was posted on Jan, 13, 2015 (Page 1) by Kender. All of the ones since then create the overlay but it never shows in the screen. I see in the one that works that the overlay is created under the combo box but in the newer ones it is created under canvas. I am not sure what else is different. I am using version 4.6.3f1.
     
  44. dkely

    dkely

    Joined:
    Mar 3, 2014
    Posts:
    22
    See my post above:
    Unfortunately i wont be back on this for awhile yet but will share my solution when i get one.
     
  45. dkely

    dkely

    Joined:
    Mar 3, 2014
    Posts:
    22
    Update:
    Looked at this issue again and noticed that if you change the size of the combo box by even a tiny amount during runtime, the box will work. No idea why but i tried a quick fix by resetting their delta size on awake to add 1 to width.
    But there is a second issue. You must click on the combo box in the hierarchy for the size change to take effect.
    Why the location is fixed by changing the width is a mystery that perhaps @Kender can shine light on.
    The issue of not updating until clicking on the hierarchy must be a Unity bug.
     
  46. HockeyMarv

    HockeyMarv

    Joined:
    Oct 16, 2013
    Posts:
    6
    It doesnt work in Unity 5 :(. On click nothing happens.


    edit: Ive found a solution: IPlace the combobox in Editor Mode , switch them off and activate them during runtime. Now it works.
     
    Last edited: Mar 4, 2015
  47. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    I tried your update but I stll get the message, only the line numbers changed:
    Maybe this screenshot helps. I have made a prefab for the combobox and then added it to my ListView during runtime. The Hierachy looks like this

    But in the inspector the script ComboBox looks abit strange to me because in the prefab I can see als variables and here they aren't displayed during runtime


    When I load my game and instanciate my gameobjects and canvas I can see following error message:
     
    Last edited: Mar 4, 2015
  48. Ralstlin

    Ralstlin

    Joined:
    Jan 12, 2014
    Posts:
    87
    dont work in unity 5 to me.. any update?
     
  49. franzbischoff

    franzbischoff

    Joined:
    Feb 23, 2015
    Posts:
    8
    Fix for Unity5

    Move line 468 to Start():

    private void Start()
    {
    scrollPanelGO.transform.SetParent(overlayGO.transform, true);
    }
     
  50. dkely

    dkely

    Joined:
    Mar 3, 2014
    Posts:
    22
    Thanks. Must be a different version from you because it was line 470 for me.

    My previous issue was to do with my UI coming in on a moving panel. The drop down list position is determined on Awake but my UI then moves. A quick fix that seems to be working fine for me is to add the same line to ToggleComboBox() so the start looks like this:
    private void ToggleComboBox(bool directClick)
    {
    overlayGO.SetActive(!overlayGO.activeSelf);
    if (overlayGO.activeSelf)
    {
    scrollPanelGO.transform.SetParent(overlayGO.transform, true);
    ...

    One small issue is that there is a gap between the box and the start of the list
     
unityunity