- Home /
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?
Answer by OLP · Jul 14, 2014 at 10:40 PM
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
[1]: http://docs.unity3d.com/ScriptReference/GUILayoutUtility.GetLastRect.html
Necromancing the hell out of this post, But thank you for this. Such a simple way to handle this.
Answer by ikn · Apr 25, 2014 at 05:25 PM
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.
Answer by samuel-levine · Aug 02, 2013 at 02:36 PM
Well, this is a hack, but you can always create a duplicate TextMesh consisting only of underscore characters (e.g. ___ ).
Answer by sschaem · Jun 23, 2014 at 12:14 AM
If you need this functionality "TextMesh Pro" support the underlined tag so you can more easily manage underlined text.
Normal Underline text
Answer by ShawnFeatherly · May 30, 2015 at 12:42 AM
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, "_");
}
}