Enable fullScreen WebGl

Screen.fullScreen = !Screen.fullScreen[quote=“it_ra, post:1, topic: 645964, username:it_ra”]
Hi all!

When working with WebGL build I’m trying to enable full screen mode.
When working with the standard template no problem.
I put in the GUI button and by pressing it launched different versions of the scripts.

Screen.fullScreen = !Screen.fullScreen;
Screen.SetResolution(Display.main.systemWidth, Display.main.systemHeight, true, 30);
  1. A call to an external script, didn’t work
Application.ExternalEval("SetFullscreen(1)");

Everything works and switches to full screen mode correctly. The problem is that it requires not 1 but 2 clicks of the mouse. The first button, the second click in any area of the browser, then starts a full screen mode.

Tell me, is there the possibility to switch to fullscreen mode of the game, with 1 click on the button standard template.

Thank you!
[/quote]
Hello it_ra.

Switching to fullscreen in a browser requires special privileges and can only be performed from an event handler initiated by the user. The problem is that Unity UI events go through an intermediate input queue, and are processed some time after their corresponding JavaScript handlers exit.

Still there is a workaround for this. You should just use onPointerDown event instead of onClick.
Create a UI Button, add a Event - Event Trigger component for this button, add Pointer Down event to the Event Trigger component, assign your function (with Screen.fullScreen = !Screen.fullScreen) to the Pointer Down event. Now it should work with a single click.

How this trick works:
As soon as the user pushes the UI button down, an onclick JavaScript event is registered for the whole page, so when user releases the mouse button somewhere over the page, a pure JavaScript onclick event handler is executed, where it is now possible to execute a privileged operation.

In case of switching fullscreen mode, you don’t need to implement any JavaScript handlers yourself, because those are already included by Emscripten. However, if you need to perform some other privileged operation on button click (i.e. open a popup, open a file opening dialog etc.), you may just add a corresponding JavaScript plugin, which will perform the onclick handling (see the window.open example here https://forum.unity3d.com/threads/popup-blocker-and-pointerdown-pointerclick.383233/#post-2491032)

15 Likes