Search Unity

Enable fullScreen WebGl

Discussion in 'Web' started by it_ra, Nov 15, 2016.

  1. it_ra

    it_ra

    Joined:
    Feb 28, 2015
    Posts:
    5
    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.

    1.
    Code (CSharp):
    1. Screen.fullScreen = !Screen.fullScreen;
    2.
    Code (CSharp):
    1. Screen.SetResolution(Display.main.systemWidth, Display.main.systemHeight, true, 30);
    3. A call to an external script, didn't work
    Code (CSharp):
    1. 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!
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    I don't think there is a way around it, unless you switch to Fullscreen by default, then the user will only have to click once :)
     
  3. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    Screen.fullScreen = !Screen.fullScreen
    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)
     
    Last edited: Nov 15, 2016
    Graph, travlake, raulprofe and 10 others like this.
  4. it_ra

    it_ra

    Joined:
    Feb 28, 2015
    Posts:
    5
    Hello alexsuvorov.

    Thank you so much.
    After replacing the event the mouse down it worked perfectly
     
  5. Kristof_Newfort

    Kristof_Newfort

    Joined:
    Feb 21, 2014
    Posts:
    24
    For those who are wondering how you can make this work in the newer Unity's (starting from 2017 I think), this is how you get the fullscreen WebGL working on Android for Chrome and Firefox (iOS does not support fullscreen like it does on Android):

    JSLib
    Code (JavaScript):
    1. var LibraryPageTool = {
    2.     GoFullscreen: function()
    3.     {
    4.         var viewFullScreen = document.getElementById('#canvas');
    5.  
    6.         var ActivateFullscreen = function()
    7.         {
    8.             if (viewFullScreen.requestFullscreen) /* API spec */
    9.             {  
    10.                 viewFullScreen.requestFullscreen();
    11.             }
    12.             else if (viewFullScreen.mozRequestFullScreen) /* Firefox */
    13.             {
    14.                 viewFullScreen.mozRequestFullScreen();
    15.             }
    16.             else if (viewFullScreen.webkitRequestFullscreen) /* Chrome, Safari and Opera */
    17.             {  
    18.                 viewFullScreen.webkitRequestFullscreen();
    19.             }
    20.             else if (viewFullScreen.msRequestFullscreen) /* IE/Edge */
    21.             {  
    22.                 viewFullScreen.msRequestFullscreen();
    23.             }
    24.  
    25.             viewFullScreen.removeEventListener('touchend', ActivateFullscreen);
    26.         }
    27.  
    28.         viewFullScreen.addEventListener('touchend', ActivateFullscreen, false);
    29.     }
    30. };
    31. mergeInto(LibraryManager.library, LibraryPageTool);
    CSharp
    Code (CSharp):
    1. using UnityEngine;
    2. #if UNITY_WEBGL && !UNITY_EDITOR
    3. using System.Runtime.InteropServices;
    4. #endif
    5.  
    6. public class PageTool : MonoBehaviour
    7. {
    8. #if UNITY_WEBGL && !UNITY_EDITOR
    9.     [DllImport("__Internal")]
    10.     private static extern void GoFullscreen();
    11.  
    12.     public static void ActivateFullscreen()
    13.     {
    14.         GoFullscreen();
    15.     }
    16. #else
    17.     public static void ActivateFullscreen()
    18.     {}
    19. #endif
    20. }
    As you can see 'canvas' has changed to '#canvas' in the newer WebGL builds.
    Also, just replace the 'touchend' with 'click' or 'mouseup' if you want this to work on desktop browsers.

    I hope this can help someone.
     
    Last edited: Sep 10, 2019
  6. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    179
    Thanks, where should this code go? Is it enough to put it in a c# script attached to the main camera?
     
    Elieske likes this.
  7. leon-do

    leon-do

    Joined:
    Jul 6, 2020
    Posts:
    4
    LucianoJung likes this.
  8. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    @alexsuvorov
    This does not work consistently on version 2020.1 beta anymore. Sometimes it works, but sometimes I get the message
     
    mdrunk and JeffreyBennett like this.
  9. Good_Punk

    Good_Punk

    Joined:
    Aug 6, 2014
    Posts:
    81
    I can confirm this. OnMousDown and Input.GetMouseButtonDown(0) still seem to work but you have to implement the check if that's above your ui element manually it seems.
     
  10. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
  11. dradb

    dradb

    Joined:
    Jan 10, 2015
    Posts:
    86
    Marks4 likes this.
  12. kiksunitydev

    kiksunitydev

    Joined:
    Mar 18, 2021
    Posts:
    9
    GET /
    GET /TemplateData/style.css
    GET /TemplateData/UnityProgress.js
    GET /%UNITY_WEBGL_LOADER_URL%
    ERROR: GET /%UNITY_WEBGL_LOADER_URL% URIError: URI malformed
    GET /%UNITY_WEBGL_LOADER_URL%
    ERROR: GET /%UNITY_WEBGL_LOADER_URL% URIError: URI malformed

    I am getting this error
    Unity version is 2021.1.6f1

    Build link - https://drive.google.com/file/d/1wOld77KwtlE9tNM_aEPR_w_ynwfDgB5g/view?usp=sharing
    Could you please check and get back
     
  13. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
  14. kiksunitydev

    kiksunitydev

    Joined:
    Mar 18, 2021
    Posts:
    9
    Thanks! doing that and having canvas size as inner width and height of web page
     
  15. BenrajSD

    BenrajSD

    Joined:
    Dec 17, 2020
    Posts:
    25
    One more thing, is any settings specifically to be made for build working in Safari - for both Mac OS and iOS, having issue. With all other browsers in mobile (Android/iOS) and desktop for Mac and Windows are working.

    Build itself not get loaded.

    your browser does not support graphics api webgl 2.0 which is required for this content
     
    Last edited: May 22, 2021
  16. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    This is a known issue with Apple. They have decided not to adopt WebGL 2 API in Safari browser. Even though the API is counting towards being 5 years old, they have decided to not provide the functionality. Unfortunately there is nothing we can do, we have pleaded them to add support for it for several years. There is a tech preview version of WebGL 2 available in Safari, but we do not know if/when it will ship. Maybe they think that it would compete with Apple's App Store too much if they enabled WebGL 2 in Safari.
     
  17. rajeshkalavakunta

    rajeshkalavakunta

    Joined:
    Aug 20, 2021
    Posts:
    2
    By default, will get a full screen without a footer in WebGL build.

    -> Open the index.html file to edit

    -> Copy the following code inside the body

    Code (JavaScript):
    1.  <body>
    2.    <div class="webgl-content" style="display: table; width: 100%; height: 100%;">
    3.      <div id="unityContainer"></div>
    4.    </div>
    5.    <script>
    6.    var mywidth = innerWidth;
    7.    var myheight = innerHeight;
    8.        document.getElementById("unityContainer").style.width = mywidth + "px";
    9.        document.getElementById("unityContainer").style.height = myheight + "px";
    10.    </script>
    11.   </body>
     
  18. dradb

    dradb

    Joined:
    Jan 10, 2015
    Posts:
    86
  19. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    @dradb Are you using a pointerdown event?
     

    Attached Files:

    Erishno likes this.
  20. dradb

    dradb

    Joined:
    Jan 10, 2015
    Posts:
    86
    Thank you for your prompt reply.
    I'm just using the demo scene "fullscreenwebGL" that you supply. It has a "pointerdown" on the "Enter" button.
    It detects full screen (or not fs) correctly and displays the appropriate button but the buttons don't work.
     
    Last edited: Dec 15, 2021
  21. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    Is it like this on your scene?
    upload_2021-12-15_4-4-35.png

    Change to
    upload_2021-12-15_4-5-9.png

    Something must've gone wrong when I last updated this asset. Please confirm if this is the issue.
     
  22. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    @dradb I saw your review on the store. Can you tell me if the solution above fixes the problem? I tried here and it works. I will update the plugin.

    EDIT

    Oh it's because on Unity 2019 you can't select static methods in the inspector...ok will fix asap.

    EDIT

    It's fixed, I submitted to the store.
     
    Last edited: Dec 15, 2021
  23. dradb

    dradb

    Joined:
    Jan 10, 2015
    Posts:
    86
    Thanks Mark. Yes I saw that "missing".
    Do you know how long it takes to get through? Still showing the old version.
     
  24. ReenaRay

    ReenaRay

    Joined:
    May 6, 2022
    Posts:
    2
    @Marks4 I am using your fullscreen asset in my game. It was made in 2019.4.23. Its working in a separate project. But when i import in my original project it is not working even though pointerdown is properly implemented in the button
     
  25. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    My plugin was broken on Unity 2021, I recently updated it. I have a few questions.
    1. Are you using the latest version of my plugin?
    2. What Unity version are you using?
    3. Can you make a sample scene with the problem and share it?
    Reach out to me at marjob14@gmail.com to discuss further.
     
  26. ReenaRay

    ReenaRay

    Joined:
    May 6, 2022
    Posts:
    2
    1. I have recently updated you plugin
    2.I’m using unity 2019.4.23
    3.While i build the sample scene from the same unity version the plugin is working but in my original project it shrinks the screen, I will share you the scenario in your mail. Thanks for replying