- Home /
Close my GUI button by repressing the same Hot-key.
I have a couple of buttons that pop up when you press a specific hot key and then when you press the GUI button, it does a specific action. For example, when I press escape, it brings up a "Quit" button which brings you back to the main screen when you click it. The problem I'm having is when I press Esc again, i'd like the GUI button to disappear, you know, toggle it on and off. Here's the JS code i have for it now, I don't know what to change to actually get it to work, I've tried putting things like quitMenu = !(quitMenu); and other variations of the code, but nothings working. It either disables the button entirely, or just ignores the code.
private var quitMenu;
function OnGUI () { if (Input.GetButtonDown ("Esc")) { quitMenu=true;} if (quitMenu == true) { if (GUI.Button (Rect(10,10,50,50),"Quit")) { Application.LoadLevel("Opening Screen");
}
}
}
If anyone can help, I'd be greatly appreciative.
Answer by Cyclops · Mar 12, 2010 at 12:22 AM
Well, good news, this solution is tested :) I was curious why it didn't work, the logic seemed right. Turns out, for whatever reason, having an Input() in the OnGUI() call is a bad idea. I was getting double-clicks of my escape key. By moving Input() down to Update(), it worked fine. Also note I used GetKeyUp(KeyCode.Escape)
The actual complete (C#) code. Note that this should be in a file called s_GUI.cs, to match the Class name:
using UnityEngine; using System.Collections;
public class s_GUI : MonoBehaviour { bool quitMenu = false;
void OnGUI() {
if (quitMenu == true) {
if (GUI.Button (new Rect(20, 20, 20, 20), "Quit")) {
quitMenu = false;
// application stuff.
}
}
}
void Update () {
if (Input.GetKeyUp(KeyCode.Escape)) {
quitMenu = !quitMenu;
}
}
} // end class s_GUI
This one isn't working for me either. Just getting a long list of errors in the code, and none of it seems to be happy when I try to adjust it.
Really? Strange, I just cut/paste the code above back into my script, and it worked fine. The code above is correct. :) Granted, I left out the quit$$anonymous$$enu variable declaration, but that should be obvious. And I also presume you noticed it was C# code, not Javascript?
So what kinds of errors are you getting? Try a new project with the $$anonymous$$imal code to put up a button.
As for the differences - to make it Javascript, the only things I see are - changing the void to function, and removing the new keyword.
No, i know it's supposed to be in C#, it's giving me the error...CSTEST.cs(1,6):error CS0116: A namespace can only contain types and namespace declarations. so after the first "void" statement.
Don't really know what it's problem is.
Okay, that's a different problem. :) I updated the code to contain a complete working file. Start with an empty C# script file, named s_GUI.cs, cut/paste this in. You can name it something else, but the filename must match the classname in the C# declaration, or you get errors.
Answer by Lipis · Mar 13, 2010 at 03:07 AM
Here is a more complete example written in JavaScript
. Just copy/paste it as it is and assign it to an Empty Object
to see this example in action.
var width: int = 256; var height: int = 256; var skin: GUISkin;
private var rect: Rect; private var show: boolean = false;
function Awake() { var x = (Screen.width 0.5) - (width 0.5); var x = (Screen.height 0.5) - (height 0.5); rect = Rect(x, y, width, height);
}
function Update() { if (Input.GetKeyDown(KeyCode.Escape)) { show = !show; }
}
function OnGUI() { GUI.skin = skin;
if (show) { GUILayout.BeginArea(rect); GUILayout.Box("Pause Menu"); if (GUILayout.Button("Continue")) { show = false; } if (GUILayout.Button("Restart")) { Application.LoadLevel("Current Level"); } if (GUILayout.Button("Quit")) { Application.LoadLevel("Opening Screen"); } GUILayout.EndArea(); } }
Now in more details. From the inspector you are able to change the width
and height
of your menu that will be displayed in the center of the screen (the code for that is in the Awake()
function). In order to draw the GUI
I'm using the Button
and Box
functions from GUILayout
. For a better user experience you can also create a GUISkin
and assign it to the skin
variable in the Inspector
.
Good luck and have fun!
hey Lipis Thanks a lot, i used your code and it works perfectly, just two small typos ... in the Awake you have two times var x ... and "GUILayout.BeginArea($$anonymous$$enu.rect)" is not compiling because it doesn't know what $$anonymous$$enu is. If i leave only the rect then it is working perfectly.
Answer by Ony · Mar 11, 2010 at 11:27 PM
This is off the top of my head, not tested...
private var quitMenu;
function OnGUI () {
if (Input.GetButtonDown ("Esc")) { if (quitMenu == true) { quitMenu = false; } else { quitMenu=true; }
}
if (quitMenu == true) { if (GUI.Button (Rect(10,10,50,50),"Quit")) { Application.LoadLevel("Opening Screen");
}
}
}
Try this code but do what Cyclops did below and put the input section into the function Update() ins$$anonymous$$d of in OnGUI. That should work.
Your answer
Follow this Question
Related Questions
Look like a Website Menue! 1 Answer
How can I set up a series of OnGUI Toggle buttons in a for loop? 1 Answer
Pause Menu - Loading & Quitting 1 Answer
Gui Toggle help 2 Answers
GUI button off centre 1 Answer