Listbox - Adds listbox object to new UI system

Unity’s new UI system (as introduced in 4.6) lacks a Combo box or List box. So I created my own.

I added an option to the right mouse button menu in the editor to easily instantiate the list box. It can be populated with an array of strings and/or sprites and an optional array of integers for identification.

You can use large option arrays if you want, because only the visible options are represented by game objects who’s display values are swapped as you scroll through the listbox. So the hierarchy is not clogged with hundreds of UI objects.

  • add a listbox from the Unity Menu
  • supports array of strings
  • supports array of images
  • supports hidden numerical values
  • optimized for large arrays

The appearance listbox can be configured using the standard Unity UI system. There are separate UI objects for:
the closed listbox
the opened listbox
the options to choose from
the highlighted option

All source code is included and written in C#. There are no DLL’s or hidden stuff.

View the online webplayer demo (also included in the package) here. It demonstrates a listbox with image options only, with text options only and with combined image/text options.
The documentation can be viewed on my website.

Note: This package only works with the Unity UI system as introduced in 4.6, not with OnGUI().


1 Like

Version 1.05 is now available in the Asset Store.
It adds support for the mouse scroll wheel and fixes the SetParent warnings that were due to introducing a new Unity API function.

Is there a simple way to populate the list dynamically thru code

i.e. im trying to load the screen resolutions into this atm

Sure, there is a description of the interface here: http://orbcreation.com/orbcreation/docu.orb?1074
You can populate the listbox at runtime like this:

myListBox.SetOptions(new string[4] {"Ketchup", "Mayo", "BBQ", "Curry"} );

If you also want to assign id numbers to those values:

myListBox.SetOptions(new int[4] {1,2,3,4} , new string[4] {"Ketchup", "Mayo", "BBQ", "Curry"});

Thanks, no idea how i missed that

I tried the below code but all im getting is a blank listbox

Resolution[] resolutions = Screen.resolutions;
        String[] res = new string[resolutions.Length];
        for(int i=0; i> resolutions.Length;i++)
        {
            res[i] = resolutions[i].width + "x" + resolutions[i].height;
        }
        myListBox.SetOptions(res);

Hey @Orbcreation - Did you forget to add a Asset Store link?

@Hybrid1969 A good year 1969 :).
Despite the year, I think you have a typo in your code.

i> resolutions.Length
should be
i< resolutions.Length

@larku Ha, ha, thanks. Unity Asset Store - The Best Assets for Game Making

Do you have one where it shows a list of items and allows person to scroll-up and down… not a combo-box.

Can’t you use the default scrollview component for that?

Hello,

I’m having trouble getting this to work. None of the script commands appear to be working.

E.g., I tried to copy the pattern of the demo, creating a separate script with a public ListBox variable, like this:

public class PlayerProfileDropDownMenu : MonoBehaviour {

public ListBox playerDropDownMenu;

void Start()
{
playerDropDownMenu.SetOptions(new string[4] { "Ketchup", "Mayo", "BBQ", "Curry" });
}

but this doesn’t work. The resulting listbox is empty. I also can’t seem to find in the demo scene where the option strings are actually set, to see where the approach there diverges.

Any help would be appreciated. Thanks.

Either your playerDropDownMenu variable is empty or the Start() function is never called because the script is not added to a gameObject.

You can test this by adding the line:
Debug.Log(“playerDropDownMenu:” + playerDropDownMenu);

When you open up a Console window, you either see a line “playerDropDownMenu:” and then nothing, which means the variable is not set, or you see nothing at all, which means the Start function is not executed

Have bought this asset. Can it create listboxes with multiple columns and/or header columns. If not, could this be implemented? With multiple columns and with header row, it would have 5 stars…

Sorry Duffer123
It can display a sprite and a text. You can change it if you like to support multiple columns. That shouldn’t be too hard. All the source code is there.

Hi

A very nice and easy to use listbox. Thanks for that. However, I noticed that there’s a problem with the z-order of opened listboxes. Please see the attached image. Any ideas how to ensure that the opened listbox is always in front of other uGui-controls and listboxes?

Thank you already in advance for your cooperation,
Kai_Zu

The easiest way is to change the order of the gameObjects in your hierarchy with Ctrl =
Unity renders the gameObjects in the order they are placed in your hierarchy. The last one is drawn last and thus is on top of the other UI controls.

Thanks for a prompt response. Your advice corrected the problem.

I’ve ran into couple of problems with the Listbox-control.

  1. The scrollbar behaves erratically and does not allow viewing all the items. Only the top two are visible.
  2. With the scrollwheel I can see the rest of the items but it also behaves erratically.
  3. Sometimes when I manage to scroll down enough the selected index points beyond the array bounds that I’ve set to optionStrings.

I’m using Unity 5.1.2f1 so perhaps there have been some changes from version 4.x to 5.x? Unfortunately, as the Listbox is now it is quite unusable. I hope you can shed some light to these issues.

Thank you already in advance for your assistance,
Kai_Zu

Hello. Need some help.

can you please post sample code that allows me to show a list of strings in listbox.
when I use “LeaderBoardListBox.SetOptions(new string[4] {“Ketchup”, “Mayo”, “BBQ”, “Curry”} );” I get this error in unity.

NullReferenceException: Object reference not set to an instance of an object
LeaderBoardScripts.Start () (at Assets/Standard Assets/LeaderBoardScripts.cs:44).

LeaderBoardScripts.cs is my script where I am calling LeaderBoardListBox.SetOptions(new string[4] {“Ketchup”, “Mayo”, “BBQ”, “Curry”} ).

I added a List Box onto my PANEL.
The ListBox is called “LeaderBoardListBox”. it has ListBox.cs attached to it.

Thanks in advance

You do “LeaderBoardListBox.SetOptions”, but LeaderBoardListBox is a name of a GameObject and a name of a class, but not the name of a property.

What you should do in your LeaderBoardListBox.cs script is:

public ListBox leaderBoardListBox; // assign this in the editor by dragging the listbox gameobject onto this field

public void Start() {
leaderBoardListBox.SetOptions(new string[4] {“Ketchup”, “Mayo”, “BBQ”, “Curry”} );
}