Author Topic: Is there a way to get the Screen Space coordinates of any given widget?  (Read 12176 times)

quentinp

  • Guest
Is there a way to get the Screen Space coordinates of any given widget?  Maybe this is obvious, but I can't seem to figure it out.  I've been trying to search for this all morning!  Basically I'm using Vectrosity to draw lines, I have my NGUI setup, and I want to draw a line (in screen space, not messing with 3D lines right now) from a give NGUI Widget to a point on the screen matching up with a game object (the second point I can get easy enough).


loopyllama

  • Guest
remember they are just game objects so you can use gameObject.transform.position or you can get fancy and use the Renderer.bounds.center. Anyways from there you would need to use Camera.WorldToScreenPoint, making careful note that z needs to be the distance between the object and the camera, like Mathf.Abs(camT.position.z - targetT.position.z). For line drawing in the editor (not game view) you can use Debug.DrawLine.
Good luck!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Generally when working with NGUI you're always in screen coordinates, so you should just be able to use the localPosition.

quentinp

  • Guest
My issue was coming from the complexity of my camera setup, here's the basic solution - basically had to get the screen coordinate of the widget projected to the gui camera.  Then I had to get the screen coordinate of the part projected to the player camera.  Then I could create a Vectrosity line (need to record the lines to alter them later, but for now it does what I wanted).  But another issue with Vectrosity is that you need to make sure that its camera depth is less then the NGUI camera depth or it will blow away everything in the NGUI layer, no matter what I try.  Here's a bit of rough code to generally get what I needed:

            var partCam:Camera=master.camera;
            var guiCam:Camera=gameObject.GetComponentInChildren(Camera);
            var guiTransform:Transform=wwi.GetComponentInChildren(WeaponMenuWidget).background.transform;
            var p1_3d:Vector3=guiCam.WorldToScreenPoint(guiTransform.position);
            var partTransform:Transform=child;
            var p2_3d:Vector3=partCam.WorldToScreenPoint(partTransform.position);
            var p1_2d:Vector2=new Vector2(p1_3d.x,p1_3d.y);
            var p2_2d:Vector2=new Vector2(p2_3d.x,p2_3d.y);
            VectorLine.SetLine(Color.green,p1_2d,p2_2d);            
            var vlcam:Camera=VectorLine.GetCamera();
            vlcam.depth=40;


Note I've hard coded the depth - i'll probably just change it to be the ngui camera depth minus one. 

Here's what I was trying to do - get the green lines from the weapon selection boxes to point to the parts - not perfect yet - I need heads and tails on the line, but I'm on the way again!



Thanks for the suggestions!
« Last Edit: May 16, 2012, 03:15:50 PM by quentinp »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Yeah, I can't read that. Javascript. ick.

quentinp

  • Guest
Yeah, I can't read that. Javascript. ick.

I feel shame... hehe, my next project will probably be in C#.  Marker lines like that would be an awesome addition to NGUI!