x


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.

more ▼

asked Nov 15 '11 at 02:22 PM

mercury_storm gravatar image

mercury_storm
1 1 1 2

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.

Nov 15 '11 at 04:29 PM Bunny83

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)

Nov 15 '11 at 05:17 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Nov 15 '11 at 04:14 PM

felix. gravatar image

felix.
1.9k 18 23 46

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 mind you block ALL other events now. All GUILayout stuff won't work / produce errors because you blocked the Layout event.

Nov 15 '11 at 04:33 PM Bunny83

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". :)

Nov 15 '11 at 04:40 PM felix.

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.

Nov 16 '11 at 03:56 AM mercury_storm

Make 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.

Nov 16 '11 at 04:03 AM syclamoth

@mercury_storm: Good to see its fixed. Could you mark the question as answered? And syclamoth is right - I modified my answer a bit.

Nov 16 '11 at 09:03 AM felix.
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x948
x196
x176
x35
x28

asked: Nov 15 '11 at 02:22 PM

Seen: 1102 times

Last Updated: Nov 16 '11 at 09:05 AM