- Home /
Screen.dpi for in-editor
why does Screen.dpi return 0 for testing in editor
Answer by Dave-Carlile · Dec 21, 2012 at 02:50 PM
According to the documentation, Screen.dpi may return 0 if Unity is unable to determine the value. I would imagine this is some sort of device or driver issue rather than Unity. It may only function on mobile devices, but that's just a guess.
Searching here for Screen.dpi I ran across this:
http://answers.unity3d.com/questions/337460/scaling-gui-buttons-to-be-the-right-size-no-mobile.html
They have a GetDPI function that looks like this
private static float GetDPI()
{
return Screen.dpi == 0 ? DEFAULT_DPI : Screen.dpi;
}
Where DEFAULT_DPI is set to 160.0. This looks like a reasonable approach to deal with the issue.
I may be way off base here, but in Windows at least I don't think there's any way to get an actual DPI for your monitor. Windows has a DPI setting that can be changed at will, but it doesn't reflect the actual DPI of the monitor unless you set it to that yourself. You could, however, probably implement a C# library to get that value if needed.
yeah probably a better idea to have a helper function for dpi. nice use of ternary operators.
Answer by xtyler · Jun 15, 2019 at 06:36 PM
With Unity's high DPI support (2018.2 I think?) we got this: EditorGUIUtility.pixelsPerPoint! It works beautifully across monitors of different resolutions and is consistent with the sizing of other Editor GUI elements. https://docs.unity3d.com/ScriptReference/EditorGUIUtility-pixelsPerPoint.html
Answer by soxjke · Dec 21, 2012 at 02:56 PM
In play mode when free aspect is chosen for game view, dpi is exactly always 0. To get non-null dpi in editor specify target platform in build settings and display type in game view. But returned dpi will be your screen dpi. To debug actual dpi-dependent issues u have to debug on real deployment target(iOS device, Android device etc).
actually, even if you are in Android mode in editor, dpi will return 0
Answer by infinitypbr · Jan 24, 2013 at 01:24 AM
Retina mac returns 0 on game play, unfortunately :(
Your answer
Follow this Question
Related Questions
Is there a way to determine Android physical screen size? 5 Answers
Get Current Screen Window is Positioned In, Get Dpi of That Screen 1 Answer
How to obtain physical screen size in iOS simulator? 0 Answers
Detect physical resolution for showing SD or HD 0 Answers
Can I customize the resolution window that appears at the start of my executable? 2 Answers