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
0
Question by areFranz · Aug 14, 2014 at 04:30 PM · c#guiinputmenu

Game Menus WIth Keyboard Input Control

Hi All! I have a race game with various tracks and vehicles and i'm making the menu for it. I need a menu that has the ability to be controlled by the arrow keys of the keyboard. I have a "MainMenu" scene with an empty object with a script that renders the GUI but only the first page. Here is the code:

 public class MainMenu : MonoBehaviour {
 
     public AudioClip blip;
 
     public Texture2D background;
     public Texture2D wip3Logo;
 
     public GUIStyle mainButton;
 
     private string[] mainMenuLabels = { "SINGLE RACE", "CAMPAIGN", "SPLIT SCREN", "STATS", "QUIT" };
     private bool[] mainMenuButtons;
     private int mainMenuSelected;
 
     void Awake () {
 
         DontDestroyOnLoad (GameManager.Instance);
         GameManager.Instance.StartState ();
     }
 
     void Start () {
 
         mainMenuButtons = new bool[mainMenuLabels.Length];
         mainMenuSelected = 0;
     
     }
     
     void Update () {
         
         if (Input.GetKeyDown (KeyCode.DownArrow) == true) {
             audio.PlayOneShot (blip);
             if (mainMenuSelected < mainMenuLabels.Length - 1)
                 mainMenuSelected += 1;
             else
                 mainMenuSelected = 0;
         }
         
         if (Input.GetKeyDown (KeyCode.UpArrow) == true) {
             audio.PlayOneShot (blip);
             if (mainMenuSelected > 0)
                 mainMenuSelected -= 1;
             else
                 mainMenuSelected = mainMenuLabels.Length - 1;
         }
     }
 
     void OnGUI () {
 
         float width = Screen.width / 2;
         float height = Screen.height / 2;
 
         // MAIN BACKGROUND
         GUI.Box (new Rect (width - 960, height - 600, 1920, 1200), background);
 
         // MAIN MENU
         GUI.BeginGroup (new Rect (width - 512, height - 384, 1024, 768));
         
         GUI.Label (new Rect (64, 96, 416, 48), wip3Logo);
         
         for (int i = 0; i < mainMenuLabels.Length; i++) {
             
             GUI.SetNextControlName (mainMenuLabels[i]);
             mainMenuButtons[i] = GUI.Button (new Rect (64, 160 + i * 64 , 416, 48), mainMenuLabels[i], mainButton);
         }
         
         GUI.EndGroup ();
         
         GUI.FocusControl (mainMenuLabels[mainMenuSelected]);
         
         if (mainMenuButtons[0]) {
             
             Debug.Log ("SINGLE RACE");
             GameManager.Instance.SetRaceType (GameManager.RaceType.SingleRace);
         }
         
         if (mainMenuButtons[1]) {}
         
         if (mainMenuButtons[2]) {}
         
         if (mainMenuButtons[3]) {}
         
         if (mainMenuButtons[4]) {
             
             Application.Quit ();
         }
         
         if (Input.GetKey (KeyCode.Return)) {
             mainMenuButtons[mainMenuSelected] = true;
         }
     }
 }

As you can see it manage the first page of the menu. I need a way to program the various pages in the same way with personalized contollers (a page needs even left and right arrows) to build a complex menu. How can i do that?

Comment
Add comment · Show 1
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 areFranz · Aug 14, 2014 at 04:59 PM 0
Share

In other words, how can i build a complex menu controlled by keyboard input?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zaid87 · Aug 15, 2014 at 03:00 AM

So you already got the first menu working and just looking for ways of doing the same thing for a different menu? If so, I think you can either create a functions and set the codes that you've written into that function instead like "UpdateMainMenu" function and call it from the "Update" function. Then just make a copy of that function for other menus you want and call them instead if you're currently in that menu (you can use a state system to do this (I usually use enum for it)). Same thing with the OnGUI function.

Or, another way is to create another class for the other menus and attach it to a GameObject, then deactivate them at the start. When you want the menu to appear, simply set the GameObject active to true and false when you exit from them.

Hope this helps.

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 areFranz · Aug 15, 2014 at 03:49 AM 0
Share

Ok, your first solution is interesting, but as i have many pages it would be complicated to program all in one C# script...So i choose the second. I have only a roblem now: the last part of code

 if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Return)) {
     main$$anonymous$$enuButtons[main$$anonymous$$enuSelected] = true;
 }

Don't be called with Return key BUT only with Space key... Why?

avatar image zaid87 · Aug 15, 2014 at 04:38 AM 0
Share

Hmm... that's weird, that piece of code works fine in $$anonymous$$e. Are you sure that's the line that is called? Can you try to place a breakpoint on line 87 to see if that's the one that's hit when you press Space?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUILayout Context Menu on Right Click - Not Working! 1 Answer

Editor Scripting Input Problem OnGUI() 1 Answer

Gui list and color 0 Answers

How do i make a menu close if i open another menu 1 Answer

Disabling UI Input on a per player basis 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