Skip to content

Instantly share code, notes, and snippets.

@xmiao2
Created December 9, 2014 06:34
Show Gist options
  • Save xmiao2/7c9c8d20bb0058b4af04 to your computer and use it in GitHub Desktop.
Save xmiao2/7c9c8d20bb0058b4af04 to your computer and use it in GitHub Desktop.
Unity3D Multi-Instance-Friendly Popup List
// A Popup List class that supports multiple instances in Unity
// Popup list created by Eric Haines.
// Popup list Extended by Xiaohang Miao. (xmiao2@ncsu.edu)
public class Popup{
private int selectedItemIndex = 0;
private bool isVisible = false;
private bool isClicked = false;
private static DropdownBox current;
public int List(Rect box, GUIContent[] items, GUIStyle boxStyle, GUIStyle listStyle) {
if(isVisible) {
Rect listRect = new Rect( box.x, box.y + box.height, box.width, box.height * items.Length);
GUI.Box( listRect, "", boxStyle );
selectedItemIndex = GUI.SelectionGrid( listRect, selectedItemIndex, items, 1, listStyle );
if(GUI.changed) {
current = null;
}
}
int controlID = GUIUtility.GetControlID( FocusType.Passive );
switch( Event.current.GetTypeForControl(controlID) )
{
case EventType.mouseUp:
{
current = null;
}
break;
}
if(GUI.Button(new Rect(box.x,box.y,box.width,box.height),items[selectedItemIndex])) {
if(!isClicked) {
current = this;
isClicked = true;
} else {
isClicked = false;
}
}
if(current == this) {
isVisible = true;
} else {
isVisible = false;
isClicked = false;
}
return selectedItemIndex;
}
public int GetSelectedItemIndex()
{
return selectedItemIndex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment