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 douglasg14b · Jan 17, 2015 at 06:22 PM · eventdelegate

Is there a more efficient way to make events for keyboard key presses?

I'm trying to use events to handle keyboard key presses instead of calling a function each time a key is pressed, to make the game easier to work with.

The issue I'm having is that creating the event for a single key is quite long, and will have to be looped every frame in Update(). This seems inefficient, after a while you are looping through a couple hundred checks per frame. I read up on EventType.KeyDown, EventType.KeyUp,and Event.current , but was unable to figure out how to use those instead of making my own events.

Thoughts?

 public delegate void UpButtonHandler();
 public static event UpButtonHandler UpButtonHeld;
 public static event UpButtonHandler UpButtonUp;
 
 private static void UpButton(){
     if (Input.GetButton("KeyUp")){
         if (UpButtonHeldEvent != null)
             UpButtonHeldEvent();
     }
 }
 private static void UpButtonUp(){
     if (Input.GetButtonUp("KeyUp")){
         Debug.Log("working?");
         if (UpButtonUpEvent != null)
             UpButtonUpEvent();
     }
 }


Edit:

Just discovered:

     public void test(){
         if (Event.current.keyCode == KeyCode.W){
             Debug.Log("Event works");
         }
     }

This seems to work, but it has to be ran in OnGUI(){}. Is there any disadvantage to running something like this in OnGUI vs Update() ? I also can't think of a way to add subscribers to such an event.

Day 1 Edit: Started using the first example in my application, though am still very much interested in the 2nd Event.current and how I might be able to make a subscriber to that event, as well as the performance of the first way.

Day2: Still was unable to find out much more than this. The events work well (amazingly well) though. Edited example a bit.

Day5: Not finding out much more :/

Comment
Add comment · Show 3
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 HarshadK · Jan 23, 2015 at 06:24 AM 0
Share

I would say, don't try to fix if it ain't broke.

avatar image fafase · Jan 23, 2015 at 06:32 AM 0
Share

I don't really see the point of rewriting something that is working. On top of that, you will hardly get the same performance UnityEngine is providing since input is probably run on the C++ side.

Finally, ever since 4.6 OnGUI is dead to me. There is a reason why they call it legacy GUI and why some companies spent so long to develop better version (NGUI for instance).

Just leave OnGUI in the drawer and just take it out to re$$anonymous$$d you how harder the old days were.

avatar image douglasg14b · Jan 23, 2015 at 07:03 AM 0
Share

What do you guys mean?

If there is a way to subscribe to the unity input events, how do I do that? Otherwise I have to poll for them and publish my own events in the way mentioned above.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

C# Delegates, a question 1 Answer

Why doesn't unregistering an anonymous delegate trigger an error? 1 Answer

Sharing delegate types across scripts 1 Answer

Destroy(gameObject) only works once 1 Answer

Create delegate from MethodInfo 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