Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mercury_storm · Nov 15, 2011 at 02:22 PM · inputlagframerategetaxisgetbutton

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 15, 2011 at 04:29 PM 0
Share

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.

avatar image aldonaletto · Nov 15, 2011 at 05:17 PM 0
Share

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)

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 15, 2011 at 04:33 PM 0
Share

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.

avatar image fherbst · Nov 15, 2011 at 04:40 PM 0
Share

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

avatar image mercury_storm · Nov 16, 2011 at 03:56 AM 0
Share

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.

avatar image syclamoth · Nov 16, 2011 at 04:03 AM 0
Share

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

avatar image fherbst · Nov 16, 2011 at 09:03 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges