According to the Unity documentation, Screen.dpi will return 0 if Unity is unable to determine the dpi of the screen, so I was trying to provide a reasonable "default value" for each platform in case Unity couldn't retrieve the dpi of the screen. Why have I chosen those "magic numbers"?
I have chosen 160 dpi as the default value for mobile platforms because, according to the Android SDK documentation, "a 160 dpi screen is the baseline density assumed by the system for a 'medium' density screen", so I thought it was a good idea to use that value.
I chose 72 dpi as the default value for non-mobile platforms because I read in the Wikipedia that 72 dpi has been the default dpi value for Mac OS since 1980, but I didn't realize that the default dpi value for Windows is 96 dpi, so the code above should be modified to provide a different "default dpi values" Windows and Mac...
http://blogs.msdn.com/b/fontblog/archive/2005/11/08/490490.aspxAbout Linux, it seems that different distros use different "default dpi values", but I have seen a few distros that uses 96 dpi as default, so I think we can use that value safely.
//----------------------------------------//
I think the best place to invoke the GetRealWorldSize() method would be on the UIRoot script, something like this:
public int activeHeight
{
get
{
int height = Mathf.Max(2, Screen.height);
if (scalingStyle == Scaling.FixedSize) return manualHeight;
//---------------------------------------------------------------
// MY CHANGE (start)
//---------------------------------------------------------------
if (scalingStyle == Scaling.FixedRealWorldSize) return GetRealWorldSize(manualHeight);
//---------------------------------------------------------------
// MY CHANGE (end)
//---------------------------------------------------------------
#if UNITY_IPHONE || UNITY_ANDROID
if (scalingStyle == Scaling.FixedSizeOnMobiles)
return manualHeight;
#endif
if (height < minimumHeight) return minimumHeight;
if (height > maximumHeight) return maximumHeight;
return height;
}
}
Maybe it would be interesting having a "Scaling.FixedRealWorldSizeOnMobile" value too...