Created
October 15, 2015 12:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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