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, 2011 at 03:15 PM

Daniel Brauer gravatar image

Daniel Brauer
1.7k 28 30 52

(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, 2011 at 07:44 PM

Daniel Brauer gravatar image

Daniel Brauer
1.7k 28 30 52

Brilliant. Thank you.

Mar 28, 2012 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, 2012 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, 2012 at 01:35 PM HazeTI
(comments are locked)
10|3000 characters needed characters left
var widthInInches = Screen.width / Screen.dpi;
more ▼

answered Jul 27, 2012 at 12:15 AM

Waz gravatar image

Waz
7.2k 66 63 104

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

Help, apply this in te GUITexture?

more ▼

answered May 06, 2012 at 03:06 PM

gringofx gravatar image

gringofx
0 69 50 50

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

really great, thanks a lot

more ▼

answered Nov 06, 2012 at 09:43 AM

cfloutier gravatar image

cfloutier
62 7 6 10

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

I'm fairly new to Unity so forgive me about the noob question. how do I implement this code into a Unity project? I want to stretch a background according to a device's resolution. I think your code is what I need but I don't know where and how to add or connect this to the background object. thanks for the help in advance.

more ▼

answered Jan 10, 2012 at 03:11 AM

taar1 gravatar image

taar1
66 11 10 11

First , NEVER COMMENT ( ASK QUESTION ) ON A ANSWER FORM !!! USE THE COMMENT FORM Second , they already explained how to do it, Copy paste from "Francis J" comment :

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.

Jun 25, 2012 at 06:54 AM hesa2020
(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:

x4548
x745
x70
x50
x11

asked: Aug 29, 2011 at 03:15 PM

Seen: 19844 times

Last Updated: Feb 12 at 09:42 AM