Hey,
We’re trying to migrate from the old UnityEngine.Input system to the new input system and we’re having some issues with click detection on WebGL. Our web application runs Unity WebGL in an HTML canvas with additional HTML elements on top of the Unity canvas.
In the old input system, we used Input.GetMouseDown(0) to check if the user is left clicking. The great thing about this approach is that it automatically accounts for the other HTML elements (the ones on top of the Unity canvas). If the user clicks on an HTML element that is not the canvas, Unity would not detect this as a left click. Input.GetMouseDown(0) would only return true if the user clicked on the canvas itself; not on any other element.
For the new input system, we set up an action that is listening to Left Button [Mouse] with a Button action type. Then, in the code we check if the user is left clicking using _input.Player.LeftClick.performed += … and performing logic whenever that event fires. The problem is that this event fires regardless if the user clicks on the Unity canvas or not. If the user is clicking on an HTML element that is on top of the Unity canvas, the input system will still detect it as a click. This does not match the behaviour of the old input system.
We looked further into the generated WASM to try and figure out what exactly is going on. It looks like the registration function used by Unity for mouse click input is emscripten_set_mousedown_callback_on_thread
. It seems like it’s using a target
parameter of 2
, which looks like it stands for document
(screenshot attached).
If instead we substitute that above for $WebGL_UnityCanvasSelector__, this fixes our issue. In other words, it seems feasible to add some configuration to let us choose whether or not we want to register clicks on a document
level or on a canvas
level.
Would it be possible to add this configuration to the input system? The hack above isn’t feasible for production and there’s no good workarounds.
Thanks!