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
1
Question by Tomasay · Feb 29, 2016 at 04:03 PM · uitextcolor changeprintprinting

print text one character at a time with rich text

So i'm making a pretty simple text adventure game and when I output the text, I want it to print out one character at a time, like it's being typed. I managed to figure that out but the only problem is I'm using rich to text to change the color and italicize a lot of the text. So the text I'm printing out looks like this:

 "<color=blue>Enter house?</color>"

And i'm using a co routine to print it out like this:

 IEnumerator printText(String txt)
  {
     for(int i=0; i<txt.Length; i++)
     {
        output.text = output.text + txt[i];
        yield return new WaitForSeconds(0.1f);
     }
  }

So when it prints out, the < color > stuff doesn't go away until the whole line is printed. Is there any way I can change the text color without this problem?

Comment
Add comment · Show 1
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 Magius96 · Feb 29, 2016 at 09:00 PM 0
Share

This gets tricky here.

So, you are fine with adding a single character at a time, however you don't want to do that with the tags. Ins$$anonymous$$d, you want to add the entire tag, start and end tag, all at once and keep track of your position within the tags. Since you could be working inside more than one tag at a time, you will want a Stack to track your tags. When you encounter a '

For example, using your string above, here is a second by second of what it would look like

 ""
 "<color=blue>E</color>"
 "<color=blue>En</color>"
 "<color=blue>Ent</color>"
 "<color=blue>Ente</color>"

I think you get the point here.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by majecty · Nov 14, 2018 at 02:10 PM

You can use this library. This works also for child tags.

https://github.com/majecty/Unity3dRichTextHelper

Here is an example.

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 using RichTextSubstringHelper;
 
 public class NewBehaviourScript : MonoBehaviour {
     [SerializeField]
     Text uiText;
     IEnumerator Start () {
         var firstText = "<color=blue>blah</color>x";
         for (int i = 0; i < firstText.RichTextLength(); i++)
         {
             yield return new WaitForSeconds(0.5f);
             uiText.text = firstText.RichTextSubString(i + 1);
         }
     }
 
 }

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
0

Answer by tomtominc · Mar 02, 2017 at 11:31 PM

Hey Tomasay,

Warning: Does not work with child tags.

I made a quick parsing function that will do what you're asking. It's not the best so anyone that wants to jump in and edit this script is more than welcome.

It basically wraps every character in the starting and ending tag so it creates more text than necessary. So if you had text like this:

 <color=red>Hello</color> everyone.
 it would turn into
 <color=red>H</color><color=red>e</color>... etc. 

Here's the script:

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DisplayDialog : MonoBehaviour
 {
     public float delayTime = 0.1f;
     public Text paragraph;
 
     public void SetText(string text)
     {
         StartCoroutine(SetTextRoutine(text));
     }
 
     public IEnumerator SetTextRoutine(string text)
     {
         // reset the paragraph text
         paragraph.text = string.Empty;
 
         // keep local start and end tag variables 
         string startTag = string.Empty;
         string endTag = string.Empty;
 
         for (int i = 0; i < text.Length; i++)
         {
             char c = text[i];
 
             // check to see if we're starting a tag
             if (c == '<')
             {
                 // make sure we don't already have a starting tag
                 // don't check for ending tag because we set these variables at the 
                 // same time
                 if (string.IsNullOrEmpty(startTag))
                 {
                     // store the current index 
                     int currentIndex = i;
 
                     for (int j = currentIndex; j < text.Length; j++)
                     {
                         // add to our starting tag
                         startTag += text[j].ToString();
 
                         // check to see if we're going to end the tag
                         if (text[j] == '>')
                         {
                             // set our current index to the end of the tag
                             currentIndex = j;
                             // set our letter starting point to the current index (when we continue this will be currentIndex++)
                             i = currentIndex;
 
                             // find the end tag that goes with this tag
                             for (int k = currentIndex; k < text.Length; k++)
                             {
                                 char next = text[k];
 
                                 // check to see if we've reached our end tags start point
                                 if (next == '<')
                                     break;
 
                                 // if we have not increment currentindex
                                 currentIndex++;
                             }
                             break;
                         }
                     }
 
                     // we start at current index since this is where our ending tag starts
                     for (int j = currentIndex; j < text.Length; j++)
                     {
                         // add to the ending tag
                         endTag += text[j].ToString();
 
                         // once the ending tag is finished we break out
                         if (text[j] == '>')
                         {
                             break;
                         }
                     }
                 }
                 else
                 {
                     // go through the text and move past the ending tag
                     for (int j = i; j < text.Length; j++)
                     {
                         if (text[j] == '>')
                         {
                             // set i = j so we can start at the position of the next letter
                             i = j;
                             break;
                         }
                     }
                     // we reset our starting and ending tag
                     startTag = string.Empty;
                     endTag = string.Empty;
                 }
 
                 // continue to get the next character in the sequence
                 continue;
 
             }
 
             paragraph.text += string.Format("{0}{1}{2}", startTag, c, endTag);
 
             yield return new WaitForSeconds(delayTime);
         }
     }
 }




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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

UI TEXT not showing up in game mode 1 Answer

GameObject destroyed when changing scene 1 Answer

I can't change Text color 3 Answers

How do I let users change the background color of a selected word in an input field? 0 Answers

UI text is very blurry, and i dont know how to fix this. 1 Answer


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