Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Division · Jun 22, 2011 at 07:24 AM · fonttextmesh

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
3

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HunterAugust · Jan 16, 2016 at 11:45 PM 0
Share

Necromancing the hell out of this post, But thank you for this. Such a simple way to handle this.

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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. ___ ).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

alt text


underline.jpg (48.6 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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, "_");
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SOLVED - 3D Text - How to apply new font type in Runtime 2 Answers

Custom Font works in editor but not in build 2 Answers

How to add font material to TextMesh in the code? 0 Answers

Create a 3d text using c# (font problem) 2 Answers

How do I make Unity to print accentuated letters? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges