Author Topic: Size of UISprite after UIRoot Scaling? [Solved]  (Read 5334 times)

Lord Ned

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Size of UISprite after UIRoot Scaling? [Solved]
« on: August 21, 2013, 04:25:52 PM »
I'm using a 2D UI which has the Scaling Style set to "FixedSize" (and "FixedSizeOnMobiles" for well, Mobile devices). The Manual Height is set to 800px and the minimum/maximum height values don't seem to do anything.

What I'm trying to do:
I'm trying to programatically create a level select screen (an example of which can be found here: http://i.imgur.com/JaZtwnE.png ). The idea behind creating the layout programatically is to handle different width screens as the game is played in landscape mode. As part of programatically laying out the level select screen, I need to know how big the icons are on the screen (in pixels, after scaling) to see how many of them I could fit on the screen (ie: numIcons = Mathf.Floor(scaledIconWidth/Screen.Width);

The problem:
Any attempts at getting the size of the UISprite seem to give me the wrong number. I've tried all of the variations inside of NGUIMath such as CalculateRelativeWidgetBounds, CalculateAbsoluteWidgetBounds, CalculateReltativeInnerBounds, etc. Each of them gives me a different number, but none of them are the correct number. They seem to give me the numbers related to the sprite but before it actually gets scaled down by UI Root's FixedSize.

Is there a function, or some math that I can use to get the size of the UISprite's on-screen size after scaling, in pixels?
« Last Edit: August 22, 2013, 01:54:32 PM by Lord Ned »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Size of UISprite after UIRoot Scaling?
« Reply #1 on: August 22, 2013, 11:13:46 AM »
UIRoot.pixelSizeAdjustment is what you're looking for. You need to take it into account.

However... not sure why you'd want to to be honest. Fixed size means your UI will always be treated as that dimension. So for example if it's 800, that means the height from NGUI's point of view will always remain 800 pixels, regardless of screen resolution. This makes it really easy to position your stuff as Screen.height is irrelevant.

Min/max height only kick in if you go past that. For example if you have max set to 1080, and then you try to run your game on a retina screen with the height of 1800, the UI's scale will be adjusted so that it behaves as if the screen was only 1080 pixels tall.

Lord Ned

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Size of UISprite after UIRoot Scaling?
« Reply #2 on: August 22, 2013, 11:45:54 AM »
First off, thank you very much for the support. I've been struggling to find an answer to this for quite some time now and no one else who's used NGUI seems to have run across the same need as I.

UIRoot.pixelSizeAdjustment is what you're looking for. You need to take it into account.

Thank you! If I take my UISprite's size (125px) and divide it by UIRoot.GetPixelSizeAdjustment(MyUISprite.gameObject), I get 86 - which happens to be the size on screen which is exactly what I wanted!

Quote
However... not sure why you'd want to to be honest. Fixed size means your UI will always be treated as that dimension. So for example if it's 800, that means the height from NGUI's point of view will always remain 800 pixels, regardless of screen resolution. This makes it really easy to position your stuff as Screen.height is irrelevant.

This is correct. However, I am targeting devices in a landscape ratio, and didn't want wide bars on the edge of the screen (which would have been the result of laying out my levels in the most square aspect ratio there was and just leaving it up to NGUI's scaling to squash it down to fit.)

Thanks!


Edit:
Just an example of what it lets me do:



Same code automatically positions all of the icons in both examples and the UIRoot.GetPixelSizeAdjustment function was required to figure out how many of the icons I could fit given Screen.Width.
« Last Edit: August 22, 2013, 02:02:02 PM by Lord Ned »