Author Topic: Better DPI - UIRoot scaling for 'Flexible Scaling Style' - using device size  (Read 3508 times)

bogdandude

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Hey there!

We tweaked the UIRoot code a bit to do this:
  • Have a flexible scaling, starting from 640 height (iPhone) to 768 height (iPad)
  • For anything resembling a phone in size, up to a 5.5" device, we're still using the larger UI we set up at 640 height
  • For any device larger than 7", we're using the smaller UI (smaller buttons relative to the game view size), which we set up at the 768 height
  • For anything in between, we interpolate

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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Hmm interesting, thanks. This might be more of Nicki's expertise though.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Quite interesting; I'll take a look when I have a chance to. Good DPI handling something that NGUI would definitely benefit from.