Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by Daniel-Brauer · Aug 29, 2011 at 03:15 PM · androidscreenphonetabletdpi

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
27
Best Answer

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");
             }
         }
     }
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iMugen · Mar 28, 2012 at 02:32 AM 0
Share

Brilliant. Thank you.

avatar image ShirleyXiao · Apr 17, 2012 at 01:00 PM 0
Share

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.

avatar image HazeTI · Apr 17, 2012 at 01:35 PM 0
Share

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?

avatar image Cress7 · Jul 09, 2016 at 11:22 PM 0
Share

Great! (and still works in 2016)

avatar image Sir-Gatlin · Dec 29, 2017 at 08:48 PM 0
Share

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?

Show more comments
avatar image
5

Answer by Waz · Jul 27, 2012 at 12:15 AM

 var widthInInches = Screen.width / Screen.dpi;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Elecman · Feb 11, 2014 at 01:23 PM 1
Share

Using Screen.dpi is much simpler and works across platforms as well.

avatar image ktrifon · Feb 12, 2014 at 09:42 AM 0
Share

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...

avatar image LoungeKatt ktrifon · Nov 21, 2018 at 03:44 PM 0
Share

@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.

avatar image
3

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;


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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,

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges