Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Shifty Geezer on December 28, 2016, 04:22:53 PM

Title: Position widget at screen position
Post by: Shifty Geezer on December 28, 2016, 04:22:53 PM
Given screen coordinates from (0,0) to (1,1), how does one set the position of a widget? In the editor I can set transform's position to +/- UI dimensions/2, but setting widget.transform.position or localPosition either set the transform a tiny amount or a huge amount.
Title: Re: Position widget at screen position
Post by: Shifty Geezer on December 28, 2016, 04:27:01 PM
Note the intention is to use the widget's pivot, so two sprites set to position (0.25,0.25) only with one's widget set Left aligned and the other's Right aligned would both be positioned side-by-side at (240,135) in UI space in a 1920x1080 UI.
Title: Re: Position widget at screen position
Post by: devomage on December 28, 2016, 08:06:43 PM
take a look at:

NGUITools.FitOnScreen
Title: Re: Position widget at screen position
Post by: ArenMook on December 31, 2016, 07:36:19 AM
This isn't actually an NGUI question, but a Unity / math question. You need to use math to convert from one transform's space to another. Look at NGUITools.OverlayPosition for an example.
Title: Re: Position widget at screen position
Post by: Shifty Geezer on January 01, 2017, 08:16:53 AM
I replied to this but it hasn't appeared. NGUITools.FitOnScreen didn't work because it clamps to the borders (designed for tooltips) but it showed that NGUI just uses localPosition, leading me to investigate the values I was passing.

Turns out you can just set the transform.localPosition to NGUI screen coordinates and it works. It's simple enough to get the UI dimensions and multiply by whatever screen coordinate system you want to use, whether 0 to 1 or -0.5 to 0.5 or whatever.