Skip to content

Instantly share code, notes, and snippets.

@demonixis
Created October 15, 2015 12:46
Show Gist options
  • Select an option

  • Save demonixis/cb072e6783f008233497 to your computer and use it in GitHub Desktop.

Select an option

Save demonixis/cb072e6783f008233497 to your computer and use it in GitHub Desktop.
A static method to retrieve the screen inch of an Android device in Unity
public static int GetScreenInch()
{
#if UNITY_ANDROID
var displayMetrics = new AndroidJavaObject("android.util.DisplayMetrics");
var heightPixels = displayMetrics.Get<int>("heightPixels");
var widthPixels = displayMetrics.Get<int>("widthPixels");
var xdpi = displayMetrics.Get<float>("xdpi");
var ydpi = displayMetrics.Get<float>("ydpi");
var x = widthPixels / xdpi;
var y = heightPixels / ydpi;
var size = (x * x) + (y * y);
return Mathf.Sqrt(size);
#else
return -1;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment