Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GUI element attached to a GameObject

Discussion in 'UGUI & TextMesh Pro' started by InsaneGoblin, Jan 6, 2016.

  1. InsaneGoblin

    InsaneGoblin

    Joined:
    Jun 2, 2013
    Posts:
    239
    Hi there!

    I have seen this question asked many times, unfortunately I am unable to find conclusive answers.

    All I am looking for is to have a uGUI element following a GameObject. Example: a Text element on top of an enemy character prefab. The enemy moves, the text above his head follows it. He leaves view of the camera, the text is disabled. Has to be done with uGUI, not the old GUI or nGUI.

    Seems super basic, and yet I am unable to find out how. Help?

    Thanks!
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Well there are two approaches really.
    First is to attach a world space canvas to the 3D GO and put your UI on it. Making sure to rotate the canvas to face the camera! (look on Will G's post about the UI for a good example of this on the Unity blog)
    Alternatively, have a ScreenSpace canvas, work out the screen position of the 3D object (WorldtoScreen conversion with the camera), then place the UI on the canvas relative to that point, making sure to adjust for scale based on how far away the object is from the camera.

    The second is more performant but a bit more work.
     
  3. InsaneGoblin

    InsaneGoblin

    Joined:
    Jun 2, 2013
    Posts:
    239
    This is not possible, due to the sheer amount of canvases I would need to make (dozens if not hundreds) for all the enemies on the scene

    This is that I was researching. Would you be so kind to provide a guide on how to transform the different coordinates and have it "follow" the GO?

    Thanks!
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    I'll see if I can whip up a quick sample for you, might take a few days.
    I know @Adam Buckner has a working example as shown at the end of one of his awesome live learn sessions. Any thoughts yet Adam to make you example public?
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Agh! Help me remember which this was? If I did it as a live session, it should be here somewhere:
    http://unity3d.com/learn/live-training/
     
  6. InsaneGoblin

    InsaneGoblin

    Joined:
    Jun 2, 2013
    Posts:
    239
    Skimped over all Adam's UI tutorials, but cannot find it (there are several). Maybe it's the one relative to the star names? Need it billboarded as well, if possible...

    Would love some directions :)
     
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    InsaneGoblin: Camera has method called WorldToScreenPoint, with that you can observe any objects position and use that for UI canvas element's position value.
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Riiiiight. You mean my star map?


    I remember talking about it now - it'll be in Q&A at the end of one of the tutorials, and it not necessarily related to the actual content...

    Let me see if I can find anything relevant that could help.

    (FWIW, that project [now idle] was from the Beta of Elite Dangerous while their internal map was still being developed and an additional tool was needed.)
     
  9. InsaneGoblin

    InsaneGoblin

    Joined:
    Jun 2, 2013
    Posts:
    239
    Yup, that's the one. Unless you have another way, or better explanation. I'm all ears :)
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The meat of this, and it's hard to extract (as it's not running in 5.3 due to updates to the www class that I don't have time to fix) is this code:

    Code (csharp):
    1.  
    2.         foreach (Star star in viewableStars) {
    3.             star.label.transform.position = mainCamera.WorldToScreenPoint (star.coordinates) + labelOffset;;
    4.             float labelAlpha = SetLabelAlpha (star.coordinates);
    5.             Color labelColor = new Color (star.label.color.r, star.label.color.g, star.label.color.b, labelAlpha);
    6.             star.label.color = labelColor;
    7.         }
    Essentially, stripping it all back, this is what I do with the star labels. The stars are simply Vector3 positions. They are displayed with particles, all from one particle system. Then there are auto-generated pooled labels that are positioned on screen using the above code.
     
    qcw27710 and SimonDarksideJ like this.
  11. InsaneGoblin

    InsaneGoblin

    Joined:
    Jun 2, 2013
    Posts:
    239
    Ok, so confirmed that WorldToScreenPoint is the best way to go.

    Fine, this will do. Many thanks for your help, guys!
     
  12. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    That's the one @Adam Buckner , was mighty impressed when you went off piste and showed that galaxy map scenario.

    Would be another good live session walking through that example and a few other UI layout examples :D
     
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Heh - I'm working thru animating 25 buttons simultaneously using the Animator and getting my state machines correct... maybe I'll do some more UI sessions... I like them. I just need to learn more.
     
    qcw27710 and SimonDarksideJ like this.
  14. InsaneGoblin

    InsaneGoblin

    Joined:
    Jun 2, 2013
    Posts:
    239
    We'll be here for you!
     
  15. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Lol, don't we all. Need to clear plate of other tasks first :D
     
  16. emanuel_rc

    emanuel_rc

    Joined:
    Feb 21, 2016
    Posts:
    2
    This is awesome, i'm currently trying to make a starmap like this, but it has around 30k stars on scene, so i wonder, how do you make it so that only the closest stars show the name?, thank you very much
     
  17. JacksonGordon

    JacksonGordon

    Joined:
    Jan 10, 2019
    Posts:
    1
    You could sort them by distance from camera first and then draw the first 50 or however many.
     
  18. InsaneGoblin

    InsaneGoblin

    Joined:
    Jun 2, 2013
    Posts:
    239
    2.5 years necro :D

    I'm sure he solved the issue long ago
     
  19. FBones

    FBones

    Joined:
    Aug 28, 2016
    Posts:
    73
    I know this an old thread, but I think this method is still legit, and I had a question about it.
    In the second case (where you are using WorldtoScreen), are you updating this value every frame based on where the GO is? The GO in this case is moving around the screen, so I would assume you would have some CanvasManager script that has to explicitly reposition the elements dynamically at each frame, right?
    Or am I missing something?