Author Topic: Sprite dimensions don't correspond to screen dimensions?  (Read 2761 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Sprite dimensions don't correspond to screen dimensions?
« on: November 23, 2013, 06:05:26 AM »
This is one of those weird things that I've always wanted to get rid of in NGUI.

I have a sliced sprite of 150x100 dimensions. Yet if I click on its right-most edge, I get an x value of around 125, and not 150! (See attachment)

  1. var screen = NGUITools.FindCameraForLayer(fromSlot.gameObject.layer).WorldToScreenPoint(Slot.background.cachedTransform.position);
  2. float x = (Input.mousePosition.x - screen.x);
  3. float y = (screen.y - Input.mousePosition.y);
  4. Debug.Log("x: " + x + "y: " + y);
  5.  

I need this to figure our how many slots I'm clicking away from the top-left slot. After I get the difference, I divide by the slot's template size (50) to get the right number:

  1. // continuation from previous code...
  2. float colDif = x / Owner.SlotSize;
  3. float rowDif = y / Owner.SlotSize;
  4. Debug.Log("col: " + Mathf.FloorToInt(colDif) + " rowDif: " + Mathf.FloorToInt(rowDif) + " sSize: " + Owner.SlotSize);
  5. AddingOffset = new Index2D((int)rowDif, (int)colDif);
  6.  

This will only work well, if I get a correct x and y. But since they're a bit lesser than the original values (~125 and not 150 for the width, and ~80 and not 100 for the height), my calculations all will go wrong.

Can somebody tell me why this is happening, and how to go about doing it the right way?

Thanks a lot!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite dimensions don't correspond to screen dimensions?
« Reply #1 on: November 23, 2013, 08:30:49 PM »
You shouldn't be working with the widget's position. It varies depending on the pivot point, and in case of world position it can also vary based on the UIRoot scale. Instead, use localCorners (or worldCorners depending on what you're doing). See my response here: http://www.tasharen.com/forum/index.php?topic=6755.0

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Sprite dimensions don't correspond to screen dimensions?
« Reply #2 on: November 24, 2013, 02:47:28 AM »
Sir, your are one the best developers the world has ever known! (at least to me) :)

It's now 95% accurate - I still get about 148 and not 150 when I click on the right edge of the sprite. Any idea why? could it be related to the sprite, since it's sliced, or is it the UIRoot affecting this as well...?

Lastly, should I always avoid using position/localPosition? when is it safe to use them?

PS: I think it would be VERY COOL to write a "Don't do it like this, but this" or "NGUI Gotchas" post - keeping us away from doing things the wrong way - maintaining awareness. - Like for example not to deal with position/localPosition directly, etc. It would a super time saver for people like me.

Thanks.
« Last Edit: November 24, 2013, 02:58:08 AM by vexe »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite dimensions don't correspond to screen dimensions?
« Reply #3 on: November 24, 2013, 05:44:27 AM »
You're probably not accounting for the sliced sprite border. In the picture you posted the sliced sprite extends past the arrow. It's easier to see the outline in the scene view.

Local position is only useful if you know it to be something like the center of a centered sprite. Otherwise, use the corners and calculate your own center.