Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Listbox - Adds listbox object to new UI system

Discussion in 'Assets and Asset Store' started by Orbcreation, Nov 26, 2014.

  1. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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().


     
    JasonC_ likes this.
  2. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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.
     
  3. Hybrid1969

    Hybrid1969

    Joined:
    Apr 19, 2013
    Posts:
    31
    Is there a simple way to populate the list dynamically thru code

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

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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:
    Code (CSharp):
    1. myListBox.SetOptions(new string[4] {"Ketchup", "Mayo", "BBQ", "Curry"} );
    If you also want to assign id numbers to those values:
    Code (CSharp):
    1. myListBox.SetOptions(new int[4] {1,2,3,4} , new string[4] {"Ketchup", "Mayo", "BBQ", "Curry"});
     
  5. Hybrid1969

    Hybrid1969

    Joined:
    Apr 19, 2013
    Posts:
    31
    Thanks, no idea how i missed that

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

    Code (CSharp):
    1. Resolution[] resolutions = Screen.resolutions;
    2.         String[] res = new string[resolutions.Length];
    3.         for(int i=0; i> resolutions.Length;i++)
    4.         {
    5.             res[i] = resolutions[i].width + "x" + resolutions[i].height;
    6.         }
    7.         myListBox.SetOptions(res);
     
  6. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Hey @Orbcreation - Did you forget to add a Asset Store link?
     
  7. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
  8. Polywick-Studio

    Polywick-Studio

    Joined:
    Aug 13, 2014
    Posts:
    307
    Do you have one where it shows a list of items and allows person to scroll-up and down... not a combo-box.
     
  9. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    Can't you use the default scrollview component for that?
     
  10. Archestratidas

    Archestratidas

    Joined:
    Feb 1, 2015
    Posts:
    10
    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:

    Code (csharp):
    1.  
    2. public class PlayerProfileDropDownMenu : MonoBehaviour {
    3.  
    4. public ListBox playerDropDownMenu;
    5.  
    6. void Start()
    7. {
    8. playerDropDownMenu.SetOptions(new string[4] { "Ketchup", "Mayo", "BBQ", "Curry" });
    9. }
    10.  
    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.
     
  11. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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
     
  12. Duffer123

    Duffer123

    Joined:
    May 24, 2015
    Posts:
    1,215
    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...
     
  13. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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.
     
  14. Kai_Zu

    Kai_Zu

    Joined:
    Dec 14, 2013
    Posts:
    17
    Screen Shot 2015-08-31 at 14.06.59.png 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
     
  15. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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.
     
  16. Kai_Zu

    Kai_Zu

    Joined:
    Dec 14, 2013
    Posts:
    17
    Thanks for a prompt response. Your advice corrected the problem.
     
  17. Kai_Zu

    Kai_Zu

    Joined:
    Dec 14, 2013
    Posts:
    17
    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
     
  18. Creating-Genius

    Creating-Genius

    Joined:
    Sep 3, 2015
    Posts:
    1
    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
     
  19. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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"} );
    }