Search Unity

Bug TypeError: screen.orientation.lock is not a function.

Discussion in 'Web' started by royi-mat, Apr 30, 2023.

  1. royi-mat

    royi-mat

    Joined:
    Jan 23, 2021
    Posts:
    7
    we have started getting this error while loading our webGL game on Safari browser.

    according to safari documentation, the lock / unload API is still experimental.

    i wonder how is it possible that unity invokes an experimental method of safari all of a sudden. we do enforce landscape orientation in our game.

    did anyone encounter this as well - i can't find anything on the subject here or anywhere.
     
    makaka-org and LaviK47 like this.
  2. LaviK47

    LaviK47

    Joined:
    Dec 21, 2021
    Posts:
    13
    This bug was introduced in Unity 2021.3. In Unity 2020.3 I am not getting this error.

    Attached a screenshot of the error in Safari and Chrome:

    upload_2023-5-4_15-3-31.png

    chrome: upload_2023-5-4_15-4-21.png
     
    makaka-org likes this.
  3. boriswp99

    boriswp99

    Joined:
    Mar 5, 2020
    Posts:
    1
    Has anyone figured out how to fix this error? Or just change the version of the unity?
     
    Saravana_Vins likes this.
  4. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    1,024
    In Unity 2022.3.2 it's still here.

    I have a problem with the getting incorrect orientation after changing the device orientation.

    Possibly, it's related to this bug.

    I guess that this affects the C#'s "Screen.orientation" as well as both Input Systems internally:
    • Old - Input Manager (iOS, Android)
    • New - Input System (iOS — so "Compensate For Screen Orientation" setting doesn't work sometimes).
    On the opposite side the next assets called Gyroscope Accelerometer WebGL and Screen Orientation WebGL get orientation from the JavaScript, and they don't have such issues.
     
    Last edited: Jun 19, 2023
  5. tmars

    tmars

    Joined:
    Jul 26, 2013
    Posts:
    42
    bump
     
  6. kit33

    kit33

    Joined:
    Jul 13, 2020
    Posts:
    1
    If you want just emergency stop the error , please patch for /PlaybackEngines/WebGLSupport/BuildTools/lib/ScreenOrientation.js

    Code (JavaScript):
    1.  
    2.         JS_ScreenOrientation_Lock: function(orientationLockType) {
    3.                 // We will use the Screen Orientation API if available, and silently return if not available
    4.                 // - https://www.w3.org/TR/screen-orientation/
    5.                 // - https://caniuse.com/screen-orientation
    6.                 // - https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation
    7.                 if (!screen.orientation) {
    8.                         // As of writing, this is only not implemented on Safari
    9.                         return;
    10.                 }
    11.                 // here patched. all browser disabled
    12.                 return;
    13.  
     
  7. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Hey, thanks for letting us know.

    Prior to Unity 2021.2, Unity Web platform did not support orientation locking. Orientation locking support for mobile browsers was added in Unity 2021.2 for the first time. That is when this code was first introduced.

    If a browser vendor releases an experimental browser feature in the stable namespace, Unity Web platform code will have no way of distinguishing between that. However looks like that is not what has happened here.

    The root cause is that Safari has added partial support for screen.orientation API to their browser in some newer version since December 2020 when Unity's implementation landed. This partial support in Safari adds the screen.orientation object, but incomplete, so it does not have the screen.orientation.lock() function.

    The author who added support for Screen Orientation Locking into Unity did not anticipate that a browser vendor would do a partial implementation like this. So they used the following code to check whether browser supports Screen Orientation API or not:

    Code (JavaScript):
    1.         // We will use the Screen Orientation API if available, and silently return if not available
    2.         // - https://www.w3.org/TR/screen-orientation/
    3.         // - https://caniuse.com/screen-orientation
    4.         // - https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation
    5.         if (!screen.orientation) {
    6.             // As of writing, this is only not implemented on Safari
    7.             return;
    8.         }
    Now based on what Safari has shipped, that check no longer properly detects the lack of Orientation Locking support in Safari.

    To fix, recommend changing the line

    Code (JavaScript):
    1. if (!screen.orientation) {
    into
    Code (JavaScript):
    1. if (!screen.orientation || !screen.orientation.lock) {
    in file ScreenOrientation.js inside Unity installation tree, and that should fix the issue. I am pushing that fix as a PR into the Unity tree.
     
    t_paine and makaka-org like this.
  8. FdenUijl

    FdenUijl

    Joined:
    Sep 5, 2019
    Posts:
    29
    Thanks for fixing this, any idea when this is in an official release?
     
  9. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Unfortunately I don't have a specific ETA. The PR was now reviewed and approved by engineering, and it went on to our QA to verify against iPhones with latest and older Safaris, and after that it will be approved by QA, and be able to land.

    After that the bug will be backported from main branch down to the different released versions branches, and be picked up to the next point releases of each branch. It will be a couple of weeks at least I presume, +/- a couple of weeks more, depending on how the backport release trains happen to align.
     
  10. FdenUijl

    FdenUijl

    Joined:
    Sep 5, 2019
    Posts:
    29
    Still not fixed in 2022.3.16f1
     
    makaka-org and yty like this.
  11. FdenUijl

    FdenUijl

    Joined:
    Sep 5, 2019
    Posts:
    29
    I have written this post-build script if you do not want to patch the javascript file (/PlaybackEngines/WebGLSupport/BuildTools/lib/ScreenOrientation.js) every time you update the unity version.


    Code (CSharp):
    1. public class WebGLPostBuildProcessor
    2.     {
    3.         [PostProcessBuild]
    4.         public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    5.         {
    6.             if (target == BuildTarget.WebGL)
    7.             {
    8.                 //get folder name
    9.                 string folderName = Path.GetFileName(pathToBuiltProject);
    10.                 string frameworkJsPath = Path.Combine(pathToBuiltProject, $"Build/{folderName}.framework.js");
    11.                 if (File.Exists(frameworkJsPath))
    12.                 {
    13.                     string content = File.ReadAllText(frameworkJsPath);
    14.  
    15.                     if (content.Contains("if(!screen.orientation){return}"))
    16.                     {
    17.                         Debug.Log("Replacing screen.orientation");
    18.                     }
    19.                     else
    20.                     {
    21.                         Debug.LogWarning("We can remove this post-build script if the issue is fixed in Unity 2022.3.17f1 or higher");
    22.                     }
    23.  
    24.                     content = content.Replace("if(!screen.orientation){return}", "return;");
    25.                     File.WriteAllText(frameworkJsPath, content);
    26.                 }
    27.             }
    28.         }
    29.     }
     
    makaka-org likes this.
  12. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    273
    We're backporting the fix that landed in the latest version! I don't think the bug report is public so I'll update this thread once they land.
     
    t_paine and makaka-org like this.