Legacy Documentation: Version 5.2
Using Audio In WebGL
Memory Considerations when targeting WebGL

Cursor locking and fullscreen mode in WebGL

Cursor locking (using Cursor.lockState) and Fullscreen mode (using Screen.fullScreen) are both supported in Unity WebGL, implemented using the respective HTML5 APIs (Element.requestPointerLock and Element.requestFullscreen). These are supported in Firefox and Chrome, Safari cannot currently use fullscreen and cursor locking.

How to enable cursor locking and fullscreen mode in WebGL?

Due to security concerns, browsers will only allow locking the cursor or going into fullscreen mode in direct response to a user-initiated event (like a mouse click or key press). Unfortunately, due to the way Unity handles events (it does not have separate event and rendering loops), Unity defers event handling to a point where the browser will no longer see a fullscreen or cursor lock request issued from Unity scripting to be a direct response to the event which triggered it. Unity will then trigger the request on the next user-initiated event.

In order to make this work with acceptable results, we recommend that you trigger fullscreen or cursor locking requests on mouse/key down events (as opposed to mouse/key up events as you would usually use). That way, when the request is deferred to the next user-initiated event, it will get triggered when the user releases the mouse or key.

If you use Unity’s UI.Button component, you can achieve the desired behaviour by creating a subclass of Button, which overrides the OnPointerDown method.

Note that browsers may show a notification message and/or ask the user for permission before entering fullscreen mode or locking the cursor.

Using Audio In WebGL
Memory Considerations when targeting WebGL
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961