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 Piflik · Apr 19, 2012 at 08:14 PM · keyboardmain menugui button

GUI Button keyboard?

I have created a simple Main Menu using GUI.Buttons. The work fine with the mouse, but how can I make them work with the keyboard? (Arrow keys for choosing an entry, enter to confirm)

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
1

Answer by kevork · Apr 19, 2012 at 08:38 PM

Grab the events you need from the Input class and use those to trigger the effects you want. Unity's GUI does not support keyboard input natively, so you will have to manually trigger a lot of the effects that you take for granted with the mouse, for example, the hover effects to indicate the current selection.

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 Piflik · Apr 19, 2012 at 08:43 PM 0
Share

That's what I feared...off to coding then...

avatar image
0

Answer by Mifuyne · Apr 27, 2012 at 02:50 AM

Using arrow keys to change button focus:

You need to set the "name" of each button via GUI.SetNextControlName(); then you switch focus with GUI.FocusControl();

Using buttons with keypresses:

GUI.Button returns a boolean value. You store the button in a boolean variable and change the value of it whenever the keystroke triggers.

Example (in C#):

 string[] buttonNames = {"Resume", "Main Menu"};
 bool[] buttons;
 int currentSelection = 0;    // current button selected

 void Start() {
     buttons = new bool[buttonNames.Length];
 }
 
 void OnGUI() {
     // Generate buttons, store into buttons array
     for (int i = 0; i < buttonNames.Length; i++) {
         GUI.SetNextControlName(buttonNames[i]);
         buttons[i] = GUI.Button(new Rect(10, 70 + (20 * i), 80, 20), buttonNames[i]);
     }
     
     // Using button with keystroke
     if (Input.GetButton("Use")) {
         // When the use key is pressed, the selected button will activate
         buttons[currentSelection] = true;
     }
     
     // Button conditions
     if (buttons[0]) {
         // resume
     }

     if (buttons[1]) {
         // return to main menu
     }
     
     // Cycling through buttons
     if (Input.GetButton("Down")) {
         GUI.FocusControl(buttonNames[currentSelection + 1]);
     }
     if (Input.GetButton("Up")) {
         GUI.FocusControl(buttonNames[currentSelection - 1]);
     }
 }

Bear in mind that this snippet of code doesn't check whether or not currentSelection is outside of the buttons array index. I also haven't tested this exact code myself. I sort of copied it from my own project (I have a separate class handling menu navigation via gamepad).

I hope this helps :)

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
avatar image
0

Answer by amasiye313 · Sep 08, 2012 at 11:11 PM

I answered something similar on another forum. Here's what you do (the same logic applies for C# but I will do it in javascript/unityscript):

 #pragma strict
 @script ExecuteInEditMode;
 
 var selGridInt : int = 0;    //This integer is the index of the button we are hovering above or have selected
 
 var selStrings : String[] = ["Grid 1", "Grid 2", "Grid 3", "Grid 4"];
 
 private var maxButton : int;    // The total number of buttons in our grid
 
 function Start()
 {
     maxButton = selStrings.Length;    // Set the total no. of buttons to our String array size
 }
 
 funtion Update()
 {
     // Get keyboard input and increase or decrease our grid integer
     if(Input.GetKeyUp(KeyCode.UpArrow))
     {
         // Here we want to create a wrap around effect by resetting the selGridInt if it exceeds the no. of buttons
         if(selGridInt > 0)
         {
             selGridInt--;
         }
         else
         {
             selGridInt = maxButton - 1;
         }
     }
 
     if(Input.GetKeyUp(KeyCode.DownArrow))
     {
         // Create the same wrap around effect as above but alter for down arrow
         if(selGridInt < (maxButton-1))
         {
             selGridInt++;
         }
         else
         {
             selGridInt = 0;
         }
     }
 
     if(Input.GetKeyUp(KeyCode.Return))
     {
         switch(selGridInt)
         {
             case 0: Debug.Log("Button "+(selGridInt + 1 )+ " pressed.");
                 break;
             case 1: Debug.Log("Button "+(selGridInt + 1 )+" pressed.");
                 break;
             case 2: Debug.Log("Button "+(selGridInt + 1 )+ " pressed.");
                 break;
             case 3: Debug.Log("Button "+(selGridInt + 1 )+ " pressed.");
                 break;
         }
     }
 }
 
 function OnGUI () 
 {
     selGridInt = GUI.SelectionGrid (Rect (Screen.width/2-75, Screen.height/2-25, 150, 200), selGridInt, selStrings, 1);
 }
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 Piflik · Sep 08, 2012 at 11:23 PM 0
Share

While this might work to call the respective functions associated with each button, the player wouldn't know which button he is going to be using, because there is no highlighting or similar.

avatar image amasiye313 · Sep 09, 2012 at 03:47 AM 0
Share

Try it out. It takes care of the highlighting as well, that's the joy of using GUI.SelectionGrid. I realize that there is more than one way to do this, but this is just one simple way to achieve the same effect. But try out the code in an empty scene. Simply copy it into a new script and drag the script onto the main camera and you'll see what I mean.

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

6 People are following this question.

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

Related Questions

GUI using keyboard as input? 0 Answers

A node in a childnode? 1 Answer

Mobile Keyboard Scales Screen 1 Answer

How would I use a Gui Button to shoot? 1 Answer

How to turn on/off multiple lights using GUI buttons? 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