Created
December 9, 2014 06:34
-
-
Save xmiao2/7c9c8d20bb0058b4af04 to your computer and use it in GitHub Desktop.
Unity3D Multi-Instance-Friendly Popup List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A Popup List class that supports multiple instances in Unity | |
// Popup list created by Eric Haines. | |
// Popup list Extended by Xiaohang Miao. ([email protected]) | |
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