- Home /
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?
Answer by Daniel-Brauer · Aug 31, 2011 at 07:44 PM
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");
}
}
}
}
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.
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?
Trying to figure out how to use this. Do i treat it like a static? do I add monodeveloper and add it to an object?
Answer by Waz · Jul 27, 2012 at 12:15 AM
var widthInInches = Screen.width / Screen.dpi;
Using Screen.dpi is much simpler and works across platforms as well.
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 Display$$anonymous$$etricsAndroid function...
@krifton Unfortunately the Unity developers do not appear very familiar with Android intricacies (in my experience, anyway) and appear to have assigned density to Screen.dpi
, which is based on a hardcoded value of 160. In all reality, this is only useful in deter$$anonymous$$ing the conversion from pixels to Android's scaled values, such as dp
or sp
, which are also based on a value of 160. Android added densityDPI to attempt some flexibility for varying device DPI, but it is easier to retrieve the values of get$$anonymous$$etrics
and getReal$$anonymous$$etrics
when accessing Display$$anonymous$$etrics
than try to perform any calculations with Unity's crippled versions.
Answer by ViicEsquivel · Aug 05, 2015 at 09:56 PM
My C# solution
float inch = Mathf.Sqrt((Screen.width * Screen.width) + (Screen.height * Screen.height));
inch = inch/Screen.dpi;
Answer by HarleysZoonBox · Oct 31, 2013 at 03:30 AM
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,
Your answer
Follow this Question
Related Questions
How to adjust the screen for many types of screens in Android? 1 Answer
Random X posistion between left and right edges of screen 2D 1 Answer
App plays well on Android phones but really slow on an Android tablet. 2 Answers
how to make the resolution fit the screen on the phone 0 Answers
How can i run a Unity Build on my PC, but use my phone as second screen, (Cheapskate Oculus-Like) 2 Answers