Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question How can I change color while writing texts in typing game?

Discussion in 'Scripting' started by dorukmeseci1, Apr 9, 2023.

  1. dorukmeseci1

    dorukmeseci1

    Joined:
    Apr 8, 2023
    Posts:
    1
    Hello,

    I'm making a typing game. I followed the commands in the video, but I have a question.
    I want the texts to be painted rather than erased as I write them. how can I do that





     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Mmm... coffee!!! Lifting a delicious steaming cup right now in fact...

    For color in a text field, I would just wrangle the usual HTML rich text color tags in, something like putting text in like so:

    <color=#ff0000">COF</color>FEE


    You can use the Substring() method to chop the string apart and inject coloring.

    Here was my "typewriter progressive text" package, which has to handle colored text, so it does all the fiddling and jiggling with the HTML rich text tags.

    The code, all set up in the attached package:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. // @kurtdekker - inspired from Unity forum at:
    7. // https://forum.unity.com/threads/text-typewriter-without-displaying-richtext-tag.891742/
    8.  
    9. public class TypewriterWithHTMLStrings : MonoBehaviour
    10. {
    11.     const string initialString = "Leave this blank and it will pull up what is in the UI.Text field already.";
    12.     [Tooltip(initialString)]
    13.     [Multiline]
    14.     public string originalString;
    15.  
    16.     public string cursor;
    17.  
    18.     public float interval;
    19.  
    20.     // If you prefer to use TMPro, then do two things:
    21.     //    add using TMPro; at the top of this script
    22.     //    replace this Text declaration with TextMeshProUGUI
    23.     public Text output;
    24.  
    25.     void Reset()
    26.     {
    27.         originalString = initialString;
    28.         cursor = "_";
    29.         interval = 0.05f;
    30.     }
    31.  
    32.     int GetPayloadLength( string s)
    33.     {
    34.         int count = 0;
    35.         bool payload = true;
    36.  
    37.         for (int i = 0; i < s.Length; i++)
    38.         {
    39.             char c = s[i];   // one character
    40.  
    41.             if (payload)
    42.             {
    43.                 if (c == '<')
    44.                 {
    45.                     payload = false;  // we just entered a formatting block
    46.                 }
    47.                 else
    48.                 {
    49.                     count++;   // tally payload character
    50.                 }
    51.             }
    52.             else
    53.             {
    54.                 if (c == '>') payload = true;  // done with this formatting block, back to payload
    55.             }
    56.         }
    57.  
    58.         return count;
    59.     }
    60.  
    61.     string  GetPartialPayload( string s, int typedSoFar)
    62.     {
    63.         int count = 0;
    64.         bool payload = true;
    65.         string tempString = "";
    66.  
    67.         for (int i = 0; i < s.Length; i++)
    68.         {
    69.             char c = s[i];   // one character
    70.  
    71.             if (payload)
    72.             {
    73.                 if (c == '<')
    74.                 {
    75.                     tempString = tempString + c.ToString();
    76.                     payload = false;  // we just entered a formatting block
    77.                 }
    78.                 else
    79.                 {
    80.                     if (count < typedSoFar) tempString = tempString + c.ToString();
    81.                     count++;   // tally payload character
    82.                 }
    83.             }
    84.             else
    85.             {
    86.                 tempString = tempString + c.ToString();
    87.                 if (c == '>')
    88.                 {
    89.                     payload = true;  // done with this formatting block, back to payload
    90.                 }
    91.             }
    92.         }
    93.  
    94.         return tempString;
    95.     }
    96.  
    97.     IEnumerator Start ()
    98.     {
    99.         // if blank, pick up what's in the UI.Text field to start with
    100.         if (string.IsNullOrEmpty(originalString))
    101.         {
    102.             originalString = output.text;
    103.         }
    104.  
    105.         int i = 0;
    106.         while( true)
    107.         {
    108.             output.text = GetPartialPayload( originalString, i) + cursor;
    109.  
    110.             yield return new WaitForSeconds( interval);
    111.  
    112.             i++;
    113.  
    114.             if (i > GetPayloadLength(originalString))
    115.             {
    116.                 break;
    117.             }
    118.         }
    119.  
    120.         // remove cursor
    121.         output.text = originalString;
    122.     }
    123. }
     

    Attached Files:

  3. Samvin

    Samvin

    Joined:
    Jun 14, 2018
    Posts:
    1
    Hey! I am working on a similar game myself! Did you got any solutions? It would be really helpful! :D