- Home /
Open window on button click
Hi, i simply want to open a new window when the user clicks on a button. I have this code (not working)
... Rect x = new Rect(250, 0, 150, 250); string ctype = "aaa"; if (GUILayout.Button(ctype)) { x = GUI.Window(0, x, ConstraintPicker, "Pick a Constraint"); } ...
public void ConstraintPicker(int id) { GUILayout.BeginArea(new Rect(0, 15, 150, 250)); scroller = GUILayout.BeginScrollView(scroller); foreach( string item in constraints ) { if (GUILayout.Button(item.ToString(), GUI.skin.GetStyle("Button"))) { ctype = item; } } GUILayout.EndScrollView(); GUILayout.EndArea(); }
window gets displayed if i remove the if construct and leave the body
Answer by Eagle32 · Aug 09, 2010 at 10:22 AM
class variable
bool showWindow = false;
In OnGUI
if (GUILayout.Button(ctype)) { showWindow = true; }
if (showWindow) { x = GUI.Window(0, x, ConstraintPicker, "Pick a Constraint"); }
And whenever you want to stop showing the window again obviously just change showWindow back to false.
Ok, this works. but i don't understand why my code does not work
The Button function only returns true the frame it's clicked. So the window function only gets called that one frame when the button is clicked.
The window function has to be called every frame in order to be drawn each frame.
Your answer
Follow this Question
Related Questions
Stop clicking through a GUI window 2 Answers
apply click event in test 0 Answers
Two buttons (Quads) are not working at a same time. 2 Answers
Button waits 3 clicks before working 0 Answers