Author Topic: How to get UI coordinate in WorldSpace?  (Read 8699 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
How to get UI coordinate in WorldSpace?
« on: September 07, 2016, 03:00:55 PM »
I'm wanting to measure the worldspace distance between a transform and a dragged NGUI widget.

  1.         void OnDragStart(){
  2.                 if(CanBeDragged){
  3.                         dragPos = transform.localPosition;
  4.                         isDragging = true;
  5.                 }
  6.         }
  7.  
  8.         void OnDrag (Vector2 delta){
  9.                 if(isDragging){
  10.                         dragPos += (Vector3)(delta*uiRoot.pixelSizeAdjustment);
  11.                         dragWorldPos = Camera.main.ScreenToWorldPoint (dragPos);
  12.                         print ("dragpos "+dragPos+"  worldposs  "+dragWorldPos+"    transpos "+targetTrans.position+"   mag "+(targetTrans.position - dragWorldPos).sqrMagnitude);
  13.                 }
  14.         }
  15.  
sqrMagnitude should decrease towards zero as the widget is dragged over towards the target object, but there's an offset. dragWorldPos isn't the correct world coordinates.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: How to get UI coordinate in WorldSpace?
« Reply #1 on: September 07, 2016, 04:37:24 PM »
try using something like this:
(i use this to calculate the distance of a row in a grid/table)

  1. Vector2 size = NGUIMath.CalculateRelativeWidgetBounds(t, true).size;
  2.  
  3. Vector3 point = scrollview.transform.InverseTransformPoint(t.FindChild("Sprite").position);
  4.  
  5. float amount = Mathf.Round(point.y - scrollview.panel.localCorners[0].y - size.y - table.padding.y * 2);
  6.  

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: How to get UI coordinate in WorldSpace?
« Reply #2 on: September 08, 2016, 03:06:47 AM »
Isn't that working in screen space coordinates?

My specific requirement is to see if a drag is within range (world space) of a gameObject's skill, and to cap the distance from the gameObject of the widget based on skill range.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get UI coordinate in WorldSpace?
« Reply #3 on: September 09, 2016, 02:35:33 PM »
Check UIScrollView.Drag(). A plane is created, a ray is cast into it using the screen point's position. This gets you a 3D position on the plane of the widget.