Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Making an In-Game Menu?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Nanne, Feb 22, 2010.

  1. Nanne

    Nanne

    Joined:
    Jan 17, 2010
    Posts:
    19
    I've just created a main menu using the tutorial for the 3DPlatform game and it works fine. But this uses a seperate scene. Now I want an in-game menu that can be toggled on and off anytime during the game, without losing game data. I've search the forum but haven't find enough to help me through.

    Anyway here's what I got so far:

    Code (csharp):
    1. private var isPaused = false;
    2. var resumeTexture : Texture2D;
    3.  
    4. function Update () {
    5.     if(Input.GetKeyDown("escape")  !isPaused)
    6.     {
    7.         print("Puased");
    8.         Time.timeScale = 0.0;
    9.         isPaused = true;
    10.         GetComponent(MouseLook).enabled = false;
    11.         GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
    12.     }
    13.     else if(Input.GetKeyDown("escape")  isPaused)
    14.     {
    15.         print("Unpuased");
    16.         Time.timeScale = 1.0;
    17.         isPaused = false;
    18.         GetComponent(MouseLook).enabled = true;
    19.         GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;     
    20.     }
    21. }
    22. //Create and check if the buttons are being pressed.
    23. function OnGUI(){
    24.     if(isPaused)
    25.     {
    26.         if(GUI.Button (Rect((Screen.width-140)/2,80,140,70), resumeTexture, GUIStyle.none))
    27.         {
    28.             print("Quit!");
    29.             //Application.Quit();
    30.         }
    31.         if(GUI.Button (Rect((Screen.width-140)/2,200,140,70), resumeTexture, GUIStyle.none))
    32.         {
    33.             print("Restart");
    34.             Application.LoadLevel("UnitySpelprojekt");
    35.             Time.timeScale = 1.0;
    36.             isPaused = false;
    37.         }
    38.         if(GUI.Button (Rect((Screen.width-140)/2,280,140,70), resumeTexture, GUIStyle.none))
    39.         {
    40.             print("Continue");
    41.             Time.timeScale = 1.0;
    42.             isPaused = false;  
    43.             GetComponent(MouseLook).enabled = true;
    44.             GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true; 
    45.         }
    46.     }
    47. }
    This script works, it creates tree GUI.Button when the Escape key is pressed. One for contine (close the menu again), one for restarting the game and one for quiting it. Currently all three are useing the same texture, untill I get the real ones.

    Is this a good way to go about it? And how do I add an rollover effect for the buttons?
     
  2. Nanne

    Nanne

    Joined:
    Jan 17, 2010
    Posts:
    19
    No one, nothing, not even a hint - anybody? :)
     
  3. sn4k3

    sn4k3

    Joined:
    May 16, 2009
    Posts:
    36
    I think the reason why you haven't gotten any answers is because there are no problems with your code.
    you just have to add that code to your main camera and you said that it works...
    If you're talking about efficiency... I personally think it's fine and I don't know any other way to do it :D
    [removed link]

    the rollover effect is something else, so I guess u should start a different thread to discuss it.
     
    Last edited by a moderator: May 6, 2020
  4. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    what is that code in c#?first post.

    so thanks.
     
  5. jamesser

    jamesser

    Joined:
    May 29, 2010
    Posts:
    2
    Hi,

    Thanks for the code above. I am working on something similar, however, when press on esc, it will load a gui texture and the buttons are also made up texture. Please advise how to go able doing it. THanks in advance!!
     
  6. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256
    I have a separate script attached to the player for the hud and ingame menus.

    Here are some code snippets how I did it:
    Variables that keep track if a menu is on or not
    and the update function that checks if the player calls a menu (key's assigned to a specific menu)

    Code (csharp):
    1.  
    2. var ingameMenuToggle    : boolean = false;
    3. var helpMenuToggle      : boolean = false;
    4. var scoreboardToggle        : boolean = false;
    5.  
    6.  
    7. function Update()
    8. {
    9.     if (Input.GetKeyDown (KeyCode.Escape ))
    10.     {
    11.         EscapePressed();
    12.     }
    13.  
    14.     if (Input.GetButtonDown ("Helpmenu")) // Helpmenu == H button in key setup
    15.     {
    16.         HelpMenuCall();
    17.     }
    18.    
    19.     if (Input.GetButtonDown ("scoreboard")) // Helpmenu == tab button in key setup
    20.     {
    21.         ScoreBoardCall();
    22.     }
    23. }
    24.  
    They call functions like this that checks
    IF any of the menu's are visible it will close them, (as thats what players generaly expect the Esc key to do).
    Else it shows the main menu thats assigned to Esc. It also pauzes the game and brings up the cursor since I have have it disabled ingame.

    Code (csharp):
    1.  
    2. function EscapePressed()
    3. {
    4.     if (!ingameMenuToggle  !helpMenuToggle  !scoreboardToggle)
    5.     {
    6.         ingameMenuToggle = true;
    7.         Screen.lockCursor = false;
    8.         Time.timeScale = 0 ;    // pauze
    9.     }
    10.    
    11.     else
    12.     {
    13.         Screen.lockCursor = true;
    14.         ingameMenuToggle = false;
    15.         helpMenuToggle = false;
    16.         scoreboardToggle = false;
    17.         Time.timeScale = 1 ;    // unPause,
    18.     }
    19. }
    20.  

    Next,the GUI code looks like this,
    If the toggle variable is on it calls a function that has all the specific gui code for that menu.
    It could be done inside the if function but I prefer separate functions to make it easyer in the code to find specific menu's.
    Since OnGui works like the update function, the moment a variable turns false again the menu disapears.
    Code (csharp):
    1.  
    2. function OnGUI ()
    3. {
    4.     GUI.skin = guiSkin; // Set up gui skin
    5.     // GUI is laid out for a 1920 x 1200 pixel display (16:10 aspect). The next line makes sure it rescales nicely to other resolutions.
    6.     GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));
    7.    
    8.     // INGAME MENU:
    9.     if (ingameMenuToggle)  
    10.     {
    11.         ShowIngameMenu();
    12.     }
    13.  
    14.     // HELP MENU:
    15.     if (helpMenuToggle)
    16.     {
    17.         ShowHelpMenu();
    18.     }
    19.        
    20.     // SCOREBOARD:
    21.     if (scoreboardToggle)
    22.     {
    23.         ShowScoreboard();
    24.     }
    25. }

    Code (csharp):
    1.  
    2. function ShowIngameMenu()
    3. {
    4. GUI.BeginGroup (Rect ((scaledResolutionWidth / 2) -128,( Screen.height / 2) - 192, 256, 384));
    5.     //GUI.Box (Rect (0,0,256,384), "Ingame menu");  // Make the group visible.
    6.    
    7.     GUI.Label(Rect (0,0,256,384), menuBG);  // menuBG  is a background texture
    8.    
    9.     // RESUME GAME
    10.     if (GUI.Button( Rect (38,50,180,45), "Resume game"))
    11.     {
    12.         ingameMenuToggle = false ;
    13.         Time.timeScale = 1 ;
    14.         Screen.lockCursor = true;
    15.     }
    16. }
    17.  
    I hope this is what you guys are looking for.
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Don't forget that within OnGUI you can use a switch if you don't need to show two menus simultaneously.

    In a number of my projects, I have one scene containing a number of full screen menus that are controlled using an enum (eg: enum (StartMenu, OptionsMenu, HelpMenu, etc. ...). Instead of using an if (statement){}, I use a switch.


    Code (csharp):
    1. function OnGUI() {
    2.     switch (menuLevel) {
    3.         case StartMenu:
    4.             //Your GUI Information
    5.         break;
    6.  
    7.         case OptionsMenu:
    8.             //Your GUI Information
    9.         break;
    10.  
    11.         case HelpMenu:
    12.             //Your GUI Information
    13.             //Keep up this format until each screen is covered
    14.         break;
    15.     }
    16. }
    This is efficient as it doesn't check all the if's if there is only one possible screen choice at any one time that doesn't need to be viewed simultaneously with another screen.

    ( - Apologies in advance for any code errors, as this was pulled from my back pocket and memory rather than copied from a working piece of code. This should point you in the right direction.)

    (EDIT)

    I've got an example of this in my sample scrolling project for the iPhone:
    http://theantranch.com/Unity/Entries/2010/3/5_iPhone_Skrollin_Example.html

    It is designed for the iPhone, but the Switch in OnGUI is fine for all Unity projects.
     
  8. kc01

    kc01

    Joined:
    Aug 14, 2014
    Posts:
    1
    I got some errors, help


    Code (CSharp):
    1. private var isPaused = false;
    2. var resumeTexture : Texture2D;
    3. function Update () {
    4.     if(Input.GetKeyDown("escape")  !isPaused)
    5.     {
    6.         print("Puased");
    7.         Time.timeScale = 0.0;
    8.         isPaused = true;
    9.         GetComponent(MouseLook).enabled = false;
    10.         GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
    11.     }
    12.     else if(Input.GetKeyDown("escape")  isPaused)
    13.     {
    14.         print("Unpuased");
    15.         Time.timeScale = 1.0;
    16.         isPaused = false;
    17.         GetComponent(MouseLook).enabled = true;
    18.         GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;  
    19.     }
    20. }
    21. //Create and check if the buttons are being pressed.
    22. function OnGUI(){
    23.     if(isPaused)
    24.     {
    25.         if(GUI.Button (Rect((Screen.width-140)/2,80,140,70), resumeTexture, GUIStyle.none))
    26.         {
    27.             print("Quit!");
    28.             //Application.Quit();
    29.         }
    30.         if(GUI.Button (Rect((Screen.width-140)/2,200,140,70), resumeTexture, GUIStyle.none))
    31.         {
    32.             print("Restart");
    33.             Application.LoadLevel("UnitySpelprojekt");
    34.             Time.timeScale = 1.0;
    35.             isPaused = false;
    36.         }
    37.         if(GUI.Button (Rect((Screen.width-140)/2,280,140,70), resumeTexture, GUIStyle.none))
    38.         {
    39.             print("Continue");
    40.             Time.timeScale = 1.0;
    41.             isPaused = false;
    42.             GetComponent(MouseLook).enabled = true;
    43.             GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;
    44.         }
    45.     }
    46. }

    It says in console:


    Assets/PauseMenu.cs(1,13): error CS0116: A namespace can only contain types and namespace declarations

    Assets/PauseMenu.cs(2,19): error CS8025: Parsing error

    Please help.

    Also, thats all I have in the code, so yeah, I don't know if I need the using UnityEngine thing...
     
    Last edited: Aug 14, 2014
  9. AzraelYTB

    AzraelYTB

    Joined:
    May 13, 2015
    Posts:
    2
    Could it be that it shows puased instead of paused? Certainly seems that way to me.