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.

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.