Search Unity

DropFix.cs - Unlock horizontal overflow for the new 5.2 uGUI dropdown control [Free]

Discussion in 'Assets and Asset Store' started by ketura, Sep 10, 2015.

  1. ketura

    ketura

    Joined:
    Oct 2, 2013
    Posts:
    29
    So it's great that Unity has finally added a dropdown combo box control by default--it was a glaring omission, and I'm glad they've fixed this. However, it's obnoxious to have the width of the drop down limited by the width of the entire control; sometimes you simply don't have the screen real estate to make the entire control stretch from side to side, but you'd still like to be able to have longer options show up, since it only takes up screen space temporarily. This is the default behaviour for Windows dropdowns, and so I've written a script that fixes the dropdown to match it.

    (It is quite possible that there is some configuration of the ScrollRect, etc that might produce this behavior, but after messing with it for quite a while, was unable to find an option that worked while preserving the actual default functionality. Please advise if I've missed something.)

    This is the result:


    Installation is as follows:

    1- The code for the script is available here. Copy-paste this into a new file called 'DropFix.cs' and place it with your other scripts. Alternatively, download the version attached to this post.

    2- Add it as a Component to the 'Template' GameObject on the DropDown that you want it to work on, shown here:


    That's it!

    The script has been written in such a way that it works with or without vertical scrollbars (as shown), and should also work with reasonable modifications to the templated Item. For instance, you can remove the tacky "Item Checkmark" object, scoot the label over to compensate, and everything will be just as dandy as before.

    Comments or critiques are welcome. Feel free to use this in whatever.
     

    Attached Files:

    Last edited: Sep 11, 2015
    MightyFeather and EliasMasche like this.
  2. Saxy_Guy

    Saxy_Guy

    Joined:
    Nov 27, 2013
    Posts:
    34
  3. ketura

    ketura

    Joined:
    Oct 2, 2013
    Posts:
    29
    That's exactly what this does, but dynamically; no resizing of individual dropdowns needed, nor do you have to worry if you add drop down entries programmatically. Simply add a long item (whether in the editor or via script), and it will take care of itself, so long as the script is in place. Because of the manner in which Unity handles the creation of the template, it will get resized by my script each time the dropdown itself is clicked, too, so even frequent changes will be updated promptly.

    EDIT: to demonstrate:
     
    Last edited: Sep 10, 2015
  4. Saxy_Guy

    Saxy_Guy

    Joined:
    Nov 27, 2013
    Posts:
    34
    Ah, of course. Makes sense!

    I decided to mess around and take it a bit further - it now adjusts itself to make sure it never goes offscreen: http://pastebin.com/v15Mq4ZZ

    Example:
     
  5. ketura

    ketura

    Joined:
    Oct 2, 2013
    Posts:
    29
    Wow, thanks for posting that! I was wondering if I should bother adding that as a feature or not, as Windows default behaviour appears to be to let it run off the side of the window or screen horizontally, but hey, if you've got it, then no point not including it.

    I'm also grateful to see the code of someone who seems to understand the whole layout thing more than I do; I didn't know about SetInsetAndSizeFromParentEdge or some of those other options.

    I've incorporated your feature addition into the files linked/attached in the OP, as well as overhauling the script based on your superior methodology. In particular, while comparing notes, I found a bug in the dropdown that had plagued me from the beginning; I had kept adding things to address this when in fact it turns out my dropdowns were getting corrupted due to that bug, rather than faulty code on my part. I have thus removed those parts that apparently were doing nothing.

    Note that some of your changes result in too much space being allotted, specifically removing the horizontalOverflow setting of the child objects, and your checking of the scrollbar. The overflow setting is actually necessary; otherwise you have to go several pixels wider than the Text's width to prevent the wrap. Your scrollbar code is flawed because Template always includes the scrollbar, it just hides it if its unneeded, so your code as it is now will always detect the (hidden) Scrollbar and allocate extra space. Both of these are thus the reason why the coroutine is included to get that one-frame wait, to allow both of those issues to evaporate.

    To demonstrate both of these edge cases together, here is a dropdown with the checkbox removed and the text scooted to the left. My (current) script is on top, yours on the bottom:



    But yeah. I don't mean to rag on your style, after all, I did learn a bunch of stuff from your script. Just wanted to defend my code and point out that not all of my clauses were useless runaround! :)

    Thanks again for your help!