Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 PogyManTV · Mar 15, 2015 at 11:18 AM · c#guimenukeycodevoid

How can i open in game manu (GUI) with button?

using UnityEngine; using System.Collections;

public class QuickMenu : MonoBehaviour { private float y; private float x; private Vector2 resolution;

 void Update () {
     
     if(Screen.width!=resolution.x || Screen.height!=resolution.y)
     {
         resolution=new Vector2(Screen.width, Screen.height);
         x=resolution.x/1920.0f;
         y=resolution.y/1080.0f;
     }
 }

 void OnGUI(){
     if (Input.GetKeyDown (KeyCode.C)){
         GUI.Button(new Rect(1310*x,825*y,420*x,225*y), "Quit the Game");

} } }

I tried this but it won't work:(

Comment
Add comment · Show 5
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 meat5000 ♦ · Mar 15, 2015 at 02:19 AM 0
Share

What doesnt work? Do you see the button?

avatar image PogyManTV · Mar 16, 2015 at 01:36 PM 0
Share

I don't know what you mean. You mean GetButtonDown?

avatar image PogyManTV · Mar 16, 2015 at 10:22 PM 0
Share

Okay so for the first thank you alot! :) and the variables x and y are there for the resolutions because if i have build game and when i change the resolution for example to 640x480 the GUI disssapear and these variables are looking for specific resolution so i can see GUI Buttons or Texts on any of the resolutions :)

avatar image PogyManTV · Mar 17, 2015 at 12:50 PM 0
Share

public $$anonymous$$enu_Control_CS $$anonymous$$enu_Control_CS;

what i have to do with these? it telling me that it doesn't exist in the current context sorry but iam a pretty newbie to the scripting :/

avatar image JusticeAShearing · Mar 17, 2015 at 05:35 PM 0
Share

$$anonymous$$enu_Control_CS is a script of $$anonymous$$e which you probably don't have. You can omit the parts of the script with it in. I have amended my answer to try to help.

2 Replies

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

Answer by black1ops22 · Mar 16, 2015 at 09:48 PM

Try moving the IF statement to void update thus it'll repeatedly checkto see if "c" is being pressed, and then call onGUI. BTW im pretty sure its onGUI not OnGUI. Try that.

Comment
Add comment · 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
1

Answer by JusticeAShearing · Mar 16, 2015 at 05:31 PM

I can tell you how to open an in-game menu GUI with a button, but I do not understand why you need your definitions of the variables 'x' and 'y', or why they are multiplied by those specific values when defining your GUI button's position and size. If you would like further help, please tell me about what those variables are for so that I may help you in that field.

The script to make a window which can be dragged and has three buttons in it is as follows. I believe that it makes a useful in-game menu.

 using UnityEngine;
 using System.Collections;
 
 public class Esc_Menu_CS : MonoBehaviour {
     
     public bool MenuOn = false;
     public Rect windowRect = new Rect(Screen.width * 0.3f, 0, Screen.width * 0.2f, Screen.height * 0.1f);
     
     void Update ()
     {
         if (Input.GetKeyUp(KeyCode.Escape))
         {
             if (MenuOn == false)
             {
                 MenuOn = true;
                 Screen.lockCursor = false;
             }
             else if (MenuOn == true)
             {
                 MenuOn = false;
                 Screen.lockCursor = true;
             }
         }
     }
     
     void OnGUI ()
     {
         if(MenuOn == true)
         {
             windowRect = GUI.Window(0, windowRect, MakeMyWindow,"");
         }
     }
 
     void MakeMyWindow(int windowID)
     {
         if (GUI.Button (new Rect (Screen.width * 0.025F, Screen.height * 0.1F, Screen.width * 0.15F, Screen.height * 0.042F), "Main Menu" ))
         {
             //Insert your consequence to pressing this button here
         }
         if (GUI.Button (new Rect (Screen.width * 0.025F, Screen.height * 0.15F, Screen.width * 0.15F, Screen.height * 0.042F), "Shutdown" ))
         {
             //Insert your consequence to pressing this button here
         }
         if (GUI.Button (new Rect (Screen.width * 0.025F, Screen.height * 0.2F, Screen.width * 0.15F, Screen.height * 0.042F), "Resume" ))
         {
             MenuOn = false;
         }
 
         GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height * 0.04f));
     }
 }

This makes its size and position relative to the screen also, thus evades any inability to see the button. However, in Unity Web-Player without going full-screen, the text is impossible to see. It is also important to note that this script needs to be adapted. I use Menu_Control_CS to check whether I want the main menu on or not.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i make a menu close if i open another menu 1 Answer

Distribute terrain in zones 3 Answers

Display multiple scenes? 1 Answer

Game Menus WIth Keyboard Input Control 1 Answer

C sharp menu problem with bottons 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