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().
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.
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.
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.
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.
I’ve ran into couple of problems with the Listbox-control.
The scrollbar behaves erratically and does not allow viewing all the items. Only the top two are visible.
With the scrollwheel I can see the rest of the items but it also behaves erratically.
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
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.