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
1
Question by doublegumbo · Feb 03, 2014 at 09:54 PM · guicontrolsbasicoverlay

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

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 doublegumbo · Feb 03, 2014 at 11:37 PM 0
Share

What sort of component should I attach this script to? A GUITexture, as recommended below? Also, is this in C# or JS?

avatar image
0

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;
        }
     }
Comment
Add comment · Show 2 · 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 NickP_2 · Feb 03, 2014 at 10:31 PM 0
Share

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.

avatar image doublegumbo · Feb 03, 2014 at 11:20 PM 0
Share

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

avatar image
0

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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