Search Unity

Simple character coloring script

Discussion in 'UGUI & TextMesh Pro' started by Lednar, Oct 2, 2018.

  1. Lednar

    Lednar

    Joined:
    Jan 9, 2018
    Posts:
    14
    I just started experimenting with TMPro and I want to color a single character in text. I know the method like in markup languages (We are <color=green>green</color> with envy). But I am looking something as simple as this example, unfortunately this colors the whole text:

    Code (CSharp):
    1. void Awake()
    2.     {
    3.         TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
    4.         textmeshPro.color = new Color32(255, 128, 0, 255);
    5.     }  
    I am hoping to do it outside Update() function. Any help or tips?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The color of individual characters is controlled via the vertex color of the individual / geometry of each character.

    When using the <color=#FF8000> rich text tag, this sets the vertex color of that character to the appropriate color.

    The color of individual characters can also be modified via code. Take a look at Example 23 - Animating Vertex Attributes (included in the TMP Examples & Extras) and related scripts.
     
  3. Lednar

    Lednar

    Joined:
    Jan 9, 2018
    Posts:
    14
    Thank you, they are really relevant hints. After playing with mentioned Example 23, a question raised for me. Can the vertex color be changes only in Update() or in coroutine cycle? Without Update() or coroutine yielding I was not able to change single character vertices color. If not then I should stop trying to do it that way.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    In that example, the color and position of the characters are being changed each frame so in such case it makes sense to use something like update or a coroutine. However, these vertex attributes can be changed in some function or on the press of a button or whatever.