1
NGUI 3 Support / Better DPI - UIRoot scaling for 'Flexible Scaling Style' - using device size
« 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:
This goes into the 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
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:
- // 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;
- }
- }
This goes into the
- static public int AdjustByDPI(float height, int minimumHeight, int maximumHeight)
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
