From Unify Community Wiki
Revision as of 17:56, 10 November 2008 by AngryAnt (Talk | contribs)

Jump to: navigation, search

By AngryAnt.


Visualises a list of strings or objects overriding ToString and returns the item selected by the user. Clicking a selected item will deselect it.

Optionally, the OnListItemGUI delegate can be used to do custom GUI rendering of list items.

Component code

<csharp> public static object SelectList( ICollection list, object selected, GUIStyle defaultStyle, GUIStyle selectedStyle ) { foreach( object item in list ) { if( GUILayout.Button( item.ToString(), ( selected == item ) ? selectedStyle : defaultStyle ) ) { if( selected == item ) // Clicked an already selected item. Deselect. { selected = null; } else { selected = item; } } }

return selected; }

public delegate bool OnListItemGUI( object item, bool selected, ICollection list );

public static object SelectList( ICollection list, object selected, OnListItemGUI itemHandler ) { ArrayList itemList;

itemList = new ArrayList( list );

foreach( object item in itemList ) { if( itemHandler( item, item == selected, list ) ) { selected = item; } else if( selected == item ) // If we *were* selected, but aren't any more then deselect { selected = null; } }

return selected; } </csharp>


<csharp> currentSeletion = SelectList( myList, currentSelection, "Button" ), "Label" ) ); </csharp>

In this example, myList could be an ArrayList of strings or instances of your own type, returning the desired list label in ToString.

<csharp> currentSeletion = SelectList( myList, currentSelection, OnCheckboxItemGUI );

private bool OnCheckboxItemGUI( object item, bool selected, ICollection list ) { return GUILayout.Toggle( selected, item.ToString() ); } </csharp>

In this example, the same list is used, only this time it is visualised using a custom list item rendering function.

Personal tools