- Home /
Why does holding down any button cause ~20-30 FPS loss?
I'm not sure if this is something I'm doing wrong or something specific to the engine, but holding down any button (even one that is not bound to anything and is not being polled for in any scripts) causes my FPS in the game to drop quite a lot. This happens even if I export the game to an executable and test it there. I usually have about 80 FPS without touching a button, and then it drops to about 50-60 while holding a key down. If I disable all of my scripts it seems to get better, but I can't really tell because at that point the FPS is at around 500 so I can't really tell if holding down a button is making a difference.
I do have a lot of scripts that look for specific inputs using Input.GetButton("Fire1"), Input.GetAxisRaw("Horizontal"), etc., but none that look for just any key being pushed down. So, I don't understand why just holding down any key that is not even being used or alluded to in any way in the code would be such a big deal. I don't have any general statements that just search for any input.
***I just checked and this doesn't happen in other projects such as the Bootcamp demo. I have no idea what could be causing this and it would take me forever to comb through all my scripts to find the culprit (though I suspect its something I've been doing wrong repeatedly in multiple locations), so any ideas would be appreciated.
Do you use Debug.Log
or print
when you hold down your key? Debug.Log and print are extremly slow! Never use them to print something every frame except it's for debugging purposes and you're going to remove them after testing.
Test this in a new project: create a simple scene and check the frame rate impact of pressing any key - this will tell you if the problem is in your project of in your machine (it may have some badly installed driver or some software running in the background that eats a lot of CPU cycles when a key is pressed)
Answer by fherbst · Nov 15, 2011 at 04:14 PM
A possible and not-so-obvious cause could be your OnGUI() calls.
Have a look a the reference for Events - each OnGUI is called once for each event (which includes KeyDown and such, even for keys not used by anything). OnGUI thus could be called multiple times in a single frame, and if your OnGUI method is quite expensive (doing game logic in there), that can drop the frame rate very quickly.
One solution to this is to check in each GUI call which event is currently happening, using the EventType:
void OnGUI() {
// this will make sure only one GUI event gets through each frame,
// because EventType.Repaint is sent only once
if((Event.current.type != EventType.Repaint) &&
(Event.current.type != EventType.Layout))
return;
// leave everything else as it was, drawing stuff, logic, etc.
// ...
}
But even with this check in place, if you use GUILayout methods, OnGUI will be called twice per frame (first, a Layout event, second, a Repaint event). If you have any expensive code in it, consider moving that into the Update() method, to make sure it gets called only once.
You're right that OnGUI can be called multiple times per frame, but usually that shouldn't have a great impact on the FPS. Also keep in $$anonymous$$d you block ALL other events now. All GUILayout stuff won't work / produce errors because you blocked the Layout event.
Of course it shouldn't have a great impact, but if you have game logic in your OnGUI call (like calling GameObjects.FindObjectsWithType, or a for loop with 1000 iterations, ...) it will linearily slow down your scene (if all your logic is in OnGUI, than pressing a button means half framerate), and thats why I stated "a possible cause could be". :)
I put that code in my Radar script and the frame loss when I pressed any key stopped.
Funny thing is, the script still functions normally even with that code in there, only now the problem is fixed. The radar script just iterates through an array of transforms that a radar sphere collider has collected and draws a dot for them on the GUI, so I still have no clue why its fixed. Even with no transforms in the array it would still slow down when any key was pressed and I didn't have if(Event.current.type != EventType.Repaint) return; in there.
$$anonymous$$ake sure that the array of transforms is being collected in Update, not in OnGUI- there's no need to do it more than once per frame.
@mercury_storm: Good to see its fixed. Could you mark the question as answered? And syclamoth is right - I modified my answer a bit.
Your answer
Follow this Question
Related Questions
Achieve framerate independent AddTorque rotaion with mouse drag. 2 Answers
Get DPad input value via GetButton instead of GetAxis? 8 Answers
How can I prevent Input.GetAxis from sporadically reporting deltas of 0.0? 0 Answers
Load scene on button press 4 Answers
Project in play mode creates a lagspike when pressing/releasing a key 1 Answer