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. Dismiss Notice

Reliability of Screen.dpi?

Discussion in 'Scripting' started by angrypenguin, Apr 13, 2013.

  1. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    Does anyone know how reliable the above is? It says it'll return 0 is unable to determine the DPI, but doesn't say under what circumstances that may happen. Is it a per-device thing that simply may or may not work on any given device, or is it more along the lines of not being able to determine the DPI for things like computer monitors?

    I want to zoom my camera in by 20% on devices with small screens so that players can see what's going on more easily. It's super easy to do, on the proviso that I can figure out how big the screen is. And I'm hoping that Screen.height / Screen.dpi will be reliable enough to work on all iOS and the majority of Android devices. It won't be game breaking if that's not the case, but it'd be cool to know how reliable I can expect that to be.
     
  2. Deeperbeige

    Deeperbeige

    Joined:
    May 12, 2013
    Posts:
    17
    I've been trying to survey this on Android devices recently. So far I've found it to be pretty accurate across the following devices:

    Nexus 10
    Nexus 4
    Galaxy S1, S3
    Galaxy tab 2 (7")
    HTC One

    Haven't found any devices where it's zero yet, except when running in the Unity editor on my PC.

    I think my approach will be to read the Screen.DPI value, and if it's negative, zero or implausible, use a substitute failsafe value based on the resolution. As long as the game still works with the failsafe value, you're golden.
     
  3. kreso

    kreso

    Joined:
    Sep 7, 2013
    Posts:
    133
    This is an old post, but google directs here. I just spent some time because it wasn't obvious so perhaps it saves some time for others:
    On iOS7.1 (tested in simulator) Screen.dpi returns -1.
    Unity 5.1.2
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I wonder how much the simulator has to do with this?
     
  5. kreso

    kreso

    Joined:
    Sep 7, 2013
    Posts:
    133
    Actually, you might be completely right.

    I ended up using -1 as a possible value in my code, just in case.
    Up until now I had detection of 0, but never bothered checking -1 so it caused some unexpected issues with my game (in the simulator).

    Thanks,
    Kreso
     
    Last edited: Jul 31, 2015