- Home /
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:(
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 :)
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 :/
$$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.
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.
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.
Your answer
Follow this Question
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