Author Topic: Different results of WorldToScreenPoint in different aspet ratio in Unity Editor  (Read 13449 times)

Brady Ji

  • Guest
Hi all,

I try to obtain the screen position of a UIbutton. But I get ALL DIFFERENT results by using the

WorkingCam.WorldToScreenPoint(obj.transform.positi on) // obj is the button. WorkingCam is the 2D camera.

in different resolutions output, runing in Android phone, such as 480x320, 800x480. (Android system..) I wanna know why ?

Some results are negative in its x component when I minus its 0.5*width to calculate its left-top position, although it does show and locate on the screen, which must not be negative.

It led to some errors during calculating touching area in some resolutions.
But in this situation, it seems only the 480x320 worked right. I have no idea about it.

In general, no matter what the resolution is , the screen positions of this button should remain the same , right?

What's the possible reason that might lead to this situation ?

thanks for every help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Why would it remain the same if the resolution changes? Resolution changes = screen size changes = position changes.

Brady Ji

  • Guest
Why would it remain the same if the resolution changes? Resolution changes = screen size changes = position changes.

Because the button is anchorred at left side and even though the position is changed, why could the left-top position be negative ?

I mean I use WorldToScreenPoint to calculate its screen position (s1) and I substract s1 with half of it's width to calculate its top-left position. I found the x position of the result is negative. I dont know why ?!

Because the position of the button should be in the middle of the button image and I need to subtract half of its width to calculate its top/left position, most of all , I do see the button is on the screen , so the position of the button must not be negative.

Please clarfy my question, thanks a lot.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
The origin point for the camera is the center. Origin for screen coordinates is top left. Origin for graphics is bottom left. This is further complicated by pixel-perfect vs fixed size UIRoot. Confused yet? Never assume anything. ;)

Brady Ji

  • Guest
The origin point for the camera is the center. Origin for screen coordinates is top left. Origin for graphics is bottom left. This is further complicated by pixel-perfect vs fixed size UIRoot. Confused yet? Never assume anything. ;)

Hi,

I knew the origin of screen coordinate is top left. And for GUI, the origin is botton left. But I don't know about the origin of camera which is the center. what doest it use for ?

In fact, the WorldToScreenPoint is to calculate its screen coordinate, right? And I subtract it with half of its width, which I get by "width = obj.GetComponent<BoxCollider>().size.x". All this operaiton should not be led to negative result, right?

But the resulting x component of the result position is negative.  That's problem and I am now confused about.

Thanks for your great help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Camera = view space. View space matrix is generally -1 to 1 range, although I think Unity may have it be 0 to 1 instead.

WorldToScreenPoint takes a transform.position and converts that to screen coordinates, which are 0 to Screen.width and 0 to Screen.height. The UI may or may not be in screen coordinates -- it's determined by how you have your UIRoot set up. It's only in screen coordinates if the UIRoot is set to be pixel-perfect.

Brady Ji

  • Guest
Camera = view space. View space matrix is generally -1 to 1 range, although I think Unity may have it be 0 to 1 instead.

WorldToScreenPoint takes a transform.position and converts that to screen coordinates, which are 0 to Screen.width and 0 to Screen.height. The UI may or may not be in screen coordinates -- it's determined by how you have your UIRoot set up. It's only in screen coordinates if the UIRoot is set to be pixel-perfect.

Thanks. I understood it's a pixel-perfect situatiaon.

But how can I caculate the UI-object's width under the non-pixel-perfect situation?  for example, I have a UI which has a collider with size 128*128. How can I calculate size of the object in pixel ? Which I need to know to calculate the upper-left position of the UI in the screen.

Do you have any suggestions? Thanks for everyone.

By the way, if I can know the 3D position of the upper-left side of the UI, I can use 3D-2D mapping to get the screen coordinate too. So I need to multiply the size(128x128) with the world scale of the UI and to get the 3D location of the upper-left side of the UI. But I try to get the lossyScale of the NGUI Button and the value is 0. Why ?

Thanks again.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
UIRoot sets up virtual pixels based on:

1 / virtualHeight = root.localScale(x,y,z)

You can read the actual screen size in Screen.height and compare that with the value UIRoot has calculated, to figure out your widget's actual pixel size on the screen.

Brady Ji

  • Guest
UIRoot sets up virtual pixels based on:

1 / virtualHeight = root.localScale(x,y,z)

You can read the actual screen size in Screen.height and compare that with the value UIRoot has calculated, to figure out your widget's actual pixel size on the screen.

Hi,

I try to calculate the value of the NGUI Button : transform.root.localScale and the value is still be (0,0,0). why?

I am confused about the function of Manual Height of the UIRoot object and why does the scale is always be 1/(0.5*MaunalHeight)?

What's the relationship between this ManualHeight and the aspect ratio setting in Game View, such as 480*320 ?

And btw could you explain the equation more detailed?

Thanks a lot.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
The scaling of UIRoot is there to keep the size of the UI manageable in the world. Without it, everything will be huge. It's not mandatory, but it does make it easier.

I suggest you take a look at the tooltip script. It resizes the background based on the widget's dimensions. Popup list does the same thing -- resizes the background to envelop the contents.

Both use NGUIMath.Calculate series of functions.