- Home /
Best way to handle keypress?
I'm still new to all of this, but most tutorials call key-press inside of the Update function, which is called once per frame... Now, when using a Key-UP to see when a key was pressed or (KeyDown) whichever works.
Either way, I'm going to be drawing a GUI when a player hits a certain key, although I would think that if I did this inside of the Update() method it would draw the GUI EVERY FRAME, which... would eventaully cause a crash. If I'm not mistaken, perhaps I am. Anybody care to explain?
Answer by Democre · Nov 23, 2012 at 01:12 AM
What you should do is set a flag when the key is pressed. In your OnGUI, check the flag and draw if true.
public bool drawingGUI = false;
void Update()
{
if(Input.GetKeyUp(KeyCode.X))
drawingGUI = true;
}
void OnGUI()
{
if(drawingGUI) {
//draw all your GUI stuff
//include a line to set your flag back to false whenever you want to hide the GUI
if (GUI.Button(Rect(10,70,50,30),"Dismiss"))
drawingGUI = false;
}
}
Hope this helps
Answer by Bunny83 · Nov 23, 2012 at 01:26 AM
Unity provides any kind of input only between visual frames / updates. That means during one Update cycle all Input data will stay the same. Unity has a lot of ways to process input:
Use the Input class in Update.
Use the Event class in OnGUI.
Basically OnGUI is ment for GUI events in the first place. The Event class is purely "event based", so it only provides information when something changed (key down, key up)
The Input class provides two different kinds of functions:
Event based (GetKeyDown, GetKeyUp, GetButtonDown, GetButtonUp, ...)
State based (GetKey, GetButton, GetAxis)
In order to react to Input you have to check it every frame. Functions like OnKeyDown will only be true for exactly one frame right after the event occurred.
Keep in mind this important rules:
Avoid the Input class in OnGUI. If you want to check input there use either the Event class or process the input in Update, set a variable and use the variable in OnGUI.
Never use the Event class outside of OnGUI. It only works in OnGUI (or in functions called from inside OnGUI of course).
Next thing is what you mean by "drawing a GUI". Unity's GUI system (OnGUI and the GUI / GUILayout classes) uses an immediate mode, so OnGUI is called every frame after the cameras are drawn and just renders the GUI on top of the screen. So whatever you "draw" in OnGUI will be visible as long as you draw it every frame. When you want to "remove" a GUI element, just don't draw it anymore.
Answer by LPGaming · Nov 23, 2012 at 09:59 PM
Thanks to both of you, although Democre provided a code sample which helped me understand what he meant, I'm honestly suprised I didn't think of it. My question is, wouldn't calling something or even setting a variable EVERY SINGLE FRAME.... cause lag? especially in a multi-player setting?
This shouldn't be its own answer, just a comment on the original.
But to answer this question, the amount of lag introduced is extremely $$anonymous$$imal. The Input system is already more or less polling as often as it can, so checking it or setting a variable based on it, does nothing noticeable. The time it takes to set a variable is effectively controlled by the speed of your RA$$anonymous$$. You will find bottle necks in many other areas of your code long before you find bottle necks in variable assignment.
No haha. Calling a function or setting a variable every frame will not cause a slowdown. In fact, most games will be calling hundreds if not thousands of functions every frame and setting just as many variables. Obviously there is a limit, but it's much, much higher than you assume :)