Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: bogdandude on November 14, 2014, 04:42:00 AM

Title: Better DPI - UIRoot scaling for 'Flexible Scaling Style' - using device size
Post by: bogdandude on November 14, 2014, 04:42:00 AM
Hey there!

We tweaked the UIRoot code a bit to do this:

This seems to work great across all iOS and Android devices, and we no longer see tiny UI elements on 1920x1080 Android screens.

Here's the modified source code:

  1. // CUSTOM HEIGHT CALCULATOR BASED ON DPI
  2.         {
  3.  
  4.             if (dpi == 0)
  5.             {
  6. #if UNITY_EDITOR
  7.                 dpi = 160;
  8. #else
  9.                 dpi = (platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer) ? 160f : 96f;
  10. #endif
  11.             }
  12.  
  13.             //Diagonal value for which below we use the minimum Height we desire
  14.             float minDiagonal = 5.5f;
  15.             //Diagonal value for which above we use the maximum Height we desire
  16.             float maxDiagonal = 7f;
  17.  
  18.             //Limit values for the height
  19.  
  20.             int devWidth = Screen.width * Screen.width;
  21.             int devHeight = Screen.height * Screen.height;
  22.             float ypotinousa = devHeight + devWidth;
  23.             ypotinousa = Mathf.Sqrt(ypotinousa);
  24.             //Diagonal size in inches
  25.             float diagonalInches = ypotinousa / dpi;
  26.  
  27.             if (diagonalInches <= minDiagonal)
  28.                 return minimumHeight;
  29.             else if (diagonalInches >= maxDiagonal)
  30.                 return maximumHeight;
  31.             else
  32.             {
  33.                 //IF diagonal is in between the limits we adjust the height to it
  34.                 float diagonalDifference = maxDiagonal - minDiagonal;
  35.                 float diagonalPercantege = ((float)diagonalInches - minDiagonal) / diagonalDifference;
  36.  
  37.                 float widthDifference = maximumHeight - minimumHeight;
  38.                 int targetHeight = Mathf.RoundToInt(minimumHeight + (widthDifference * diagonalPercantege));
  39.                
  40.                 return targetHeight;
  41.             }
  42.         }

This goes into the
  1. static public int AdjustByDPI(float height, int minimumHeight, int maximumHeight)
  function, in NGUIMath.cs

I hope you guys will find it useful too, and maybe we'll someday see this included as standard in future NGUI versions :)

Bogdan

Angry Mob Games
Title: Re: Better DPI - UIRoot scaling for 'Flexible Scaling Style' - using device size
Post by: ArenMook on November 14, 2014, 05:06:36 AM
Hmm interesting, thanks. This might be more of Nicki's expertise though.
Title: Re: Better DPI - UIRoot scaling for 'Flexible Scaling Style' - using device size
Post by: Nicki on November 14, 2014, 08:33:09 AM
Quite interesting; I'll take a look when I have a chance to. Good DPI handling something that NGUI would definitely benefit from.