Author Topic: Screen coordinates of UISprite  (Read 14530 times)

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Screen coordinates of UISprite
« on: December 09, 2013, 11:28:37 AM »
Hello there!

First of all, NGUI is a fantastic package. I am just getting started with it but it saved me a lot of time already and is worth every dollar.
I ran into a bit of a problem though and would like to get some feedback on the issue.

I have relatively normal setup with a UISprite (UI Root(2D)/Camera/Anchor/Panel/Sensor_1), with "Sensor_1" being a sprite I would like to move on the screen depending on mouse/touch input. So far so good, but I somehow can't sort the coordinates out. I have a valid position for the mouse/finger movement in proper screen coordinates (0,0 for bottom left,1280, 720 for top right, etc.) but I have failed to set the sprite to the according position so far.

I use
  1. Sensor1.GetComponent<UISprite>().transform.localPosition
to get the screen position of the sprite, but I what I get back is the position relative to the center of the panel my sprite is on. What I would need however is coordinates which match the screen coordinates. Example: My sprite is in the center of the screen at 1280*720 and I get back (0,0,0) as a position instead of (640,360,0) as I would expect. I am not sure what the right approach at this point would be.
While I would prefer a non-hacky solution that doesn't require manually translating the coordinates every time, any suggestions are appreciated :)


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Screen coordinates of UISprite
« Reply #1 on: December 09, 2013, 02:38:51 PM »
If your UIRoot is pixel-perfect, then your sprites will work with pixel coordinates. If your UIRoot is anything else, then they will be in virtual coordinates, based on the specified height value.

Center of the screen is (0, 0), and it extends outwards from there.

I recommend transforming coordinates. transform.position gives you world coordinates. Use camera.WorldToScreenPoint to convert to screen. Same goes for vice versa -- camera.ScreenToWorldPoint, transform.InverseTransformPoint to make it local, position the widget.

It's just math, and not NGUI-specific.

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Re: Screen coordinates of UISprite
« Reply #2 on: December 10, 2013, 06:02:39 PM »
Thank you for the explanation. The 'WorldToScreenPoint' and other similar functions are extremely useful. Sorry I didn't see them before. They solved the problem.
I am using something like:
  1. Sensor1.transform.position = UICamera.currentCamera.ScreenToWorldPoint(dragInfo.pos);
and it works great. There is one issue with it however, which I don't really understand: It works well if I use that code after I have been in the game for a moment. However if I try to position some sprites in the Start() function of my script using UICamera.currentCamera it seems to be null.

I am not sure if this any mistake in my setup or if the camera is simply not ready when the Start() event is called?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Screen coordinates of UISprite
« Reply #3 on: December 10, 2013, 09:28:31 PM »
UICamera.current series of properties (and indeed everything in NGUI saying 'current') implies they are only valid during a specific action. In this case -- NGUI events. You can use NGUITools.FindCameraForLayer if you need it, or just reference the camera on the script to start with.

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Re: Screen coordinates of UISprite
« Reply #4 on: December 12, 2013, 09:11:59 PM »
It all works beautifully now.
Thanks for the excellent support!