- Home /
Change alpha color of specific letter in a Text UI with C#
Hello! I need a script that allows me to selectively hide and show characters of a Text UI. I know how to change the alpha color of text itself, but I don't know how to analyze the Text string. For this purpose I have a Text UI with the text component I need to check. I also have a gameobject with a bool: when this bool is true, the alpha color of a specific letter in Text UI is set to 1. For example, let's say I need to show every "B" letter in the Text. Which code should I use to pick every "B" letter and set the alpha color to 1?
Thanks in advance
This is my code
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Collectible : MonoBehaviour {
public bool characterB;
public GameObject letterText;
public Color textColor;
void Update ()
{
if (characterB == true)
{
Text text = letterText.GetComponent<Text>();
text.color = textColor;
textColor.a=0;
}
}
possibly post a screen shot. it's often better to do this with individual letters as individual sprites
Hi, yes the individual letters way was my first choice to achieve what I wanted. However, my game is based on these text, so I was looking for a way to speed up the creation of the texts, and eventually the translation process. I've noticed that you can set a different color (and alpha as well) for different letter of the same text, but I don't know how to check the entire string. To be more clear, it's like the hangman game. In the very beginning every letter is hidden (alpha color = 0), then according to the player's progress I need to display every letter that matches with the player's input. I suppose it's just a matter of understanding what is the right code that checks the string of the Text UI object for a specific value (for example A), and changes the alpha color. I really don't know how to access that element of a Text UI. $$anonymous$$aybe someone does?
Answer by saschandroid · Dec 30, 2015 at 01:17 PM
Example for letter 'B'
Text myText = letterText.GetComponent<Text>();
myText.text = myText.text.Replace("B", "<color=#ffffffxx>B</color>");
xffffffxx = white with xx = your alpha value in hex
yep, it works! you're magic, thank you very much! What if I would use a string variable ins$$anonymous$$d of the value? I've tried:
public string alphabetLetter;
myText.text = myText.text.Replace(alphabetLetter, "<color=#ffffffxx>alphabetLetter</color>");
but obviously it doesn't work when it is in quotation marks. I suppose there's a specific syntax or maybe not?
try:
myText.text = myText.text.Replace(alphabetLetter, "<color=#ffffffxx>"+alphabetLetter+"</color>");
Great, I was so close to the solution. Thank you very much!
Answer by rajan4uto · Dec 30, 2015 at 11:53 AM
use this : textColor=new Vector4(0, 0, 0, 0);
that contain Vector4(r,g,b,a)
Your answer
Follow this Question
Related Questions
Remove the & at the end of concatenated string 0 Answers
Ui text to string ? 1 Answer
Unity UI: Text Adventure 2 Answers
How to bold only the selected characters from a text? 3 Answers