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:(
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.
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.