- Home /
OnGUI() keyboard input
I don't have a very good understanding of OnGUI() or getting player input, and so the following code that I use technically works, but isn't very pleasant to use.
without further ado, here's the code:
void OnGUI() {
Event e = Event.current;
if (e.isKey) {
//debug:
Debug.Log ("key read: " + e.keyCode);
if ((e.keyCode == KeyCode.Keypad1 || e.keyCode == KeyCode.Alpha1) && (moveOptions.Count > 0)) {
move(moveOptions[0]);
} else if ((e.keyCode == KeyCode.Keypad2 || e.keyCode == KeyCode.Alpha2) && (moveOptions.Count > 1)) {
move(moveOptions[1]);
} else if ((e.keyCode == KeyCode.Keypad3 || e.keyCode == KeyCode.Alpha3) && (moveOptions.Count > 2)) {
move(moveOptions[2]);
} else if ((e.keyCode == KeyCode.Keypad4 || e.keyCode == KeyCode.Alpha4) && (moveOptions.Count > 3)) {
move(moveOptions[3]);
} else if ((e.keyCode == KeyCode.Keypad5 || e.keyCode == KeyCode.Alpha5) && (moveOptions.Count > 4)) {
move(moveOptions[4]);
} else if ((e.keyCode == KeyCode.Keypad6 || e.keyCode == KeyCode.Alpha6) && (moveOptions.Count > 5)) {
move(moveOptions[5]);
} else if ((e.keyCode == KeyCode.Keypad7 || e.keyCode == KeyCode.Alpha7) && (moveOptions.Count > 6)) {
move(moveOptions[6]);
} else if ((e.keyCode == KeyCode.Keypad8 || e.keyCode == KeyCode.Alpha8) && (moveOptions.Count > 7)) {
move(moveOptions[7]);
} else if ((e.keyCode == KeyCode.Keypad9 || e.keyCode == KeyCode.Alpha9) && (moveOptions.Count > 8)) {
move(moveOptions[8]);
}
}
}
Now here's the problem: let's say I'm in a situation where I'm given the option to move to a specific location using '1'. If I do that, I am moved there and then to wherever '1' takes me from that location, and so on, for as long as I hold down '1'. This becomes very inconvenient because even if I quickly tap and release the button, Unity will read that as two moves.
How do I alter my code so that I only get one move per 'tap' (press/depress) of the keyboard?
UPDATE: READ... I was actually able to fix this on my own, using this code instead:
void Update () {
if ((Input.GetKeyDown ( KeyCode.Keypad1) || Input.GetKeyDown (KeyCode.Alpha1)) && (moveOptions.Count > 0)) {
move(moveOptions[0]);
} else if ((Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.Alpha2)) && (moveOptions.Count > 1)) {
move(moveOptions[1]);
} else if ((Input.GetKeyDown(KeyCode.Keypad3) || Input.GetKeyDown(KeyCode.Alpha3)) && (moveOptions.Count > 2)) {
move(moveOptions[2]);
} else if ((Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.Alpha4)) && (moveOptions.Count > 3)) {
move(moveOptions[3]);
} else if ((Input.GetKeyDown(KeyCode.Keypad5) || Input.GetKeyDown(KeyCode.Alpha5)) && (moveOptions.Count > 4)) {
move(moveOptions[4]);
} else if ((Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.Alpha6)) && (moveOptions.Count > 5)) {
move(moveOptions[5]);
} else if ((Input.GetKeyDown(KeyCode.Keypad7) || Input.GetKeyDown(KeyCode.Alpha7)) && (moveOptions.Count > 6)) {
move(moveOptions[6]);
} else if ((Input.GetKeyDown(KeyCode.Keypad8) || Input.GetKeyDown(KeyCode.Alpha8)) && (moveOptions.Count > 7)) {
move(moveOptions[7]);
} else if ((Input.GetKeyDown(KeyCode.Keypad9) || Input.GetKeyDown(KeyCode.Alpha9)) && (moveOptions.Count > 8)) {
move(moveOptions[8]);
}
}
However, I'd still like an explanation of why each of these code snippets works in the way that they do;
Answer by Flightkick · Jun 21, 2015 at 06:29 PM
OnGUI() is for GUI stuff, I think it's still from the legacy GUI system. For game mechanics you should use the Update() method. I have never tried OnGUI() using the Event for button press checks.
Might be interesting to take a look at Input.GetButtonDown() where you can specify keys declared in the inputmanager. This way a player can change the keybindings in the launcher.
Hello I would like to ask why shouldn't we use OnGUI() for checking the input? Is it wrong for some reason or just because thats how it is? I'm an inexperienced Unity user so that common knowledge would help me a lot. I'm asking cause Update is calling every frame am I right? And events happen everytime and if you add your event listener then you will catch the event everytime but in Update there is still possibility to not get it, highly unlikely but what if framerate drops for any reason? Will input in update still manage to catch it? And still it sounds more appropriate to catch an event when it happens than checking it every update if it occured isn't it?
Cheers :)
Update() happens every frame. Therefore you can't miss user input if you check it there. OnGUI() happens more than once a frame and as @Flightkick points out, it's the legacy pre-4.5 GUI system. It was never designed for user input and, tbh, shouldn't really be used at all now.
What if my input happens between frames? And if OnGUI() happens more than once a frame it's more accurate doesn't it? I'm just curious, I'm not trying to be rude :) I just want to know what about performance and accuracy :) Thanks for answer!
Your answer
Follow this Question
Related Questions
How to change the editor's keyboard to AZERTY layout? 1 Answer
how make to make ios keyboard's return key submit input? 0 Answers
How do you make a method that gets called when a button gets highlighted using the arrow keys? 0 Answers
Google Featuring Requirement on Android Themes 1 Answer
MonoDevelop: command a sometimes all, sometimes replace 0 Answers