Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

WebGL and Screen DPI

Discussion in 'General Graphics' started by MaT227, Nov 23, 2015.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Hey everyone,

    I would like to know if there is a way to get the good Screen DPI in WebGL because it always return 0.

    To extend the question, how would you use the canvas scaler for WebGL to make the UI look good on every screen size and resolution ?

    Thank you very much !
     
  2. zulu_mashabella

    zulu_mashabella

    Joined:
    Apr 11, 2013
    Posts:
    16
    I have this problem too. And i don't know how i can determine screen dpi without Screen.dpi.
     
  3. inZania

    inZania

    Joined:
    Mar 3, 2016
    Posts:
    28
    I'm having trouble with this, too. Did you ever find a solution?
     
  4. Grhyll

    Grhyll

    Joined:
    Oct 15, 2012
    Posts:
    119
    Soo I guess there's no workaround?
     
  5. inZania

    inZania

    Joined:
    Mar 3, 2016
    Posts:
    28
    If you're just looking to support Retina builds, this worked perfectly for me: https://www.assetstore.unity3d.com/en/#!/content/74306

    If you really need to know the exact DPI, you can create a Javascript bridge to the Window.devicePixelRatio via a Unity WebGL plugin.
     
  6. Grhyll

    Grhyll

    Joined:
    Oct 15, 2012
    Posts:
    119
    Thanks for the tip, I'll look into that!
     
  7. GLeBaTi

    GLeBaTi

    Joined:
    Jan 12, 2013
    Posts:
    47
    UP. Screen.dpi return 0 in webGL
     
  8. robal1991

    robal1991

    Joined:
    Mar 31, 2016
    Posts:
    32
    Did you guys find any solution to get Screen dpi in webGL? I'm trying to make webGL work with windows screen scaling. It does not work as I expected it to work. Icons and texts are very blurry etc... I tried each of canvas scaler settings and it doesnt work. Desktop builds are working as expected.

    The link provided leads to main asset store page... Thanks for any help
     
  9. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    I'm currently using 2019.3.3 and Screen.Dpi always reports 0 in my WebGL builds.

    Is this a regression? Has this ever worked?
     
  10. jkelly_unity51

    jkelly_unity51

    Joined:
    Sep 24, 2019
    Posts:
    1
    Same problem as @jeffweber Using 2019.3.13f1 and I get a Screen.dpi = 0 when I push to the webGL build.
     
  11. fkorsa

    fkorsa

    Joined:
    Dec 23, 2018
    Posts:
    9
    FYI, here's the workaround mentioned by inZania:

    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.  
    3.     GetDPI: function ()
    4.     {
    5.         return window.devicePixelRatio;
    6.     },
    7.  
    8. });
    File: WebGLDPI.jslib

    Code (CSharp):
    1. using System.Runtime.InteropServices;
    2. using UnityEngine;
    3.  
    4. public class CrossPlatformScreenDPI : MonoBehaviour
    5. {
    6. #if UNITY_WEBGL
    7.     [DllImport("__Internal")]
    8.     private static extern double GetDPI();
    9. #endif
    10.  
    11.     private float dpi = 96.0f;
    12.     private bool isInitialized = false;
    13.  
    14.     private void Initialize()
    15.     {
    16. #if UNITY_WEBGL && !UNITY_EDITOR
    17.         double dpiFromJavascript = GetDPI();
    18.         Debug.Log("dpi from javascript: " + dpiFromJavascript);
    19.         dpi = (float) dpiFromJavascript * 96.0f;
    20. #else
    21.         dpi = Screen.dpi;
    22. #endif
    23.         Debug.Log("resulting dpi: " + dpi);
    24.         isInitialized = true;
    25.     }
    26.  
    27.     public float GetScreenDPI()
    28.     {
    29.         if (!isInitialized)
    30.         {
    31.             Initialize();
    32.         }
    33.         return dpi;
    34.     }
    35. }
    36.  
    File: CrossPlatformScreenDPI.cs

    Just attach the CrossPlatformScreenDPI component to a GO and call GetScreenDPI on it to get a correct value on all platforms, including webGL.
     
  12. radektesar

    radektesar

    Joined:
    Nov 1, 2017
    Posts:
    2
    Where to put the WebGLDPI.jslib file or how to link it to WebGL please?
     
  13. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    That script is partially wrong, It does work to get the devicePixelRatio from the WebGl.jslib but Screen.dpi is a different thing. Dots per inch means the amount of dots in an Inch line and this is where the Unity Scripting documentation gets weird because it says "This is the actual DPI of the screen or physical device running the application. Returns 0 if unable to determine the current DPI." but DPI is typically only important for printing. So if I run Screen.dpi I get 72 but if I run the script on WebGl I get 96 because the device pixel ratio is 1 in my case so 1 x 96 = 96.
    And according to Apple my screen has PPI of 128.5

    Correct me if I am wrong.
     
    Last edited: Apr 29, 2022