- Home /
Toggling a basic graphical GUI overlay using the escape key.
I'm trying to present a basic GUI overlay (in the form of a .png file) that presents the instructions of my game. Included in these instructions are the option to toggle the overlay on or off by hitting the 'escape' key. There seems to be a very basic, straightforward way to do this, but I am not having any success figuring it out on my own. Any guidance would be greatly appreciated,
thanks
-G
Answer by RadWayne · Feb 03, 2014 at 10:00 PM
I did something like:
bool menu=false;
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
if(menu==true)
{
menu=false
Time.timeScale=0;
}
else if(menuLevel==false)
{
menu=true;
Time.timeScale=1;
}
}
And then included if(menu) in my OnGUI(). This will also pause/unpause (most of) the game.
What sort of component should I attach this script to? A GUITexture, as recommended below? Also, is this in C# or JS?
Answer by NickP_2 · Feb 03, 2014 at 10:24 PM
Create a GUITexture
:(Menu -> GameObject -> Create other-> GUITexture)
This is an object in your gamescene, the transform' position of this object is tricky:
transform.position.x or y has to be between 0 or 1. For x, 0 is the most left side of the screen, 1 is the most right side of the screen, and for y, 0 is the most down side, and 1 most top, no matter the size of the screen!
GUITexture.pixelInset is something total different, this gives you the ability to reposition/size the texture from the relative point you've chosen with the transform.position positions (some kind of margin or padding)
So when setting the transform.position.x/y to 0, it should be position at the down-left corner of your screen (yes x:0, y:0 in GUI textures is downleft) and now we can scale the texture to the screen pixel size:
Oh yea, the transform.position.z is the index overlay, so other GUITextures with higher/lower values will be behind/above this texture.
So to actually turn this magic into pixels, attach a GUITexture into the scripts inspector, and almost forgot: attach a texture in the GUITexture :)
GL
public GUITexture textureOverlay;
public int overlayDepth = 1;
private bool menuEnabled = false;
void Start()
{
//on start, make texture same size as the screen.
textureOverlay.transform.position = new Vector3(0, 0, overlayDepth);
textureOverlay.pixelInset = new Rect(0, 0, Screen.width, Screen.height);
textureOverlay.enabled = false;
}
void Update()
{
//toggle menu on escape key up
if(Input.GetKeyUp(KeyCode.Escape))
{
menuEnabled = !menuEnabled;
}
//show or hide texture
switch (menuEnabled)
{
case true:
if(!textureOverlay.enabled)
textureOverlay.enabled = true;
break;
case false:
if(textureOverlay.enabled)
textureOverlay.enabled = false;
break;
}
}
Oh, if the user changes screen size in between the game, the texture size will still be the same as for the first screen where he started the game. To make it adaptive to every update, just move textureOverlay.pixelInset = new Rect(0, 0, Screen.width, Screen.height);
to the update function, this way it'll set the GUITexture's size to the screensize every frame.
Thanks for this guidance. Is this a JS script or C#? Also, what should I call this script? Finally, am I attaching the script and GUITexture to the camera?
thanks, -g
Answer by Firedan1176 · Feb 04, 2014 at 01:40 AM
I agree with RadWayne. If you're using Unity's character controller, it isn't paused by the game so you can enable/disable it (vise versa too) with this (which will basically pause the game):
if (menu) {
GetComponent(MouseLook).enabled = false;
Time.timeScale = 0;
}
else {
if (!menu) {
GetComponent(MouseLook).enabled = true;
Time.timeScale = 1;
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Create a 3D GUI with Unity 2 Answers
iPhone joystick? 0 Answers
GUI clipping with 3D matrix 1 Answer