Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by HolBol · Mar 23, 2014 at 06:47 PM · texthtmlcolour

Typewriter with coloured text

I'm wondering whether it's possible to harness the rich text format options in conjunction with the AutoType script on the wiki (http://wiki.unity3d.com/index.php?title=AutoType).

The main problem I've found is as it splits the string into characters and types them out one-by-one, it would take the html tags for colouring with it and type those out, meaning you wouldn't get the colour until that section of the string is finished.

Here's the script, by the way:

 var letterPause = 0.2;
 var sound : AudioClip;
  
 private var word;
  
 function Start () {
     word = guiText.text;
     guiText.text = "";
     TypeText ();
 }
  
 function TypeText () {
     for (var letter in word.ToCharArray()) {
         guiText.text += letter;
         if (sound)
             audio.PlayOneShot (sound);
         yield WaitForSeconds (letterPause);
     }        
 }
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

3 Replies

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

Answer by robertbu · Mar 23, 2014 at 10:43 PM

There's no easy way to do it using rich text as input. You could scan for the color tags yourself and then output a color tag for each character. Or you can do it using a parallel data structure and output color tags for each character. Here is a simple example of a parallel data structure. Attach the following script to a GUIText object. Note that the string output is now in the script rather than taken from the GUI text:

 var letterPause = 0.2;
 var sound : AudioClip;
 
 public var text : String = "Here is some text";
 public var colors : Color[] = [Color.red, Color.white, Color.yellow, Color.black,
                                Color.blue, Color.cyan, Color.gray, Color.green, 
                                Color.magenta, Color.red, Color.red, Color.red, 
                                Color.green, Color.green, Color.green, Color.red,
                                Color.red];
  
 function Start () {
     TypeText ();
 }
  
 function TypeText () {
     var charArray = text.ToCharArray();
     for (var i = 0; i < charArray.Length; i++) {
     if (i <= colors.Length - 1) {
         guiText.text += "<color="+ColorToHexString(colors[i])+">"+charArray[i]+"</color>";
         Debug.Log(guiText.text);
         }
     else {
         guiText.text += charArray[i];
     }
     if (sound)
          audio.PlayOneShot (sound);
     yield WaitForSeconds (letterPause);
     }   
 }
 
 function ColorToHexString(color : Color) : String {
     var color32 : Color32 = color;
     return String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color32.r, color32.g, color32.b, color32.a);
 }

In order to keep it simple, I've established a color array that matches each character to a color. You can modify this to something that is more efficient. You could parse out the `` tags and do the same thing I've done here and output a separate color/end color tag set for each character.

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 HolBol · Nov 12, 2014 at 02:51 AM 0
Share

Hey, sorry to dig this up again, but I heard a way to do it was by positioning multiple GUI Labels next to each other. I'm wondering if you or anybody else would know how to do this in the way I've got with the original script and keeping wordwrap of some sort? I figured you could use .calcSize() to work out how long each word was for word wrapping but it's a case of getting that to work with each label printed next to each other.

avatar image HolBol · Mar 09, 2015 at 11:55 PM 0
Share

Though I've worked an alternate solution nearly a year on, I'm marking this as answered. Thanks for the help.

avatar image
0

Answer by GabLeRoux · Mar 03, 2019 at 01:12 AM

Here's a similar script as above, but in CSharp. Didn't do what I was looking for exactly, but it works.

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ColoredTypewriterText : MonoBehaviour
 {
     [SerializeField]
     float letterPause = 0.2f;
 
     [SerializeField]
     Text label;
 
     public string text = "0123456789";
     public Color[] colors = {
         Color.red,
         Color.white,
         Color.yellow,
         Color.black,
         Color.blue,
         Color.cyan,
         Color.gray,
         Color.green,
         Color.magenta,
         Color.green
     };
 
     void Start()
     {
         StartCoroutine(TypeText());
     }
 
     IEnumerator TypeText()
     {
         var charArray = text.ToCharArray();
         for (var i = 0; i < charArray.Length; i++)
         {
             if (i <= colors.Length - 1)
             {
                 label.text += "<color=" + ColorToHexString(colors[i]) + ">" + charArray[i] + "</color>";
             }
             else
             {
                 label.text += charArray[i];
             }
             yield return new WaitForSeconds(letterPause);
         }
     }
 
     string ColorToHexString(Color color)
     {
         Color32 color32 = color;
         return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color32.r, color32.g, color32.b, color32.a);
     }
 }

editor result

I did not record a gif, but it types each character at a rate of letterPause

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 FunFreighterGames · Sep 25, 2020 at 08:56 PM

I know this is old, but I ran into the same problem and found that it was a lot easier to use text mesh pro add whatever string you need into text mesh pro text and increment maxVisibleCharacters. That way you can ignore any rich formatting tags and just let text mesh pro handle it.

 myTMPText.text = myString;          
 
   for (lettersDisplayed = 0; lettersDisplayed < dialogueLength; lettersDisplayed++)
             {
                 myTMPText.maxVisibleCharacters = lettersDisplayed;
                 yield return new WaitForSeconds(letterDelay);
             }
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 rubendariohh · Sep 28, 2020 at 07:32 PM 0
Share

Hi. I have been looking for it for a while. Can you please share the entire code to see how you implemented it? Thanks!

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

23 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

Related Questions

rich text issue 3 Answers

Can I display some text from a a website in Unity? 1 Answer

How can I make the text go to a new line when it reaches the end of the box? 1 Answer

Why does my 3d text keep turning white? 1 Answer

How to color text smoothly or how to color part of a letter? 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