Hello Guest

Author Topic: Calculating correct dimension for Sliced Sprite  (Read 4407 times)

Vartib

  • Newbie
  • *
  • Posts: 21
    • View Profile
Calculating correct dimension for Sliced Sprite
« on: January 16, 2014, 04:44:56 am »
I'm using a sliced sprite as the background image for a menu that has content of varying sizes at any given time. Because of this I need to resize the sliced sprite so it is the same height as the content using dimensions. Here's a picture of the menu: http://imgur.com/Ol6fYDB The see through trapezoid is the sliced sprite, and it would need to be the same height as everything from the top of the large "Test" with the symbols next to it to the very bottom "Test Test Test".

I'm at a loss as to how this would be done. I'm using Renderer.bounds to position everything, but the size it gives me for the content (the combined bounds for everything from the big "Test" to the bottom "Test Test Test") is so far off of what the pixel dimensions are for the sliced sprite. For example, the y size of the content in the picture linked is 0.2991996, but the y pixel size of the sliced sprite is 280.

I have my UI camera set up as Orthographic with a size of 1, and the sprite collection size is set at Explicit with the default ortho size of 1 and target height of 640. Perhaps using a different combination would make this easier?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Calculating correct dimension for Sliced Sprite
« Reply #1 on: January 16, 2014, 10:44:51 am »
The sliced sprite dimensions is in pixel units. If you want to convert to local dimensions (should be the same as world dimensions if .localScale == 1), then sprite.CurrentSprite.texelSize * dimensions will give you world dimensions. You can use that relationship to calculate the correct dimensions given a world size. You don't really have to care about the camera size if you do it this way, you can precisely calculate it irrespective of camera size.

Vartib

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Calculating correct dimension for Sliced Sprite
« Reply #2 on: January 16, 2014, 04:54:14 pm »
Wonderful, thank you. I've got the main bit working, but it looks like I'm also going to need to calculate the size of the sliced sprite minus the bottom border (or the world position of the top of the bottom border; it's to ensure the symbol on the left is within the sliced sprite). I know there's .borderBottom, but I'm not sure what that value actually means. It only changes when I alter the size of the bottom border, not when the size of the sliced sprite itself is changed.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Calculating correct dimension for Sliced Sprite
« Reply #3 on: January 16, 2014, 09:52:56 pm »
borderBottom is the border in sprite fraction. i.e. if you get the original sprite dimension, Sprite.CurrentSprite.GetUntrimmedBounds(), then a fraction of the height is the world dimensions of the bottom border.