Tasharen Entertainment Forum
Support => NGUI 3 Support => Topic started by: Evil-Dog 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
-
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.
-
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 :)
-
If you can't find the functions I talked about then you need to update your NGUI. They were added maybe 2 months ago.
-
Thank you, I updated NGUI and OverlayPosition works great!