- Home /
Animated Colorization of UITextMeshPro,Animated vertex color text mesh pro
Hello there,
I'm currently working on a project where I have to colorize text left to right through time.
It's working for the most part but the thing is my first letter is never colorized.
My question is is there something wrong with the way I'm doing it ?
Here is my code:
public class WinTextHandler : MonoBehaviour {
#region SINGLETON
public static WinTextHandler instance;
private void Awake()
{
if (instance == null)
instance = this;
else
DestroyObject(gameObject);
}
#endregion
TextMeshProUGUI text;
Animator textAnim;
TMP_TextInfo textInfo;
int characterCount;
// Use this for initialization
void Start () {
text = GetComponent<TextMeshProUGUI>();
textAnim = GetComponent<Animator>();
}
public float FillInGreen()
{
textInfo = text.textInfo;
characterCount = text.text.Length;
Debug.Log(characterCount);
StartCoroutine(Fill());
return characterCount * 0.07f;
}
IEnumerator Fill()
{
int currentCharacter = 0;
Debug.Log(textInfo.characterInfo[0].vertexIndex + " " + textInfo.characterInfo[1].vertexIndex);
Color32[] newVertexColors;
Color32 c0 = new Color32((byte)142, (byte)184, (byte)28, (byte)255);
while (true)
{
// If No Characters then just yield and wait for some text to be added
if (currentCharacter == characterCount)
{
yield return new WaitForSeconds(0.20f);
break;
}
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[currentCharacter].materialReferenceIndex;
// Get the vertex colors of the mesh used by this text element (character or sprite).
newVertexColors = textInfo.meshInfo[materialIndex].colors32;
// Get the index of the first vertex used by this text element.
int vertexIndex = textInfo.characterInfo[currentCharacter].vertexIndex;
// Only change the vertex color if the text element is visible.
if (textInfo.characterInfo[currentCharacter].isVisible)
{
c0 =
newVertexColors[vertexIndex + 0] = c0;
newVertexColors[vertexIndex + 1] = c0;
newVertexColors[vertexIndex + 2] = c0;
newVertexColors[vertexIndex + 3] = c0;
// New function which pushes (all) updated vertex data to the appropriate meshes when using either the Mesh Renderer or CanvasRenderer.
text.UpdateVertexData();
// This last process could be done to only update the vertex data that has changed as opposed to all of the vertex data but it would require extra steps and knowing what type of renderer is used.
// These extra steps would be a performance optimization but it is unlikely that such optimization will be necessary.
}
currentCharacter = currentCharacter + 1;
yield return new WaitForSeconds(0.05f);
}
}
public void Win()
{
textAnim.SetTrigger("Win");
}
}
I'm using Unity 2017.4.2f1.
Thanks in advance for the help !!
Have a magical day !
Maxence
Answer by Lednar · Oct 02, 2018 at 09:45 AM
I just started experimenting with TMPro myself and in Examples And Scripts folders there is VertexColorCycler.cs. In there I debugged it and noticed that textInfo.characterInfo[currentCharacter].isVisible
is false/not_visible few first frames and on that time I am not able to color a character in any way I tried. So it is possible on your case its already jumping to next character after few frames before it can get access to the first character.
Your answer
Follow this Question
Related Questions
How to correctly shader vertices on a grid. 1 Answer
UI text color not assigning correctly 2 Answers
How to change color of part of a text? 1 Answer
How to change color via script 2 Answers