I’m currently working a game where you can click on individual words in a Text Mesh Pro.
When a word has been clicked, I need it to change color.
I’m already able to figure out what word was pressed and to find out the characterindexes of the relevant characters. Old post (2014) regarding text mesh pro suggested to change the color property of the individual vertices of the character in order to change the color of the word. (http://digitalnativestudios.com/forum/index.php?topic=253.0) Unfortunately the approach outlined there isn’t available any longer because some of the necessary fields are no longer exposed (or changed?).
I have tried to set the colors as followed instead
TMP_WordInfo info = _text.textInfo.wordInfo[wordIndex];
for (int i = 0; i < info.characterCount; ++i)
{
TMP_CharacterInfo cInfo = _text.textInfo.characterInfo[info.firstCharacterIndex + i];
cInfo.vertex_BL.color = myColor32;
cInfo.vertex_BR.color = myColor32;
cInfo.vertex_TL.color = myColor32;
cInfo.vertex_TR.color = myColor32;
}
Hey guys,
I now it is a while since somebody posted here. I am quite new to unity and C#. Actually I never had anything to do with coding but recently I decided to work on an app. This app requires that a word changes the color when I hover over any character of the word and changes to another color when I click on the chosen word.
I created a text - TextMeshPro and created a new script in the inspector of the text. Then I opened the script file.
Could someone explain where do I need to copy paste the codes above? The initial code looks like this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class changeColor : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
If you’re new to coding but understand UV and vertices then you might understand how textmesh pro colors the words. If not then the script is quite advanced I’d say. But, there’s a way. There is a script called “Text_Selector_B.cs” that gets downloaded when you download Examples of TextMeshPro From 'Package Manager. Fortunately, it works exactly as you wanted. When you hover your mouse on a word, it changes color and mouse is not hovered , word returns to original color. You can change from hover to mouse click in few minutes. you need to put that Script on the same Gameobject that contains TextMeshProUGUI component. Then it’ll surely work. Don’t forget to check out example "link interaction " if you get confused.
Hello, I’m currently trying to do the same thing but in a different way… I basically want it so the a word is highlighted with no mouse inputs and then after a failed event or correct event it would change the color of the word its currently on accordingly. I’ve been reading everything and even tried putting in the code provided above but it simply doesnt work… Is there anyone that can help?
Take a look at the example “Animating Vertex Attributes” and related scripts included in the TMP Examples & Extras. The VertexColorCycler.cs script changes the color per characters but can easily be modified to modify the color per word. Basically instead of using the textInfo.characterInfo[ ], you would access the textInfo.wordInfo[index] to lookup the index of the first and last character of each word and then just like the script does modify the appropriate vertex colors.
The original solution usually doesn’t work, but can be updated to work.
The key thing is that TMP behind the scenes silently deletes most/all your changes before the end of the frame by design (if I remember correctly: this is a deliberate (but very confusing) performance optimization). It’s hard to predictable unless you’ve read the source code / dived deep into this, but most commonly: if you change the text (or anything else) during this frame then your color changes will get wiped.