Search Unity

TextMesh Pro Problem setting individual characters alpha - bug?

Discussion in 'UGUI & TextMesh Pro' started by DarioScarpa, Aug 12, 2019.

  1. DarioScarpa

    DarioScarpa

    Joined:
    Jul 28, 2015
    Posts:
    9
    Hi,
    I'm trying to fade a text setting different alpha for individual characters (following some example code).
    Playing the scene, the first letter is not right. If one goes and edits the text through the inspector, it (sometimes) gets fixed.
    Anyway, the behaviour is not consistent: or I'm doing something wrong, or there's a bug somewhere.

    The requested alpha values are right, the first letter should be fully opaque (255), but it's not.

    upload_2019-8-12_18-27-55.png

    Unity 2018.4.4, TMP
    Minimal project to reproduce the problem: https://we.tl/t-ZEM9HaAJQV

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using TMPro;
    6.  
    7. public class TextAlphaFadeBhv : MonoBehaviour
    8. {
    9.     private TMP_Text m_textComponent;
    10.     private bool m_bLogValues = false;
    11.  
    12.     [SerializeField]
    13.     private int m_iMaxOpacity = 255;
    14.     [SerializeField]
    15.     private int m_iMinOpacity = 0;
    16.  
    17.     void Start()
    18.     {
    19.         m_textComponent = GetComponent<TMP_Text>();
    20.     }
    21.    
    22.     void Update()
    23.     {
    24.         m_bLogValues = Input.GetKeyDown(KeyCode.D);      
    25.  
    26.         m_textComponent.ForceMeshUpdate();
    27.  
    28.         TMP_TextInfo textInfo = m_textComponent.textInfo;
    29.         for (int i = 0; i < textInfo.characterCount; ++i) {
    30.             float fChar = Mathf.InverseLerp(0, textInfo.characterCount, i);
    31.             int iOpacityLevel = Mathf.RoundToInt(Mathf.Lerp(m_iMaxOpacity, m_iMinOpacity, fChar));
    32.             setCharAlpha(textInfo, i, iOpacityLevel);
    33.         }
    34.         m_textComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
    35.     }
    36.  
    37.     private void setCharAlpha(TMP_TextInfo textInfo, int iCharId, int iAlpha)
    38.     {
    39.  
    40.         int iMaterialIndex = textInfo.characterInfo[iCharId].materialReferenceIndex;
    41.         Color32[] rVertexColors = textInfo.meshInfo[iMaterialIndex].colors32;
    42.         int iVertexIndex = textInfo.characterInfo[iCharId].vertexIndex;
    43.  
    44.         byte alpha = (byte)Mathf.Clamp(iAlpha, 0, 255);
    45.  
    46.         if (m_bLogValues) {
    47.             Debug.Log("i " + iCharId + " " + alpha);
    48.         }
    49.         rVertexColors[iVertexIndex + 0].a = alpha;
    50.         rVertexColors[iVertexIndex + 1].a = alpha;
    51.         rVertexColors[iVertexIndex + 2].a = alpha;
    52.         rVertexColors[iVertexIndex + 3].a = alpha;
    53.     }
    54. }
    55.  
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    When iterating over the characters, you need to exclude the characters that are not visible by checking the .isVisible flag. Characters that are not visible have the vertexIndex = 0 which is why the first character is being changed.
     
  3. DarioScarpa

    DarioScarpa

    Joined:
    Jul 28, 2015
    Posts:
    9
    Awesome, thanks a lot!