Author Topic: How to implement floating hud controls in NGUI ?  (Read 4756 times)

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
How to implement floating hud controls in NGUI ?
« on: June 15, 2014, 12:14:34 PM »
Hi, i want to make a heath bar in 3D game look like this http://www.daikonforge.com/dfgui/examples/floating-hud-controls/ in Daikon UI. I'm newbie with Unity and NGUI so anyone know how to do that ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to implement floating hud controls in NGUI ?
« Reply #1 on: June 16, 2014, 07:03:45 AM »
Easiest way -- just anchor the UI element to the 3D object. You will generally want to organize your UI element like this:

Widget (anchored)
- Slider for HP
- Label for the name

Alternatively you'd write a script to convert from world to screen point, or just use the HUDText plugin if you have that.

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: How to implement floating hud controls in NGUI ?
« Reply #2 on: June 16, 2014, 10:27:45 AM »
Thank you for support, i don't have HUDText plugin so may you show me an example how to convert from world to screen point :) It's very useful for me

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to implement floating hud controls in NGUI ?
« Reply #3 on: June 17, 2014, 10:46:16 AM »
// Take the 3D point and convert it to screen
Vector3 pos = Camera.main.WorldToScreenPoint(yourObject.transform.position);

// Now take this world point and make it relative to the widget you want to reposition
pos = yourWidget.transform.parent.transform.InverseTransformPoint(pos);

// Now position your widget
yourWidget.transform.localPosition = pos;