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.4k 12 19 38

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

6 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.4k 12 19 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
6.4k 22 33 71

(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
31 4 5 8

(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
-10 14 25 25

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

really great, thanks a lot

more ▼

answered Nov 06 '12 at 09:43 AM

cfloutier gravatar image

cfloutier
67 2 2 7

(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:

x2477
x485
x33
x27
x5

asked: Aug 29 '11 at 03:15 PM

Seen: 8874 times

Last Updated: Nov 06 '12 at 09:43 AM