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 Clopay · Jan 04, 2013 at 07:28 PM · guimainmenuguibutton

Access to GUI Keyboard Focus

Hey guys,

So I asked a pretty in depth question yesterday, which you can view here. Haven't gotten any hits as of yet, so I thought I'd simplify the question to something I can work with for now.

Question Is there a way to gain script access to which button has keyboard focus?

I can apply a GUI skin to a button that has the functionality of changing text color and background texture while a button has keyboard focus, but I'd like to be able to animate/scale a button while it's focused, not when it's pushed. Anyone know of a way to do this?

EDIT: Didn't think to provide code so you all can see how I do this -.- Like I said to David, I utilize a string array in which I add menu items in the inspector, after which I have a "for" loop that creates a button for every array item. Here's the code:

 function OnGUI () {
 
     GUI.BeginGroup (Rect (0, Screen.height / 4, Screen.width / 2, Screen.height / 2));    // Begin group to nest the following  GUI elements in
     
     for (i = 0; i < menuOptions.Length; i++) {                    // For every menu option we have...
         GUI.SetNextControlName (menuOptions[i]);                // Assign a control name
         GUI.Button (Rect (0, 0 + (((Screen.height / 18) * 1.25) * i), Screen.width / 2.5, Screen.height / 18), menuOptions[i]);        // Create a scalable button
     }
     GUI.EndGroup ();                                            // End the group
     GUI.FocusControl (menuOptions[selectedIndex]);                // Set focus control to the selected menu option
 }

NOTE The "selectedIndex" variable within "GUI.FocusControl" is an integer that is determined based upon the up/down selection keys. For example - if selectedIndex = 0, the first button is highlighted, etc. Am I utilizing FocusControl & SetNextControl name properly?

Thanks! Clopay

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

1 Reply

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

Answer by Davidovich · Jan 04, 2013 at 09:41 PM

You can use GUIUtility.keyboardControl to get the ID of the focussed element, then compare that against the control ID.

Example code in C# (Unityscript version is almost identical)

 GUIContent content = new GUIContent("I'm a button!");
 int controlId = GUIUtility.GetControlID(content, FocusType.Keyboard);
 if (GUIUtility.keyboardControl == controlId) {
     Debug.Log("It's got focus - " + controlId);
 }
     
 if (GUI.Button(new Rect(10,10,100,30), content)) {
     Debug.Log("Clicked the control button");
 }
Comment
Add comment · Show 4 · 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 Clopay · Jan 04, 2013 at 10:24 PM 0
Share

Thanks for the response, $$anonymous$$! I do have the ability to access WHICH menu item is selected, but I can't figure out how to gain direct access to the actual item, if that makes sense. I utilize a string array for the menu items, and then use a for-loop to create the menu OnGUI. I'll edit my post to provide the code for how I construct the menu.

avatar image Davidovich · Jan 06, 2013 at 01:57 PM 0
Share

It does make sense, but to my knowledge there isn't any way to access the actual button. You can access which button has focus, but not the button itself.

I may be wrong, but the way I've found to work with the GUI functions is to largely think of them as draw functions - you set everything up before calling GUI.Button and based on whether the button you're about to draw is the focussed one you can change the data you pass into the function.

As an example (warning: possibly stupid code)

function OnGUI () { GUI.BeginGroup (Rect (0, Screen.height / 4, Screen.width / 2, Screen.height / 2));

 for (i = 0; i < menuOptions.Length; i++) {   
     
     // Set up all the same stuff as before
     GUI.SetNextControlName (menuOptions[i]);
 
     // Create the settings for the defaut setup
     var buttonRect = Rect(0, 0 + (((Screen.height / 18) * 1.25) * i), Screen.width / 2.5, Screen.height / 18);
     
     // Deter$$anonymous$$e whether this button has focus. Do this bit however you want.
     var content = GUIContent(menuOptions[i]);
     var controlId = GUIUtility.GetControlID(content, FocusType.$$anonymous$$eyboard);
     
     if (GUIUtility.keyboardControl == controlId) {
     
         // if this button has focus, change whatever needs to be changed. 
         // In this case, just make it bigger, but you could do whatever.
         buttonRect = Rect((buttonRect.x - 10), (buttonRect.y - 5), (buttonRect.width + 20), (buttonRect.height + 10));
     }
     
     // $$anonymous$$now when the button is clicked.
     if (GUI.Button (buttonRect, menuOptions[i])) {
         // Button interactions go here...
     }
 }
        
 GUI.EndGroup ();
 GUI.FocusControl (menuOptions[selectedIndex]);

}

avatar image Clopay · Jan 06, 2013 at 05:42 PM 0
Share

$$anonymous$$ - Brilliant! Thank you so much. This brings a huge amount of relief to me. I never thought of passing your button specs BEFORE you drew the button - this works perfectly for keyboard focus as desired - the buttons scale up nicely on focus, and upon pressing I can pass even more functions. All that's left is styling.

avatar image Clopay · Jan 06, 2013 at 06:23 PM 0
Share

Alright, so the code is working great - upon focusing a button, I can get it to scale up in width a tad bit, which is just what I was looking for; however, it seems that the code somewhat nullifies the GUISkin hover settings. I'd like to see if I can ALSO change the font text/button background of the focused button, as I had before.

It looks like I can have one or the other - and that's based upon where I place the "SetNextControlName" line. If I place it before all of the established variables in OnGUI, I get the button scale. If I place it after all of the established variables, I'll get the button text/texture changes. Do you know of a solution with which I can attain both?

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

9 People are following this question.

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

Related Questions

Changing Font, Background/Style of GUI Buttons 1 Answer

Don't get how to do rounded edges at GUI! 0 Answers

camera zoom using gui button 2 Answers

GUI.Button not responding when drawn in a Rect. 2 Answers

Main menu help needed (C#) 2 Answers


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