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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GUI label and wordwrap

Discussion in 'Scripting' started by Panchos, Aug 4, 2015.

  1. Panchos

    Panchos

    Joined:
    Oct 31, 2012
    Posts:
    34
    I'm making speech boxes in my game for NPCs but having a few issues.

    I'm a unity rookie so dunno if this is the best way of doing it, but I create two GUI labels, one for the actual speech and one for the shadow underneath it (just black). I want the position of the speech box in the center of the NPC, which is why guiPosition is halved.

    Here is my code:
    Code (csharp):
    1. //get size of text so we know shadow box size
    2. Vector2 size = SpeechBox.CalcSize (new GUIContent (content));
    3.  
    4. //some code to set position of box above center of NPC
    5. var guiPosition = Camera.main.WorldToScreenPoint (new Vector3 (transform.position.x, transform.position.y + 0.15f, 0));
    6. guiPosition.y = Screen.height - guiPosition.y;
    7. guiPosition.x = guiPosition.x - (size.x / 2f);
    8. guiPosition.y = guiPosition.y - size.y;
    9.          
    10. //draw boxes
    11. GUI.Label (new Rect (guiPosition.x + 3f, guiPosition.y + 3f, size.x, size.y), "", black);
    12. GUI.Label (new Rect (guiPosition.x, guiPosition.y, size.x/2, size.y), content, SpeechBox);
    13.  
    The issue I'm having is when I try and get word wrap added to it. I don't want the text to go over say.. 300 pixels, and if it does it needs to move to the next line. I was thinking I could just add a min(size, 300) kind of thing to the code, but then the calcsize to get the shadow size wouldn't work would it?

    Just a little confused, am I in the right direction?
     
  2. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    sorry for not really answering your question... but why do you use the legacy UI system of Unity?
    It is not only hard to tame but also very imperformant.

    I would highly recommend to use the new UI system instead. With the new UI system you can achieve such an effect (text shadows) with 3 clicks and without coding - and you have a live preview.
     
  3. Panchos

    Panchos

    Joined:
    Oct 31, 2012
    Posts:
    34
    Someone in the chat pointed that out, and I re-coded it with new UI and sorted it, thanks