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
1
Question by LPGaming · Nov 23, 2012 at 12:46 AM · keypress

Best way to handle keypress?

I'm still new to all of this, but most tutorials call key-press inside of the Update function, which is called once per frame... Now, when using a Key-UP to see when a key was pressed or (KeyDown) whichever works.

Either way, I'm going to be drawing a GUI when a player hits a certain key, although I would think that if I did this inside of the Update() method it would draw the GUI EVERY FRAME, which... would eventaully cause a crash. If I'm not mistaken, perhaps I am. Anybody care to explain?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Democre · Nov 23, 2012 at 01:12 AM

What you should do is set a flag when the key is pressed. In your OnGUI, check the flag and draw if true.

 public bool drawingGUI = false;
 
 void Update()
 {
     if(Input.GetKeyUp(KeyCode.X))
         drawingGUI = true;
 }
 
 void OnGUI()
 {
     if(drawingGUI) {
         //draw all your GUI stuff
         //include a line to set your flag back to false whenever you want to hide the GUI
         if (GUI.Button(Rect(10,70,50,30),"Dismiss"))
             drawingGUI = false;
     }
 }

Hope this helps

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
avatar image
3

Answer by Bunny83 · Nov 23, 2012 at 01:26 AM

Unity provides any kind of input only between visual frames / updates. That means during one Update cycle all Input data will stay the same. Unity has a lot of ways to process input:

  • Use the Input class in Update.

  • Use the Event class in OnGUI.

Basically OnGUI is ment for GUI events in the first place. The Event class is purely "event based", so it only provides information when something changed (key down, key up)

The Input class provides two different kinds of functions:

  • Event based (GetKeyDown, GetKeyUp, GetButtonDown, GetButtonUp, ...)

  • State based (GetKey, GetButton, GetAxis)

In order to react to Input you have to check it every frame. Functions like OnKeyDown will only be true for exactly one frame right after the event occurred.

Keep in mind this important rules:

  • Avoid the Input class in OnGUI. If you want to check input there use either the Event class or process the input in Update, set a variable and use the variable in OnGUI.

  • Never use the Event class outside of OnGUI. It only works in OnGUI (or in functions called from inside OnGUI of course).

Next thing is what you mean by "drawing a GUI". Unity's GUI system (OnGUI and the GUI / GUILayout classes) uses an immediate mode, so OnGUI is called every frame after the cameras are drawn and just renders the GUI on top of the screen. So whatever you "draw" in OnGUI will be visible as long as you draw it every frame. When you want to "remove" a GUI element, just don't draw it anymore.

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
avatar image
0

Answer by LPGaming · Nov 23, 2012 at 09:59 PM

Thanks to both of you, although Democre provided a code sample which helped me understand what he meant, I'm honestly suprised I didn't think of it. My question is, wouldn't calling something or even setting a variable EVERY SINGLE FRAME.... cause lag? especially in a multi-player setting?

Comment
Add comment · Show 2 · 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 Democre · Nov 23, 2012 at 10:08 PM 0
Share

This shouldn't be its own answer, just a comment on the original.

But to answer this question, the amount of lag introduced is extremely $$anonymous$$imal. The Input system is already more or less polling as often as it can, so checking it or setting a variable based on it, does nothing noticeable. The time it takes to set a variable is effectively controlled by the speed of your RA$$anonymous$$. You will find bottle necks in many other areas of your code long before you find bottle necks in variable assignment.

avatar image Clonkex · Nov 16, 2014 at 10:46 AM 0
Share

No haha. Calling a function or setting a variable every frame will not cause a slowdown. In fact, most games will be calling hundreds if not thousands of functions every frame and setting just as many variables. Obviously there is a limit, but it's much, much higher than you assume :)

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

11 People are following this question.

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

Related Questions

Key Press Queuing 2 Answers

How do I rotate an object at a constant rate a specific angle on key down? 3 Answers

How to create a collider-trigger that will start an animation when you press E 0 Answers

Multiple keypresses at once aren't regitered? 1 Answer

Implementing collision and key pressing at once 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