Version: 2022.1
Interaction with browser scripting
Debug and troubleshoot WebGL builds

Input in WebGL

Gamepad and Joystick support

Unity WebGL supports Input and InputSystem for gamepads and joysticks for browsers that support the HTML5 Gamepad API.

Some browsers only allow access to input devices after the end user interacts with the device while the application has focus. This is a security measure that prevents the end user using connected devices for browser fingerprinting purposes. For this reason, your application should instruct the user to press a button on their gamepad/joystick before you call Input.GetJoystickNames() to check for connected devices.

The WebGL game controller mapping for the old input system aligns with the W3 spec, where the button mapping layout is shown as follows:

Buttons 描述
buttons[0] Bottom button in right cluster = CROSS (X)
buttons[1] Right button in right cluster = CIRCLE
buttons[2] Left button in right cluster = SQUARE
buttons[3] Top button in right cluster = TRIANGLE

Note: As WebGL follows the W3 spec, it might not be consistent with other platforms, and requires unique handling, especially, if you’re targeting multiple platforms simultaneously (for example, Windows and Web).

Touch support

Unity WebGL doesn’t officially support mobile devices (see WebGL browser compatibility) but it does implement Input.touches and other related APIs in browsers and devices with touch support. By default, mobile devices display a soft keyboard on the touch screen for entering text into UI input fields. To disable this behavior, use the WebGLInput.mobileKeyboardSupport property.

Keyboard input and focus handling

By default, Unity WebGL processes all keyboard input the web page receives, regardless of whether the WebGL canvas has focus or not. This is so the end user can start using a keyboard-based application without the need to click on the WebGL canvas.

Important: This can cause issues when there are other HTML elements on the page that should receive keyboard input, such as text fields. Unity consumes the input events before the rest of the page can receive them. To have HTML elements receive keyboard input, set WebGLInput.captureAllKeyboardInput to false. When you do this, the application only receives input if the WebGL canvas has focus.

Mobile Sensor support

Unity WebGL doesn’t officially support mobile devices (see WebGL browser compatibility) but, for browsers and mobile devices with touch support, Unity WebGL includes support for the following sensors:

Important: Browsers only allow sensor input in secure contexts. This means you must serve the page over HTTPS. The single exception is http://localhost, which you can use during development.

Additional resources:

Interaction with browser scripting
Debug and troubleshoot WebGL builds
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961