Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: jmorhart on February 27, 2015, 07:49:18 PM

Title: Scale UIWidget based on DPI
Post by: jmorhart on February 27, 2015, 07:49:18 PM
Hi all,

I would like to know if there is a way of scaling UIWidgets based on the screen DPI? I understand that the entire UI can be scaled that way by setting the UIRoot's scaling style to Flexible and checking Adjust By DPI. However, I want the majority of my UI to be Constrained, and have just a few UIWidgets scale based on DPI. Any ideas how I would go about doing this? Thanks!
Title: Re: Scale UIWidget based on DPI
Post by: ArenMook on March 01, 2015, 04:49:09 PM
You'd have to write code for that. Have a look at what "adjust by DPI" setting does and do something similar in a script attached to one or more of your widgets.
Title: Re: Scale UIWidget based on DPI
Post by: jmorhart on March 02, 2015, 04:41:03 PM
I figured I'd have to write some code, and I'm fine with that. I think I explained myself poorly as well. What I'd really like to do is scale a UIWidget based on a physical size. For example, make a UIWidget take up 1 inch on the screen no matter what the UIRoot's scaling mode is set to.
Title: Re: Scale UIWidget based on DPI
Post by: ArenMook on March 03, 2015, 05:31:09 PM
Yeah it's just a matter of math. Knowing the screen DPI and the desired size, calculate the size of the widget.

Just keep in mind screen DPI is not reliable. There is no 100% case where you can have something be 1 inch on every device.
Title: Re: Scale UIWidget based on DPI
Post by: jmorhart on March 05, 2015, 01:18:27 PM
Yep, that part I understand. Physical Size * DPI = size in pixels. So a 1" physical size @ 96 DPI = 96 pixels. The problem is that I have my UI's scaling style set to constrained by height, so that means it's using 96 virtual pixels. How do I convert those virtual pixels to physical pixels?
Title: Re: Scale UIWidget based on DPI
Post by: jmorhart on March 05, 2015, 02:01:22 PM
Figured it out, just had to wrap my head around the math :p Some pseudo code:

physicalPixels = sizeInInches * DPI
physicalPercent = physicalPixels / screenHeight;
spriteHeight = physicalPercent / uiRootActiveHeight;

Hope this can help someone else!