3
\$\begingroup\$

Been trying for days to figure out if there is a way to subscribe to keyboard/mouse events in unity. Instead of having to poll for them every frame and make my own events.

Right now I poll through the inputs and fire off my own events if a button is pressed. I'd prefer to subscribe directly to Unities input events, but have yet to figure out how.

I have a Unity Answers post that may have some relevant info to my question as well: http://answers.unity3d.com/questions/878485/is-there-a-more-efficient-way-to-make-events-for-k.html

\$\endgroup\$

3 Answers 3

6
\$\begingroup\$

There's no built-in way to do that. If you prefer an event based solution you can wrap all input polling inside a component and expose the relative and subscribe for them.

Some like:

public EventHandler : MonoBehaviour
{
  public event Action<Vector2> mouseCliked;

  void Update()
  {
    if (Input.GetMouseButtonDown(0))
    {
      mouseCliked(mousePosition);
    }
    ....
  }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks Heisenbug,bit sad that I can't But I can deal with it. \$\endgroup\$ Jan 24, 2015 at 17:02
3
\$\begingroup\$

If you want an event-based input handling in modern Unity, then you might want to use the new input system. While it can be used in a polling manner like the classic Input system, it is designed to be used in an event-based manner. There are in fact four different ways to use it in an event-based manner:

  • PlayerInput component with SendMessage, which will use the Unity messaging system and call methods on the same gameObject based on the name of the method. For example, when you have an input action "Jump", it will call any method named OnJump.
  • PlayerInput component with InvokeUnityEvents, which will allow you to set any methods of any object as event handlers in the inspector or the component.
  • Define variables in your script of type InputAction, which you either set via inspector or by finding the action by name via the public methods of an InputAction asset. Then subscribe to the .performed C# event of those input actions.
  • Auto-generate a C# class from the InputActions asset. You can then instantiate that class from a script and programmatically set up event subscriptions for the C# events it provides for all the input actions.

The new input system also offers a ton of other benefits over the classic one. Like more consistent handling of different kinds of gamepads, binding keyboard keys by location instead of letter and abstracting away lots of different input devices as consistent semantic input actions.

\$\endgroup\$
2
\$\begingroup\$

As said by Heisenbug is no built-in way to do this.

My recommendation is to explore the UnityEvent namespace new to Unity 4.6 and above. This will allow you to expose functionality to designers that will allow them to rig up interesting inputs and receivers.

You can go the extra mile and implement a custom editor with a serialized dictionary and button to add new listeners.

Edit: If you need to describe you could go with the dreaded singleton and process subscription requests to provide KeyCodes and press types.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ @that's true...UnityEvent could be a nice way to serialize delegates. \$\endgroup\$
    – Heisenbug
    Jan 23, 2015 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .