Author Topic: How to set absolute widget position?  (Read 4158 times)

Evil-Dog

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 22
    • View Profile
How to set absolute widget position?
« on: July 31, 2014, 12:20:14 AM »
I'm confused on how to set the position on my widget.  Basically I've got a UIGrid with items in a UIPanel, I've also got some kind of "highlight" widget that I wanna place over the clicked item in the grid.  Now my efforts have been failures so far to set the highlight's position to the clicked item's position, I read I can't just set transform.position that I must use localPosition, but they're in different place in the hierarchy, what's the trick to convert one localPosition to another localPosition somewhere else in the hierarchy, taking into account the hierarchy's anchor points and the different scales on the uiroot and the panels?  Seems over complicated for nothing.
Thanks for any help

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to set absolute widget position?
« Reply #1 on: July 31, 2014, 11:05:18 AM »
You can use transform.position, provided the two items are visible by the same camera. Otherwise it's complicated -- you need to take the local position, turn it into world position, convert that into screen or view coordinates using the camera that draws the object, then use the other camera to convert that screen position into world position, then inverse transform world to local, relative to the widget's parent.

NGUIMath.OverlayPosition does all this math for you btw.

Evil-Dog

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: How to set absolute widget position?
« Reply #2 on: July 31, 2014, 12:37:06 PM »
Hi thanks!
I can't find OverlayPosition, by any chance, do you mean ScreenToPixels and ScreenToParentPixels? That seems potentially what I need.
I've tried it but still get offset results, closer but still wrong.  I've tried bringing the clicked widget from world to screen using the UICamera.currentCamera and then use ScreenToParentPixels to set the localPosition of my highlight widget.  I also tried ScreenToPixels but yeah, it's close but wrong. Is that the right way to do it?

When I originally used transform.position, the result was my widget at like x:16234, y:12872 or whatever, way way off.  The clicked widget is part of the UIRoot so it's seen by the UICamera and the highlight widget is also part of UIRoot so I think I'd need more than that, maybe take into account the recursive scale of the hierarchy? cause UIRoot has a scale of 0.002604167 and the widget containing the highlight widget also has a non-uniform scale.

My confusion remains :)


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to set absolute widget position?
« Reply #3 on: July 31, 2014, 12:43:24 PM »
If you can't find the functions I talked about then you need to update your NGUI. They were added maybe 2 months ago.

Evil-Dog

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: How to set absolute widget position?
« Reply #4 on: July 31, 2014, 01:11:06 PM »
Thank you, I updated NGUI and OverlayPosition works great!