x


Is there a way to determine Android physical screen size?

We're currently porting our games to Android, and would like to be able to release a universal build for tablets and phones. Is there a way to determine the actual screen size or DPI in order to adjust the size of interface elements accordingly?

more ▼

asked Aug 29 '11 at 03:15 PM

Daniel Brauer gravatar image

Daniel Brauer
1.6k 12 20 38

(comments are locked)
10|3000 characters needed characters left

7 answers: sort voted first

It took a fair bit of research, but in the end it was pretty easy. Hopefully this serves as a reasonable example of how to interact with Android's Java environment from within Unity.

using UnityEngine;

public class DisplayMetricsAndroid {

    // The logical density of the display
    public static float Density { get; protected set; }

    // The screen density expressed as dots-per-inch
    public static int DensityDPI { get; protected set; }

    // The absolute height of the display in pixels
    public static int HeightPixels { get; protected set; }

    // The absolute width of the display in pixels
    public static int WidthPixels { get; protected set; }

    // A scaling factor for fonts displayed on the display
    public static float ScaledDensity { get; protected set; }

    // The exact physical pixels per inch of the screen in the X dimension
    public static float XDPI { get; protected set; }

    // The exact physical pixels per inch of the screen in the Y dimension
    public static float YDPI { get; protected set; }

    static DisplayMetricsAndroid() {
       // Early out if we're not on an Android device
       if (Application.platform != RuntimePlatform.Android) {
         return;
       }

       // The following is equivalent to this Java code:
       //
       // metricsInstance = new DisplayMetrics();
       // UnityPlayer.currentActivity.getWindowManager().getDefaultDisplay().getMetrics(metricsInstance);
       //
       // ... which is pretty much equivalent to the code on this page:
       // http://developer.android.com/reference/android/util/DisplayMetrics.html

       using (
         AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"),
         metricsClass = new AndroidJavaClass("android.util.DisplayMetrics")
       ) {
         using (
          AndroidJavaObject metricsInstance = new AndroidJavaObject("android.util.DisplayMetrics"),
          activityInstance = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"),
          windowManagerInstance = activityInstance.Call<AndroidJavaObject>("getWindowManager"),
          displayInstance = windowManagerInstance.Call<AndroidJavaObject>("getDefaultDisplay")
         ) {
          displayInstance.Call("getMetrics", metricsInstance);
          Density = metricsInstance.Get<float>("density");
          DensityDPI = metricsInstance.Get<int>("densityDpi");
          HeightPixels = metricsInstance.Get<int>("heightPixels");
          WidthPixels = metricsInstance.Get<int>("widthPixels");
          ScaledDensity = metricsInstance.Get<float>("scaledDensity");
          XDPI = metricsInstance.Get<float>("xdpi");
          YDPI = metricsInstance.Get<float>("ydpi");
         }
       }
    }
}
more ▼

answered Aug 31 '11 at 07:44 PM

Daniel Brauer gravatar image

Daniel Brauer
1.6k 12 20 38

Brilliant. Thank you.

Mar 28 '12 at 02:32 AM iMugen

I already try to display each of the variables, but the variables always return 0. I don't know what's wrong. Can anyone help me?? Thank you.

Apr 17 '12 at 01:00 PM ShirleyXiao

Useful for all the other bits of information but if you just want the usable screen width and height in pixels why can't you use Screen.width & Screen.height?

Apr 17 '12 at 01:35 PM HazeTI
(comments are locked)
10|3000 characters needed characters left
var widthInInches = Screen.width / Screen.dpi;
more ▼

answered Jul 27 '12 at 12:15 AM

Waz gravatar image

Waz
7.1k 26 35 74

Using Screen.dpi is much simpler and works across platforms as well.

Feb 11 at 01:23 PM Elecman

I have a Samsung I9001 Galaxy S Plus, and its dpi is 233,33 for sure, but Screen.dpi returns 160.2105. This value is far away from reality. I'll try the DisplayMetricsAndroid function...

Feb 12 at 09:42 AM ktrifon
(comments are locked)
10|3000 characters needed characters left

To add to Daniel's answer, for those who do not know how to implement this script you can simply add it to the Plugins folder (in your project hierarchy, you can add one if you don't have one) and call any of DisplayMetricsAndroid() variables, for example:

Debug.Log(DisplayMetricsAndroid.WidthPixels);

ofcourse, it has to be used within an Android enviroment. Thanks again Daniel.

more ▼

answered Mar 28 '12 at 02:35 AM

iMugen gravatar image

iMugen
46 5 5 8

(comments are locked)
10|3000 characters needed characters left

unity gives us most of the basic resolutions and there is no problem for me when i use Screen.width or height i check them against variables and adjust accordingly no java needed... just use stored variable which by the way uses less overhead and adjust accordingly (check it in the Awake Function) to set before loading,

more ▼

answered Oct 31 '13 at 03:30 AM

HarleysZoonBox gravatar image

HarleysZoonBox
18 1 2

(comments are locked)
10|3000 characters needed characters left

Help, apply this in te GUITexture?

more ▼

answered May 06 '12 at 03:06 PM

gringofx gravatar image

gringofx
0 24 25 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3940
x673
x56
x48
x8

asked: Aug 29 '11 at 03:15 PM

Seen: 17041 times

Last Updated: Feb 12 at 09:42 AM