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

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.

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 :slight_smile:

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;
       }
    }

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;
}
}