x


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.

more ▼

asked Mar 11 '10 at 10:26 PM

CalledToGaming gravatar image

CalledToGaming
190 21 24 30

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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
more ▼

answered Mar 12 '10 at 12:22 AM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

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.

Mar 12 '10 at 12:31 AM CalledToGaming

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 quitMenu 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 minimal code to put up a button.

Mar 12 '10 at 01:57 AM Cyclops

As for the differences - to make it Javascript, the only things I see are - changing the void to function, and removing the new keyword.

Mar 12 '10 at 02:07 AM Cyclops

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.

Mar 12 '10 at 08:16 PM CalledToGaming

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.

Mar 13 '10 at 02:35 AM Cyclops
(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Mar 13 '10 at 03:07 AM

Lipis gravatar image

Lipis
2.3k 10 22 48

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(Menu.rect)" is not compiling because it doesn't know what Menu is. If i leave only the rect then it is working perfectly.

Aug 11 '10 at 11:59 AM alexnode

@alexnode thanx for the typo.. I changed it.. :)

Aug 12 '10 at 08:23 PM Lipis
(comments are locked)
10|3000 characters needed characters left

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");

    }

}

}

more ▼

answered Mar 11 '10 at 11:27 PM

Ony gravatar image

Ony
888 29 36 48

Nope, didn't do the trick :\

Mar 11 '10 at 11:37 PM CalledToGaming

Try this code but do what Cyclops did below and put the input section into the function Update() instead of in OnGUI. That should work.

Mar 12 '10 at 02:02 AM Ony
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3682
x385
x161
x104
x6

asked: Mar 11 '10 at 10:26 PM

Seen: 2877 times

Last Updated: Mar 13 '10 at 03:12 AM