Author Topic: Draw line to/from UISprite  (Read 6019 times)

TieKei

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Draw line to/from UISprite
« on: October 31, 2015, 03:55:21 PM »
Hey there,

I'm new to NGUI, but I did some hours of research before posting.
My main goal is to draw a line to/from an UISprite, so I placed an UISprite and I'm able to draw Lines to Unity's GUI Layer via DrawLine Script (http://wiki.unity3d.com/index.php?title=DrawLine).

Now my problem is to find the exact screen-position of my UISprite to find the perfect startpoint for my drawLine.

My current approach:
  1. // JS
  2. var my_sprite:GameObject = NGUITools.AddChild(GameObject.Find("UI Root"), prefab_sprite); //adds a sprite from prefab
  3. var ui_sprite:UISprite = gesture.GetComponent.<UISprite>();
  4. var ui_camera = GameObject.Find("UI Camera").GetComponent.<Camera>()
  5.    
  6. ui_sprite.SetAnchor(target.transform); // target is a GameObject
  7. ui_sprite.leftAnchor.absolute = -50; // wanted a 100 px sprite, centered on target
  8. ui_sprite.rightAnchor.absolute = 50;
  9. ui_sprite.ResetAnchors();
  10.  
  11.  
  12. var ngui_pos:Vector3 = ui_camera.WorldToScreenPoint( ui_sprite.get_worldCenter() );
  13.  
  14. var screen_position:Vector2 = Vector2( parseInt(pos.x), Screen.height - parseInt(pos.y) );
  15.  

I'm pretty sure there is a better way, but I wasn't able to find one.
At least it works for the y coordinate for every resolution I tested. x is always wrong though.

Thanks,
Tim

TieKei

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Draw line to/from UISprite
« Reply #1 on: November 01, 2015, 08:15:06 AM »
so I found a working solution by ignoring the UISprite position completely and reverse engineer it's position by calculating it's anchor-target's position to screen-points and go from there.

  1. var p = Main.camera.WorldToScreenPoint( target.transform.position );
  2. var screen_position = Vector2( parseInt(p.x), Screen.height -parseInt(p.y))
  3.  

Now I'm able to draw a line to/from the center of my UISprite.

I really don't understand why this has to be so ugly and complicated in Unity...
WorldToScreenPoint seems to return the correct x position (but as a float, which result in really strange positions if you don't parse it to Int before applying it to a Vector2). The y position however seems to be measured from the screens bottom (why would you even...)

Edit: my UI Camera messed up the whole process, with Main.camera it's working somewhat better, so the following rant is not appropriate anymore :)
but not enough, also it seems to be off by about Screen.height * 0.05 (this was the moment where I seriously thought about getting into wood-carving and never touch something awful like this again  :o)
« Last Edit: November 01, 2015, 08:29:28 AM by TieKei »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Draw line to/from UISprite
« Reply #2 on: November 04, 2015, 12:35:46 PM »
Position in graphics is always based from the bottom-left corner. Think about it like drawing a graph... X and Y position -- both X and Y increase from 0.