Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CalledToGaming · Mar 11, 2010 at 10:26 PM · guimenutogglegui-button

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CalledToGaming · Mar 12, 2010 at 12:31 AM 0
Share

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.

avatar image Cyclops · Mar 12, 2010 at 01:57 AM 0
Share

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.

avatar image Cyclops · Mar 12, 2010 at 02:07 AM 0
Share

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

avatar image CalledToGaming · Mar 12, 2010 at 08:16 PM 0
Share

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.

avatar image Cyclops · Mar 13, 2010 at 02:35 AM 0
Share

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.

Show more comments
avatar image
3

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexnode · Aug 11, 2010 at 11:59 AM 0
Share

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.

avatar image Lipis · Aug 12, 2010 at 08:23 PM 0
Share

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

avatar image
0

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

 }

}

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CalledToGaming · Mar 11, 2010 at 11:37 PM 0
Share

Nope, didn't do the trick :\

avatar image Ony · Mar 12, 2010 at 02:02 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges