- Home /
Dropdown menu(PopupList)
What happends here is that when there's a left click so the showlist is true.(pop ups)
but the problem is that when I check if there's a left click and showlist is true(in the if statement) so it opens and immeaditely closes(it's a matter of milliseconds), if I change Fire1 to Fire2, it does closes it correctly with the RIGHT click, i want to close it with the LEFT click.
using UnityEngine;
public class Popup { public static bool List (Rect position, ref bool showList, ref int listEntry, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle) { return List(position, ref showList, ref listEntry, buttonContent, listContent, "button", "box", listStyle); }
public static bool List (Rect position, ref bool showList, ref int listEntry, GUIContent buttonContent, GUIContent[] listContent,
GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle) {
bool done = false;
if(!showList && Input.GetButton("Fire1")){
showList = true;
}
else if(Input.GetButton("Fire1") && showList)
{
done = true;
}
GUI.Label(position, buttonContent, buttonStyle);
if (showList) {
Rect listRect = new Rect(40, 58, 95, listStyle.CalcHeight(listContent[0], 10.0f)*listContent.Length);
GUI.Box(listRect, "", boxStyle);
listEntry = GUI.SelectionGrid(listRect, listEntry, listContent, 1, listStyle);
}
if (done) {
showList = false;
}
return done;
}
}
Answer by Mike 3 · Jul 05, 2010 at 12:29 PM
It should be else if for the second if (so it doesn't go into it if you toggle it on)
Nope, It's still the same, No difference, I updated the code above.
How did u get to that conclusion? GetButtonDown works only if the button is held. I want to make just one click.
Other way around - GetButtonDown returns true only the first frame it's pressed, GetButton returns true while it's down. On the other hand, you really shouldn't be using Input.* in GUI code (I'd use if(Event.current.type == EventType.$$anonymous$$ouseUp && Event.current.button == 0){} )
Answer by krskrs · Jul 19, 2012 at 10:29 AM
class what i try to use is Standard Assets/Scripts/Popup.cs Popup list by Eric Haines.
sing UnityEngine; using System.Collections; using System.Collections.Generic;
public class Popup { static int popupListHash = "PopupList".GetHashCode(); // Delegate public delegate void ListCallBack();
public static bool List (Rect position, ref bool showList, ref int listEntry, GUIContent buttonContent, object[] list ,
GUIStyle listStyle, ListCallBack callBack) {
return List(position, ref showList, ref listEntry, buttonContent, list, "button", "box", listStyle, callBack);
}
public static bool List (Rect position, ref bool showList, ref int listEntry, GUIContent buttonContent, object[] list,
GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle, ListCallBack callBack) {
int controlID = GUIUtility.GetControlID(popupListHash, FocusType.Passive);
bool done = false;
switch (Event.current.GetTypeForControl(controlID)) {
case EventType.mouseDown:
if (position.Contains(Event.current.mousePosition)) {
GUIUtility.hotControl = controlID;
showList = true;
}
break;
case EventType.mouseUp:
if (showList) {
done = true;
// Call our delegate method
callBack();
}
break;
}
GUI.Label(position, buttonContent, buttonStyle);
if (showList) {
// Get our list of strings
string[] text = new string[list.Length];
// convert to string
for (int i =0; i<list.Length; i++)
{
text[i] = list[i].ToString();
}
Rect listRect = new Rect(position.x, position.y, position.width, list.Length * 20);
GUI.Box(listRect, "", boxStyle);
listEntry = GUI.SelectionGrid(listRect, listEntry, text, 1, listStyle);
}
if (done) {
showList = false;
}
return done;
}
}
Your answer