Author Topic: Absolute position of a texture  (Read 1917 times)

Roots

  • Guest
Absolute position of a texture
« on: November 23, 2013, 12:29:41 AM »
Hi,

Is there an easy way to get the absolute position off a texture in NGUI? I have a 2D texture and want to know were exactly the user clicked. Maybe this is basic Unity stuff but was hoping someone tackled this already.

For the X i can simply take screenwidth - width of the texture, call this TextureX (as the texture is on a top-right anchor) and then use UICamera.lastTouchPosition.x - TextureX.

For the Y it is a bit trickier as the texture is not at the top OR bottom of the screen but somewhere in the middle.

Any help would be great.

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Absolute position of a texture
« Reply #1 on: November 23, 2013, 08:27:40 PM »
You shouldn't use just flat out position/localposition. Instead use UIWidget.localCorners. This gives you the local corner positions. Take the mouse position, ScreenToWorldPoint it so it's in world space, then widgetTransform.InverseTransformPoint it into local space. You now have a local rectangle, and a local point. The rest is easy.

Roots

  • Guest
Re: Absolute position of a texture
« Reply #2 on: November 24, 2013, 01:37:04 AM »

Like this below?

transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(UICamera.lastTouchPosition))

Not seeing the expected result, i am getting the localCorners part, that seems to work fine but i think I am doing something wrong above?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Absolute position of a texture
« Reply #3 on: November 24, 2013, 01:40:34 AM »
Camera.main is your game camera. You need to use the UI camera.