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 /
  • Help Room /
avatar image
0
Question by Brad-Lee · May 18, 2016 at 09:40 PM · uitextunity5formatting

Rich text in new UI?

Like say I want a word in my sentence to be bold, better yet, be able to switch that one word to be bold at run time, or highlight to a different color to show that this word has been spoken - much like Karaoke. How would I do this in the new UI system? I've searched around on this topic, but it doesn't seem to be officially addressed anywhere.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ahndrakhul · May 19, 2016 at 07:18 AM

I hope this helps. This works like your Karaoke example. It will temporarily "bold" and change the color of each word in a string from beginning to end with some delay between each word. It works by using StringBuilder.Insert to insert rich text opening tags before the word and rich text closing tags after the word. After this is done, the UI text is updated by setting it equal to the StringBuilder.ToString(). The tags are then removed using StringBuilder.Remove in preperation for the next word.

I don't know if this is the official way of doing this sort of thing, but it does seem to work. It wouldn't be too hard to use StringBuilder.Replace to make it work on specific words etc..

 sBuilder.Replace("words", "<b><color=red>words</color></b>");

Unity Rich Text Manual Page

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Text;
 
 public class RTTest : MonoBehaviour
 {
     string boldText = "Those are some bold words!";
     private Text textToDisplay;
 
     void Start()
     {
         textToDisplay = GameObject.Find("RichTextTest").GetComponent<Text>();
         StartCoroutine(BoldWords(boldText));
     }
 
     IEnumerator BoldWords(string textToBold)
     {
         string rtOpenTag = "<b><color=green>";   //Rich text opening tag
         string rtCloseTag = "</color></b>"; //Rich text closing tag
         int wordStartIndex = 0;       //Start index of a word
         int wordEndIndex = 0;         //End Index of a word
         float delay = 1.0f;
         StringBuilder sBuilder = new StringBuilder(textToBold);
 
         while (wordEndIndex < textToBold.Length)
         {            
             wordStartIndex = GetNextStartIndex(textToBold, wordEndIndex);    
             wordEndIndex = GetNextEndIndex(textToBold, wordStartIndex);
             sBuilder.Insert(wordStartIndex, rtOpenTag);                
             sBuilder.Insert(wordEndIndex + rtOpenTag.Length, rtCloseTag);
             textToDisplay.text = sBuilder.ToString();                  
             sBuilder.Remove(wordStartIndex, rtOpenTag.Length);            
             sBuilder.Remove(wordEndIndex, rtCloseTag.Length);                             
             yield return new WaitForSeconds(delay);                      
         }
         textToDisplay.text = sBuilder.ToString(); 
     }
 
     int GetNextStartIndex(string textToBold, int wordEndIndex)
     {
         int nextStartIndex = wordEndIndex;
 
         if (textToBold[nextStartIndex] == ' ')
         {
             while (textToBold[++nextStartIndex] == ' '){ }
         }
         return nextStartIndex;
     }
 
     int GetNextEndIndex(string textToBold, int wordStartIndex)
     {
         int nextEndIndex = wordStartIndex;
 
         while (++nextEndIndex < textToBold.Length && textToBold[nextEndIndex] != ' ') { }
         return nextEndIndex;
     }
 }


Comment
Add comment · Show 2 · 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 Brad-Lee · May 19, 2016 at 09:02 AM 0
Share

Wow, I really messed up reading the docs! I assumed the new UI text wouldn't support rich text because I couldn't find any mention of it.

Thanks for your examples and bringing it to my attention though! :)

avatar image Saikat_2016 Brad-Lee · Apr 22, 2017 at 02:10 PM 0
Share

$$anonymous$$y problem is that...when I touch on UI Text area a particular word is not selected...in Unity Android...what can I do for this situation...please help me....alt text

prob.png (48.6 kB)

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

80 People are following this question.

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

Related Questions

How to write actual text into game play 2 Answers

problem with Adjustable Character Spacing script in Unity 5.4 0 Answers

Unity 5 - Does anyone know how to scale the text in the UI? 1 Answer

Resizing font by scale according to screen size, (More complicated than you might think) 1 Answer

Marquee/scrolling text with Unity 4.6 UI 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