How to render underlined text in Unity?

Hi. I need to render TextMesh with underlined font. But i didn’t find anything for it in Unity. Also I can’t get any .ttf with underlined style embedded. I can’t even manually change generated font texture to add line under characters.

Also i didn’t find method to get TextMesh width for drawing line under it manually.

How can this problem be solved?

If you are using GUILayout, you can do it by fetching the last item’s rectangle:

        // This displays a button that looks like a label. ExpandWidth(false) ensures
        // that the underline will have the same size as the text.
        if (GUILayout.Button("Click me!", EditorStyles.label, GUILayout.ExpandWidth(false)))
        {
            Application.OpenURL("http://www.google.com");
        }

        // Get the last rect to display the line
        lastRect = GUILayoutUtility.GetLastRect();
        lastRect.y += lastRect.height - 2; // Vertical alignment of the underline
        lastRect.height = 2; // Thickness of the line

        GUI.Box(lastRect, "");

See GetLastRect

Well in support of the above answer . You can create Rect using same arguments like shown below and can put underscore in String .

          //Forgot Password button
	if(GUI.Button(Rect(x, y, width, height)),"Forgot Password",forgetButtonStyle){
		Application.OpenURL("http://answers.unity3d.com/questions/topics/underline.html");
	}
	
	//UnderLine Forgot Password button
	GUI.Label(Rect(x, y, width, height), "______________", forgetButtonStyle);

If you want to reduce the gap between text and underline , you need to reduce height of underline label.

Well, this is a hack, but you can always create a duplicate TextMesh consisting only of underscore characters (e.g. ___________ ).

If you need this functionality “TextMesh Pro” support the underlined tag so you can more easily manage underlined text.

Normal Underline text

28129-underline.jpg

In Unity 4.6. Duplicate the gameobject that has the text component (Ctrl+D). This will retain its positioning information. Then replace every character in the text field with a ‘_’.

You can automate this with the following Underline.cs component to add to any Unity 4.6 gameobject that has a Text component:

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class Underline : MonoBehaviour
{
    void Start()
    {
        var transform = base.transform as RectTransform;

        // make a stripped down copy of the Text
        var go = GameObject.Instantiate(transform, transform.position, transform.rotation) as Transform;
        foreach (var component in go.GetComponents<MonoBehaviour>())
        {
            if (component is Text)
                continue;
            else
                GameObject.Destroy(component);
        }
        go.SetParent(transform.parent);

        // make sure transform has original values
        go.localScale = transform.localScale;
        var TextComponent = go.GetComponent<Text>();
        TextComponent.rectTransform.sizeDelta = transform.sizeDelta;

        // use the copy to create an underline
        TextComponent.text = new System.Text.RegularExpressions.Regex(".").Replace(TextComponent.text, "_");
    }
}

Using a combination of OLP’s and ikn’s answers:

            if (GUILayout.Button(
                    "Do Stuff",
                    EditorStyles.label,
                    GUILayout.ExpandWidth(false)))
            {
               DoStuff();
            }
            var lastRect = GUILayoutUtility.GetLastRect();
            GUI.Label(lastRect, "___________");

If this doesn’t work for you try increasing and decreasing the number of underscores in the last label.