Author Topic: Real screen size to NGUI virtual screen size  (Read 5988 times)

mkgame

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Real screen size to NGUI virtual screen size
« on: November 14, 2016, 06:17:04 AM »
Hello,

similar questions asked a lot of time, but I failed with all the solutions. My problem is to map the Input.mousePosition to the NGUI virtual screen size. The screen is pixel perfect, which has no impact to this problem. UI camera ia perspective. The only one issue is, that Input.mousePosition has a screen width e.g. 1400 but NGUI has a virtual screen width of 1600. The height scaled with the same factor. I'am trying to place a UITexture to the mouse position, the cursor image at hovering over 3D objects. What am I ask for the virtual NGUI size, is there a method for the conversion? It seems to be that it is just a scaling issue, but where can I read the scaling value?

Thanks in advance for your help.

mkgame

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Real screen size to NGUI virtual screen size
« Reply #1 on: November 14, 2016, 06:55:52 AM »
If I change the UI camera transform.z value, then the virtual screen size changes... In my case, setting the camera range to the to -595 instead of -700 then match the real screen size in which the Input.mousePosition is. But if I do this, by the way it looks like a very hacky solution, then some of the UI components are outside the screen, or changes the position in the running sceeen view.

Is there a good way to handle this? I need the 3D (perpective) UI.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Real screen size to NGUI virtual screen size
« Reply #2 on: November 14, 2016, 06:57:58 AM »
Your problem stems from the fact that the mouse coordinates are specified as a 2D coordinate, but since your UI is 3D, you need a 3D position instead. Where is the depth going to come from? transform.OverlayPosition works great for positioning a 2D widget on top of any 2D or 3D object, but it's meant to position 2D elements, not 3D. Best I can suggest is maybe look at the old UIAnchor script -- it handles the math inside for 3D elements. It simply treats the widget's plane as the Z coordinate for the purpose of the math calculations.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Real screen size to NGUI virtual screen size
« Reply #3 on: November 14, 2016, 07:00:13 AM »
You can test the old UIAnchor script by making a new scene, switching the camera to be Perspective (and moving its Z back so that it can see the UI again), creating a widget and attaching the UIAnchor to it. Specify which side or corner to attach to on the UIAnchor -- you can now drag the camera back and forth on the Z, and the widget will stay glued to wherever you attached it to.

The math you're looking for is around line 207:
  1. v.z = uiCamera.WorldToScreenPoint(mTrans.position).z;
  2. v = uiCamera.ScreenToWorldPoint(v);
then again around 234:
  1. v = mTrans.parent.InverseTransformPoint(v);
  2. if (mTrans.localPosition != v) mTrans.localPosition = v;

mkgame

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Real screen size to NGUI virtual screen size
« Reply #4 on: November 14, 2016, 07:38:00 AM »
Thank you!

Finaly the solution is:

  1. public UITexture TestCursor;
  2. Camera uiCamera; // setting in Awake by NGUITools.FindCameraForLayer(gameObject.layer);
  3. ...
  4.  
  5. Vector3 v = Input.mousePosition;
  6. v.z = uiCamera.WorldToScreenPoint(TestCursor.transform.position).z;
  7. v = uiCamera.ScreenToWorldPoint(v);
  8.  
  9. TestCursor.transform.position = v;
  10.  

mkgame

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Real screen size to NGUI virtual screen size
« Reply #5 on: November 14, 2016, 05:29:04 PM »
Hello again,

I don't know why my test script worked before. After the real implementation it doesn't work anymore. Everything looks good, the UIPanel on root has exactly the size of the screen. Would be perfect for the mouse position. I don't know how to get a scale factor to fix the 10-20% difference between mousePosition and position of the UITexture.

If I change the height of the screen (of the visible running scene/game screen), then this difference will be higher/lower and can be also exact, if I change it to the one correct height. Somehow the height/width ratio changes the exactness of the screen position for my UITexture... I don't know how to get this factor, the UIRoot and UIPanel don't know this factor. Even if I change it in the editor mode, I see that the coordinates cannot be correct.

I would like to do not change the z value of UITexture position, because that would scale the texture.

Any suggestion, maybe a new NGUI root with 2D settings? OnGUI worked fine, but I would like to remove all the OnGUI methods.

mkgame

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Real screen size to NGUI virtual screen size
« Reply #6 on: November 14, 2016, 07:05:04 PM »
The only one solution I found is to add a new NGUI root, but with ortographic projection. Then I attached the cursor on this root, and now I can set the Input.mousePosition as it is. The 2D root has its own layer, else NGUI makes strange things ;) The 2D root UI camera just shows the 2D UI layer (Camera culling mask is UI2D not the default UI).
I hope the performance for the additionally added camera is nearly nothing, because it just shows up the own layer.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Real screen size to NGUI virtual screen size
« Reply #7 on: November 15, 2016, 09:05:38 AM »
The performance cost of having an extra camera that draws nothing is basically non-existent.