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.
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, "");
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, "_");
}
}