So I tried looping through all words and tweening their alpha-values one after the other.
Sadly, what happens is that always the whole text (all words) is alpha-tweened, most likely because “textComponent” always references the same shared component, or just because all words share the same material.
Does anybody know of a way to access alpha values of words, individually?
Or would I need to create a new TextMeshPro-object-instance for each word?
In that example the fading occurs per character so to achieve a similar result per word, you would iterate over each word. For each word, you would still have to apply the vertex attribute change to each of the characters of the word.
When working with the TextInfo and the data it contains, remember that it is the resulting output of the text processing. Changing the TextInfo or data contained within has no impact on the text object.
Hello Stephan! I know it’s an old topic but I’m trying to achieve the opposite of your script. Fade IN char by char with alpha + color. But I couldn’t figure out how can I modify your script to do this. I tried
and tried to reverse the for cycle, but didn’t get to work… Or do I need to set all characters’s alpha to zero one by one first? Can you point out to me where do I need to modify the script?
I slightly modified the script so it would reveal instead of hide. Make sure your public Color ColorTint; is set to alpha max alpha, otherwise it will never trigger the next char if (alpha == 255)
using System.Collections;
using TMPro;
using UnityEngine;
public class RevealText : MonoBehaviour
{
private TMP_Text m_TextComponent;
public float FadeSpeed = 1.0F;
public int RolloverCharacterSpread = 10;
public Color ColorTint;
void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
void Start()
{
StartCoroutine(AnimateVertexColors());
}
/// <summary>
/// Method to animate vertex colors of a TMP Text object.
/// </summary>
/// <returns></returns>
IEnumerator AnimateVertexColors()
{
// Need to force the text object to be generated so we have valid data to work with right from the start.
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
Color32[] newVertexColors;
int currentCharacter = 0;
int startingCharacterRange = currentCharacter;
bool isRangeMax = false;
while (!isRangeMax)
{
int characterCount = textInfo.characterCount;
// Spread should not exceed the number of characters.
byte fadeSteps = (byte)Mathf.Max(1, 255 / RolloverCharacterSpread);
for (int i = startingCharacterRange; i < currentCharacter + 1; i++)
{
// Skip characters that are not visible
//if (textInfo.characterInfo[i].isVisible) continue;
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[i].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[i].vertexIndex;
// Get the current character's alpha value.
byte alpha = (byte)Mathf.Clamp(newVertexColors[vertexIndex + 0].a + fadeSteps, 0, 255);
// Set new alpha values.
newVertexColors[vertexIndex + 0].a = alpha;
newVertexColors[vertexIndex + 1].a = alpha;
newVertexColors[vertexIndex + 2].a = alpha;
newVertexColors[vertexIndex + 3].a = alpha;
// Tint vertex colors
// Note: Vertex colors are Color32 so we need to cast to Color to multiply with tint which is Color.
newVertexColors[vertexIndex + 0] = (Color)newVertexColors[vertexIndex + 0] * ColorTint;
newVertexColors[vertexIndex + 1] = (Color)newVertexColors[vertexIndex + 1] * ColorTint;
newVertexColors[vertexIndex + 2] = (Color)newVertexColors[vertexIndex + 2] * ColorTint;
newVertexColors[vertexIndex + 3] = (Color)newVertexColors[vertexIndex + 3] * ColorTint;
if (alpha == 255)
{
startingCharacterRange += 1;
if (startingCharacterRange == characterCount)
{
// Update mesh vertex data one last time.
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
yield return new WaitForSeconds(1.0f);
// Reset the text object back to original state.
m_TextComponent.ForceMeshUpdate();
yield return new WaitForSeconds(1.0f);
// Reset our counters.
currentCharacter = 0;
startingCharacterRange = 0;
//isRangeMax = true; // Would end the coroutine.
}
}
}
// Upload the changed vertex colors to the Mesh.
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
if (currentCharacter + 1 < characterCount) currentCharacter += 1;
yield return new WaitForSeconds(0.25f - FadeSpeed * 0.01f);
}
}
}
I’m sorry @Marijn_Kiwi , your code is not quite right.
This is the right way:
using System.Collections;
using UnityEngine;
using TMPro;
public class ConsoleSystem : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI m_TextComponent;
[SerializeField] private float FadeSpeed = 20.0f;
[SerializeField] private int RolloverCharacterSpread = 10;
void Start()
{
StartCoroutine(FadeInText());
}
/// <summary>
/// Method to animate (fade in) vertex colors of a TMP Text object.
/// </summary>
/// <returns></returns>
IEnumerator FadeInText()
{
// Set the whole text transparent
m_TextComponent.color = new Color
(
m_TextComponent.color.r,
m_TextComponent.color.g,
m_TextComponent.color.b,
0
);
// Need to force the text object to be generated so we have valid data to work with right from the start.
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
Color32[] newVertexColors;
int currentCharacter = 0;
int startingCharacterRange = currentCharacter;
bool isRangeMax = false;
while (!isRangeMax)
{
int characterCount = textInfo.characterCount;
// Spread should not exceed the number of characters.
byte fadeSteps = (byte)Mathf.Max(1, 255 / RolloverCharacterSpread);
for (int i = startingCharacterRange; i < currentCharacter + 1; i++)
{
// Skip characters that are not visible (like white spaces)
if (!textInfo.characterInfo[i].isVisible) continue;
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[i].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[i].vertexIndex;
// Get the current character's alpha value.
byte alpha = (byte)Mathf.Clamp(newVertexColors[vertexIndex + 0].a + fadeSteps, 0, 255);
// Set new alpha values.
newVertexColors[vertexIndex + 0].a = alpha;
newVertexColors[vertexIndex + 1].a = alpha;
newVertexColors[vertexIndex + 2].a = alpha;
newVertexColors[vertexIndex + 3].a = alpha;
if (alpha == 255)
{
startingCharacterRange += 1;
if (startingCharacterRange == characterCount)
{
// Update mesh vertex data one last time.
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
yield return new WaitForSeconds(1.0f);
// Reset the text object back to original state.
m_TextComponent.ForceMeshUpdate();
yield return new WaitForSeconds(1.0f);
// Reset our counters.
currentCharacter = 0;
startingCharacterRange = 0;
//isRangeMax = true; // Would end the coroutine.
}
}
}
// Upload the changed vertex colors to the Mesh.
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
if (currentCharacter + 1 < characterCount) currentCharacter += 1;
yield return new WaitForSeconds(0.25f - FadeSpeed * 0.01f);
}
}
}
This code works great with one exception. When the loop finishes fading in every letter in the paragraph, the text disappears. (Note: the isRangeMax = true; line is not commented out). I want the text to stay opaque once the loop is over. I tried to modify the code to make this work but nothing is quite working.
So I wanted to be able to change if the text should be fading yes or not.
This will activate the loop on Trigger, and deactivate the loop on Trigger. It also makes sure that when the loop exits the text is not transparent. Before this, the text would disappear on exiting the loop.
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class TextFade : MonoBehaviour
{
[SerializeField] private TextMeshPro m_TextComponent;
[SerializeField] private float FadeSpeed = 20.0f;
[SerializeField] private int RolloverCharacterSpread = 10;
public bool FadeActiveTrigger = false;
public bool FadeNotActiveTrigger = false;
private bool isFading = false;
void Start()
{
}
private void Update()
{
if (FadeActiveTrigger)
{
isFading = true;
StartCoroutine(FadeInText());
FadeActiveTrigger = false;
}
if (FadeNotActiveTrigger)
{
StopCoroutine(FadeInText());
FadeNotActiveTrigger = false;
isFading = false;
}
}
/// <summary>
/// Method to animate (fade in) vertex colors of a TMP Text object.
/// </summary>
/// <returns></returns>
IEnumerator FadeInText()
{
// Set the whole text transparent
m_TextComponent.color = new Color
(
m_TextComponent.color.r,
m_TextComponent.color.g,
m_TextComponent.color.b,
0
);
// Need to force the text object to be generated so we have valid data to work with right from the start.
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
Color32[] newVertexColors;
int currentCharacter = 0;
int startingCharacterRange = currentCharacter;
bool isRangeMax = false;
while (!isRangeMax && isFading)
{
int characterCount = textInfo.characterCount;
// Spread should not exceed the number of characters.
byte fadeSteps = (byte)Mathf.Max(1, 255 / RolloverCharacterSpread);
for (int i = startingCharacterRange; i < currentCharacter + 1; i++)
{
// Skip characters that are not visible (like white spaces)
if (!textInfo.characterInfo[i].isVisible) continue;
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[i].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[i].vertexIndex;
// Get the current character's alpha value.
byte alpha = (byte)Mathf.Clamp(newVertexColors[vertexIndex + 0].a + fadeSteps, 0, 255);
// Set new alpha values.
newVertexColors[vertexIndex + 0].a = alpha;
newVertexColors[vertexIndex + 1].a = alpha;
newVertexColors[vertexIndex + 2].a = alpha;
newVertexColors[vertexIndex + 3].a = alpha;
if (alpha == 255)
{
startingCharacterRange += 1;
if (startingCharacterRange == characterCount)
{
// Update mesh vertex data one last time.
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
yield return new WaitForSeconds(1.0f);
// Reset the text object back to original state.
m_TextComponent.ForceMeshUpdate();
yield return new WaitForSeconds(1.0f);
// Reset our counters.
currentCharacter = 0;
startingCharacterRange = 0;
//isRangeMax = true; // Would end the coroutine.
}
}
}
// Upload the changed vertex colors to the Mesh.
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
if (currentCharacter + 1 < characterCount) currentCharacter += 1;
yield return new WaitForSeconds(0.25f - FadeSpeed * 0.01f);
}
if (!isFading)
{
m_TextComponent.color = new Color(m_TextComponent.color.r, m_TextComponent.color.g, m_TextComponent.color.b, 1);
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
}
}
}
Please let me know if you find any way to improve, or if you have better solutions.