- Home /
Use a key to open a GUI?
I'm trying to implement a feature that would allow me to press a key and bring up a GUI. For example, I press Esc, and it brings up a GUI button that allows me to quit the game.
The problem I'm having is that When I press the button, they GUI stays up until the button is released. I want it to stay up until I press the button again. Obviously I'm missing a piece of code, here's what I have so far (JS)
function OnGUI () { if(Input.GetButtonDown("Esc")){ if(GUI.Button (Rect (10,10,100,20), "Quit")) { Application.Quit(); } }
}
Answer by Ashkan_gc · Mar 04, 2010 at 08:55 AM
you should have a variable called quitMenu weh nyou press esc set it to true and then when you want to hide the menu set it to false.
if (GUI.GetButtonDown ("ESC")) { quitMenu=true;}
if (quitMenu == true)
{
if (GUI.Button (Rect(10,10,300,300),"quit")
{
//place your code here and also put
quitMenu = false;
}
}
define the quitMenu on top of your script as a private var.
Well, that helps get the key to pop and stay up, Now I just need a script to get the key to disappear if pres "Esc" again. here's what I have so far...
private var quit$$anonymous$$enu;
function OnGUI () { if (Input.GetButtonDown ("Esc")) { quit$$anonymous$$enu=true;} if (quit$$anonymous$$enu == true) { if (GUI.Button (Rect(10,10,50,50),"Quit")) { Application.LoadLevel("Opening Screen");
}
}
}
you should just have something like this inside your if. if (Input.GetButtonDown ("Esc")){quit$$anonymous$$enu=!(quit$$anonymous$$enu);} so if quit$$anonymous$$enu is true it will make it not(true) and if it's false it will make it not(false). in your GUI.Button code just make quit$$anonymous$$enu false if your code is something other than changing the scene.
I can't seem to get it to work. Either I'm not placing it right, or I just don't understand what I'm doing. (Both of which are very likely scenarios)
Answer by Coreyf716 · Sep 29, 2012 at 04:49 PM
Try this:
var quitMenu : boolean = false;
function Start () {
}
function Update () {
if(Input.GetKeyDown(KeyCode.Escape)) {
quitMenu = !quitMenu;
}
}
function OnGUI () {
if(quitMenu == true) {
if(GUI.Button(Rect(10,10,300,300), "quit")) {
Application.Quit();
}
}
}
Your answer
Follow this Question
Related Questions
Key for GUI.Button 2 Answers
Best way to create large amount of buttons 2 Answers
How do I make the buttons work on this GUI? 2 Answers
Assign a hotkey to a GUI button? 3 Answers
GUI Info button not displaying 1 Answer