public float dpi{
get{
float dpi = Screen.dpi;
// If Unity is unable to detect the DPI of the screen,
// 1) Use 160 DPI as default value for mobile devices
// 2) Use 72 DPI as default value for other Mac.
// 3) Use 96 DPI as default value for other platforms.
if (dpi < Mathf.Epsilon){
if (
Application.platform == RuntimePlatform.Android ||
Application.platform == RuntimePlatform.IPhonePlayer
){
return 160f;
}else if(
Application.platform == RuntimePlatform.OSXEditor ||
Application.platform == RuntimePlatform.OSXPlayer ||
Application.platform == RuntimePlatform.OSXWebPlayer ||
Application.platform == RuntimePlatform.OSXDashboardPlayer
){
return 72f;
}else{
return 96f;
}
}
return dpi;
}
}
public float GetRealWorldSize(int pixels){
// The dpi of the devices are usually measured in diagonal,
// so we need to calculate the "diagonal number of pixels"
// per inch to calculate the correct pixel size (in inches).
float pixelSize = Mathf.Sqrt(2f) / this.dpi;
return (float)(pixels) * this.pixelSize;
}