Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed May 24, 2017 at 01:54 PM by DaRaVeNrK for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DaRaVeNrK · May 23, 2017 at 06:31 AM · c#animationgui

Why do we not yet have a getkeydown that returns a list of all keys down? Or all keys being held?

Input polling is lagging unity engine in a significant way.

I have recently switched to building input functionality using the event system in the ongui because polling the input class in the update function causes significant performance problems.

I've tested the input class using a primitive sphere that has a rigid body attached to it. Then creating a gamemanager that does little more than creating and holding a control class that polls and updates bools for the buttons that I want to be used by the player. For the test I polled all possible keys through the update function and the input class using system.enum to get all possible keys.

This caused noticeable performance delays in the falling sphere.

We need to get some input functions that would return a list of all key downs and keys being held.

If I am thinking about this the wrong way or you think it was the system.enum that caused the frame rate changes I am up for that discussion as well, but it seems right now that polling is dragging.

Comment
Add comment · Show 7
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 · May 23, 2017 at 07:17 AM 1
Share

What's the point in polling all possible keys? The only time when you need such a functionality is inside a key settings menu. Other than that you usually have a fix amount of keys you want to check.

How many keys do you currently poll inside Update? I doubt that polling even 100 keys would be even noticable. $$anonymous$$aybe you did something else wrong? Do you create any garbage each frame like an array? How does your code look like?

avatar image ShadyProductions Bunny83 · May 23, 2017 at 10:59 AM 1
Share

I agree with Bunny, please provide code so we can see if you are creating unnecessary garbage.

avatar image DaRaVeNrK Bunny83 · May 24, 2017 at 12:54 AM 0
Share

You might be right about creating unnecessary garbage but I wouldn't know how it did.

I used a polling method that enumerated through all of the buttons, but was just a test:

foreach($$anonymous$$eyCode v$$anonymous$$ey in System.Enum.GetValues(typeof($$anonymous$$eyCode))) { if(Input.Get$$anonymous$$ey(v$$anonymous$$ey)) { // do nothing for test } }

And as stated before the test did nothing except watch a sphere drop while this happened. The result was evident, the ball skipped.

Now that you mention garbage collection, I would agree that the apparent problems did resemble garbage collection issues. not all frames were sluggish.

avatar image bobisgod234 DaRaVeNrK · May 24, 2017 at 01:34 AM 1
Share

Have you tried running this test in a standalone build? Have you checked out the profiler to find the true source of the performance problems?

I tried your test code and it runs buttery smooth. I even put it in a for loop and ran it 10000 times on each update, and I still found zero performance problems.

avatar image tanoshimi · May 23, 2017 at 11:35 AM 1
Share

You want a method with a signature like List<$$anonymous$$eyCode> GetAll$$anonymous$$eysPressed() ? I can't really see how that would be helpful - the first thing you'd have to do is iterate through the result set and write a ton of:

 if(listOf$$anonymous$$eysPressed.Contains($$anonymous$$eyCode.W)) walkForward();
 if(listOf$$anonymous$$eysPressed.Contains($$anonymous$$eyCode.S)) walkBackward();
 if(listOf$$anonymous$$eysPressed.Contains($$anonymous$$eyCode.Space)) jump();
 etc. etc.

Which is no more convenient or performant than Input.Get$$anonymous$$ey(), surely?

avatar image DaRaVeNrK tanoshimi · May 24, 2017 at 01:03 AM 0
Share

I have begun to use the OnGUI event system for the input and I absolutely love that system. It does exactly what I was looking for.
It allows me to switch on the event.

like so... in the OnGUI of course.

// save current event to e

     e = Event.current;
                    
     
     if (e.is$$anonymous$$ey)
     {
         
         // update key modifiers from the event e
         $$anonymous$$odifierUpdate(e); // unincluded function: out of our context

         // keyboard event found
         Debug.Log("keyboard event found");
         Debug.Log(e.keyCode);
         Debug.Log(Time.timeSinceLevelLoad);
         switch (e.keyCode)
         {
             case ($$anonymous$$eyCode.Q):
                 if(e.type == EventType.keyDown)
                 {
                     Q$$anonymous$$ey = true;
                     Debug.Log("Q is down");
                 }
                 else if(e.type == EventType.$$anonymous$$eyUp)

This is masterful for what I need it for and cant believe i missed this before...
So, if this is what is intended by the unity $$anonymous$$m I like it, because doing all of the input in the update function is very single threaded.

So, I would have to take back the need for lists of changed keys while I can use the event system.

avatar image tanoshimi DaRaVeNrK · May 24, 2017 at 05:50 AM 2
Share

I'm struggling to see how that is an improvement.... Checking for input in OnGUI is just as "single-threaded" as it is doing it in Update(). And it's less reliable (do you know how many times per frame your code above is getting called? Hint - it's not once per frame).

As I said, you're having to use a bunch of if/switch of the event type and keyCode, which is less elegant and more fragile than using Input.Get$$anonymous$$eyDown/Input.Get$$anonymous$$eyUp - how are you going to account for runtime key rebinding, for example?

Anyway, none of that really matters so long as it's a solution that works for you!

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by DaRaVeNrK · May 26, 2017 at 09:50 AM

@Bunny83 and @ShadyProductions were on target for this problem witnessed here.

I changed the testing code after reading some information about the system.enum function and now believe that this was an invalid test.

The enumerator function was creating a lot of garbage that was having to be collected every couple frames. Still not quite sure why, but that is out of scope for this question/comment.

Thank you all for your support! RDL

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Unity Invoke method delay factor vary device to device 1 Answer

How to make Animated GUI element. 1 Answer

Custom AsyncOperation to show LoadingImage for slowish XML conversion & email upload method in Unity? 1 Answer

Animator.Update High CPU Usage on Unity 5 resulting in bugs on Android 0 Answers

GUI, SpellBar and C#-related questions 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