// CUSTOM HEIGHT CALCULATOR BASED ON DPI
{
if (dpi == 0)
{
#if UNITY_EDITOR
dpi = 160;
#else
dpi = (platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer) ? 160f : 96f;
#endif
}
//Diagonal value for which below we use the minimum Height we desire
float minDiagonal = 5.5f;
//Diagonal value for which above we use the maximum Height we desire
float maxDiagonal = 7f;
//Limit values for the height
int devWidth = Screen.width * Screen.width;
int devHeight = Screen.height * Screen.height;
float ypotinousa = devHeight + devWidth;
ypotinousa = Mathf.Sqrt(ypotinousa);
//Diagonal size in inches
float diagonalInches = ypotinousa / dpi;
if (diagonalInches <= minDiagonal)
return minimumHeight;
else if (diagonalInches >= maxDiagonal)
return maximumHeight;
else
{
//IF diagonal is in between the limits we adjust the height to it
float diagonalDifference = maxDiagonal - minDiagonal;
float diagonalPercantege = ((float)diagonalInches - minDiagonal) / diagonalDifference;
float widthDifference = maximumHeight - minimumHeight;
int targetHeight = Mathf.RoundToInt(minimumHeight + (widthDifference * diagonalPercantege));
return targetHeight;
}
}