Author Topic: Having UILabel Follow Object in Scene  (Read 4154 times)

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Having UILabel Follow Object in Scene
« on: April 11, 2014, 11:06:38 PM »
Hey guys,

I am trying to have a UILabal move and essentially follow an object in the scene.
I tried doing WorldSpaceToViewport and ToScreen on NGUI's camera and moving the object with MoveTowards. But the results are that it shoots off to the side and isnt sitting on top of the object. The only way I found is to disable NGUI's camera and use my regular Main Camera, but this seems moot.

Any suggestions will be very appreciated!

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Having UILabel Follow Object in Scene
« Reply #1 on: April 12, 2014, 04:50:44 AM »
Simply anchoring the widget to a 3D object will make it follow it. Check the UIRect's documentation page.

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Having UILabel Follow Object in Scene
« Reply #2 on: April 12, 2014, 07:06:41 AM »
Hey Aren. Thanks for your helpful reply.

What I would really like to do is have a UILabal spawn on a world space object and in the camera
space move to the score one the bottom right, as in this image. What I tried to do is
set the from position by taking the vector 3 of the world space object and transforming it
into screen space and viewport space but to no avail. Any ideas on how I could accomplish this?



Thanks a bunch!
« Last Edit: April 12, 2014, 03:54:56 PM by maxdev »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Having UILabel Follow Object in Scene
« Reply #3 on: April 13, 2014, 12:36:43 AM »
That involves going from 3D to 2D coordinates, and is not something you can do without writing some code. You will need to take the target's 3D world position, convert it to view space using the camera that draws it, then using the UI camera convert that back to world space (this time it will be in UI world coordinates). You will then need to convert this value to virtual pixels by using transform.InverseTransformPoint, where 'transform' is whatever object you want it to be relative to, such as the parent of the sprite you want to move. Now that that's done you have local coordinates to work with which are in virtual pixels which will make sense. You can now proceed to tween the object from this position to somewhere else, like the corner of the screen.

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Having UILabel Follow Object in Scene
« Reply #4 on: April 17, 2014, 11:38:36 AM »
Thanks a bunch Aren, that did the trick. I am wondering though, as I am using this for a combo system and at times on several objects per frame, is it too resource intensive for mobiles? Is there
a more recommended method?

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Having UILabel Follow Object in Scene
« Reply #5 on: April 17, 2014, 01:58:14 PM »
I doubt this will have much of an effect on performance. I wouldn't worry about it until it shows up in the profiler.