Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by M0rrigan · Dec 23, 2015 at 04:30 PM · c#uitextunity5string

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;
 
         }
 
     }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Dec 23, 2015 at 06:40 PM 1
Share

possibly post a screen shot. it's often better to do this with individual letters as individual sprites

avatar image M0rrigan Fattie · Dec 30, 2015 at 10:11 AM 0
Share

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?

alt text

alpha-color.png (38.2 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image M0rrigan · Dec 30, 2015 at 05:32 PM 0
Share

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?

avatar image saschandroid M0rrigan · Dec 31, 2015 at 12:11 AM 1
Share

try:

 myText.text = myText.text.Replace(alphabetLetter, "<color=#ffffffxx>"+alphabetLetter+"</color>");


avatar image M0rrigan saschandroid · Dec 31, 2015 at 12:40 PM 0
Share

Great, I was so close to the solution. Thank you very much!

avatar image
-2

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Remove the & at the end of concatenated string 0 Answers

Ui text to string ? 1 Answer

Unity UI: Text Adventure 2 Answers

Click ui text to dissapear in 3D unity 2017 first person game script for ui multiple texts?,Clicking Text away in Unity 2017 3D first person game 0 Answers

How to bold only the selected characters from a text? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges