Author Topic: Scale UIWidget based on DPI  (Read 2778 times)

jmorhart

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Scale UIWidget based on DPI
« 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!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scale UIWidget based on DPI
« Reply #1 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.

jmorhart

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Scale UIWidget based on DPI
« Reply #2 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scale UIWidget based on DPI
« Reply #3 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.

jmorhart

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Scale UIWidget based on DPI
« Reply #4 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?

jmorhart

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Scale UIWidget based on DPI
« Reply #5 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!