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? It took a fair bit of research.

Here's what we have tried:

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) {

     // 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");
  • 2
    \$\begingroup\$ Canvases do this automatically. Take a look at Canvas Scaler component. \$\endgroup\$ – S. Tarık Çetin Nov 20 '15 at 18:35
  • \$\begingroup\$ Possible duplicate of Resolution problems when porting a Unity game to Android \$\endgroup\$ – Vaillancourt Nov 20 '15 at 18:35
  • 1
    \$\begingroup\$ What is it about DisplayMetrics that isn't working for you? \$\endgroup\$ – fadden Nov 20 '15 at 18:57
  • 1
    \$\begingroup\$ widthInInches = Screen.width / Screen.dpi \$\endgroup\$ – bpgeck Nov 21 '15 at 0:33
  • \$\begingroup\$ This question comes from here, and has more solution suggestions there. \$\endgroup\$ – Vaillancourt Feb 9 '16 at 12:49

I'm giving you how I did this in java before I switched to Unity. Perhaps you can use it in c# with small modifications:

So lately I did it this way:

screenWidth = getResources().getDisplayMetrics().widthPixels;
screenHeight = getResources().getDisplayMetrics().heightPixels;  

But I used to do it this way too (probably deprecated):

Display display = getWindowManager().getDefaultDisplay();
            android.graphics.Point screenSize = new android.graphics.Point();
            screenWidth = screenSize.x;
            screenHeight = screenSize.y;
            screenWidth = display.getWidth();
            screenHeight = display.getHeight();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy