- Home /
GUI controlled by keyboard
Hello!
My question is very simple. I have a GUI button "Play" and I want the player to be able to press it by hitting a key (and not using the mouse). There are no other options-there is only one button and the player will hit it.
Thanks!!
Answer by cocopuffs · Jun 17, 2011 at 11:20 AM
well i was thinking cant you just add inputs???? and trough a script define that when input spacebar is pressed set GUI awake :) well thats what i would do im not shure if thats the best or the right way to do it but i gess its something:)...i would help you with the script but i still havent mastered scripting:(....ohhh the imput menu is in Edit--->ProjectSetings-->input
ok for the method i told you to ...go into input then for the "size" add one....so for my current input it displays 21 for size ...so i would make it 22....then you will see a new input appear at the bottom of the list .....(you know its new because its a duplicate of the one ontop of it......change the name of the input to whatever you want(make shure you remember)i put Button1...click on the input and find "positive Button"...click on that and press the spacebar(or the key you want to activate the GUI with),ok now make a new java and type something like this http://pastebin.com/Fh1j$$anonymous$$B$$anonymous$$J then you type what you want the space bar to do when pressed.....i dont know the exact code but i would gess some thing withon trigger and with awake....well im sorry i cant help you any further im not that good at scripting...im bearly learning :)
Answer by Anxo · Jun 17, 2011 at 11:27 AM
you can make a label look like a button with gui styles, then in the update function have a if key down command.
Answer by Bunny83 · Jun 17, 2011 at 12:21 PM
I guess you're using UnityScript?
function OnGUI()
{
var e = Event.current;
if (GUILayout.Button("Play") || (e.type == EventType.KeyDown && e.keyCode == KeyCode.Return))
{
// Start the game
}
}
If you want any key to start the game just remove the keycode check ;)
Answer by charline · Jul 12, 2011 at 03:18 PM
Thank you all very much!
Bunny83 your comment was so straightforward and easy to implement. Thanks again.
Your answer
Follow this Question
Related Questions
How to Select a Focus Control? 1 Answer
Issue with GUI.TextArea keyboard on iPhone 0 Answers
Controlling Pause Menu with Keyboard 2 Answers
Assign keystrokes to GUI button 2 Answers