Listbox example.

I came across a lack in the unity GUI, namely ListBoxes. So I went ahead and made myself such a system. However, I could easily make an example out of that, because the example found on the Wiki is kind of hard to grasp. The only thing it lacks is a scrollbar. It also uses a custom class rather than strings to list to convey more easily ths can be used for classes of your own, for it is common that an editor uses a scriptable object with a list off instances of a class.

To use it, --when you’ve imported the .unitypackage-- instantiate the class like
_myListBox = new ListBox

Then you can use these functions with the new instance
AddEntry(string Name)
Adds an entry, OnEnable in the EditorWindow extending scripts is the most appropriate place to do it.

RemoveEntry(Entry EntryToRemove)
Removes an entry, OnEnable in the EditorWindow extending scripts is the most appropriate place to do it.

LoadList(List ListToLoad)
Copies the list of another list, only useful if you change the type into the class your scriptable object lists.

Clear()
Empties the list.

Draw(Rect Area, float ItemHeight, Color BackgroundColor, Color SelectedItemColor)
Used on GUI, draws the listbox in the rectangle. The itemHeight is how much heightspace one item takes, 17-18 is normal without using cusom fonts. BackgroundColor is the color of the whole listbox while the SelectedColor is the one on the selected list entry.

I hope it helps some new beginners. (Small Editor window example included on how to read the selection too).

EDIT: if you need AddEntry() to return the Entry class instance, remove the void AddEntry and put the one in this text file in its place. Also contains the function GetEntryByName(string Name). Not really needed, but makes removal easier.
http://dl.dropbox.com/u/1564029/patch.txt

588921–20950–$ListBox.unitypackage (2.38 KB)

1 Like

Wow
ths

Thanks this is very useful