Author Topic: Position widget at screen position  (Read 2335 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Position widget at screen position
« 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.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Position widget at screen position
« Reply #1 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.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Position widget at screen position
« Reply #2 on: December 28, 2016, 08:06:43 PM »
take a look at:

NGUITools.FitOnScreen

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Position widget at screen position
« Reply #3 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.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Position widget at screen position
« Reply #4 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.