Unity Community


Results 1 to 7 of 7

  1. Posts
    19

    Making an In-Game Menu?

    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:  
    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. Posts
    19
    No one, nothing, not even a hint - anybody?


  3. 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
    look at this pause menu:
    http://www.unifycommunity.com/wiki/i...itle=PauseMenu

    the rollover effect is something else, so I guess u should start a different thread to discuss it.


  4. Posts
    117
    what is that code in c#?first post.

    so thanks.


  5. Location
    melbourne, australia
    Posts
    2

    ESC button activation in game

    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!!
    James ser


  6. Location
    Belgium
    Posts
    244
    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:  
    1. var ingameMenuToggle    : boolean = false;
    2. var helpMenuToggle      : boolean = false;
    3. var scoreboardToggle        : boolean = false;
    4.  
    5.  
    6. function Update()
    7. {
    8.     if (Input.GetKeyDown (KeyCode.Escape ))
    9.     {
    10.         EscapePressed();
    11.     }
    12.  
    13.     if (Input.GetButtonDown ("Helpmenu")) // Helpmenu == H button in key setup
    14.     {
    15.         HelpMenuCall();
    16.     }
    17.    
    18.     if (Input.GetButtonDown ("scoreboard")) // Helpmenu == tab button in key setup
    19.     {
    20.         ScoreBoardCall();
    21.     }
    22. }

    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:  
    1. function EscapePressed()
    2. {
    3.     if (!ingameMenuToggle && !helpMenuToggle && !scoreboardToggle)
    4.     {
    5.         ingameMenuToggle = true;
    6.         Screen.lockCursor = false;
    7.         Time.timeScale = 0 ;    // pauze
    8.     }
    9.    
    10.     else
    11.     {
    12.         Screen.lockCursor = true;
    13.         ingameMenuToggle = false;
    14.         helpMenuToggle = false;
    15.         scoreboardToggle = false;
    16.         Time.timeScale = 1 ;    // unPause,
    17.     }
    18. }


    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:  
    1. function OnGUI ()
    2. {
    3.     GUI.skin = guiSkin; // Set up gui skin
    4.     // 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.
    5.     GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));
    6.    
    7.     // INGAME MENU:
    8.     if (ingameMenuToggle)   
    9.     {
    10.         ShowIngameMenu();
    11.     }
    12.  
    13.     // HELP MENU:
    14.     if (helpMenuToggle)
    15.     {
    16.         ShowHelpMenu();
    17.     }
    18.        
    19.     // SCOREBOARD:
    20.     if (scoreboardToggle)
    21.     {
    22.         ShowScoreboard();
    23.     }
    24. }


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

    I hope this is what you guys are looking for.

  7. Administrator



    Location
    London, UK
    Posts
    3,308
    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:  
    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...n_Example.html

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •