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?

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");
			}
		}
	}
}

var widthInInches = Screen.width / Screen.dpi;

My C# solution

float inch = Mathf.Sqrt((Screen.width * Screen.width) + (Screen.height * Screen.height));
inch = inch/Screen.dpi;

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,