Hello,
could anyone explain to me the relation between Input and the EventSystem and its Standalone Input Module?
Being a newbie, I made my own class “InputHandler” where I would call Input.GetKey() on Update(), using the Input class.
But I need to handle touch and mouse events and discovered I should use the EventSystem for that. But how are the two related, if at all? Is EventSystem a “wrapper” for the Input class, or are they completely different systems for handling input?
How should I design (in generally) input handling in respect to these classes / modules / methods? I’m using C#.
Thank you very much in advance!
x
The EventSystem is generally used for the UI (you’ll see it inherits from UIBehaviour). There’s GetTouch() and GetMouseXYZ() available in the Input class for directly reading of those states. You can safely use those inside your InputHelper.
Thank you very much! But I’m still a little confused.
Does that mean I should use the EventSystem only when handling UI, and ignore it when input should trigger in-game actions?
For example:
In the StandaloneInputModule is “Submit” defined, either by pressing enter or the mouse button. When a UI is active, I could click on a button to trigger an action from the UI. But what happens if the game is running in the background and is also listening for a mouse click with Input.GetMouseButton()? Then both are triggered, right?
So should I turn either the InputHandler or the StandaloneInputModule on and off, depending on the state of the UI (shown/not shown), or is there a better way to handle that?
Yes, when you add a Canvas to your scene, an EventSystem is automatically generated and added to your scene as well. You can tweak the EventSystem’s input module however you see fit.
To prevent clickthrough from your UI to your game objects (ones in the world, not the Unity term) you need to quite annoyingly check that EventSystem.current.IsPointerOverGameObject() is false. You can avoid that by adding callbacks to OnMouseXYZ in your game related MonoBehaviours but that may not always be the functionality you want, eg. raycasting when the player clicks.
In your UI related scripts, you either hook into the already existing callbacks from the components you’re using, eg. Button.onClick, or implement the IPointerClickHandler (or any of its variants) interface to receive the appropriate callbacks from the EventSystem.
It’s a fairly convoluted system but that’s because the Input system came first and the UI came much latter and needed different functionality.
2 Likes
Thanks GroZZleR, that cleared things up a bit.
Just adding this, but you can use the EventSystem and the IPointer / IDrag (etc.) interfaces for non-UI game objects, as well. If you do this, you won’t get a click pass-through, provided any top level game object blocks raycasts.
1 Like