Unity UI Text Fading

When I run the following code in my user interface text

Color color = text.color; color.a -= 1.0f; text.color = color; 

The alpha value of the text is immediately set to 0. How can I just fade the text.

+6
source share
7 answers

Add this to the update method or coroutine coroutine -

if(text.color != Color.clear) Color.Lerp (text.color, Color.clear, fadeSpeed * Time.deltaTime);

+2
source

If you are using Unity 4.6 or later, you can use CrossFadeAlpha and CrossFadeColor .

Example:

 // fade to transparent over 500ms. text.CrossFadeAlpha(0.0f, 0.05f, false); // and back over 500ms. text.CrossFadeAlpha(1.0f, 0.05f, false); 

These two functions are a little nicer to use, since you don’t have to worry about tracking anything. Just call and go on your day.

+8
source

Color values ​​in Unity work in the range 0f..1f , therefore:

  • 0.0f - 0% (or 0/255, as shown in the editor)
  • 0.5f - 50% (or 127.5 / 255)
  • 1.0f - 100% (or 255/255)

Subtracting at 1.0f results in a value of 0%. Try a different decrement, for example 0.1f :

 color.a -= 0.1f; 
+2
source

You can use Coroutines:

Example:

 public Text text; public void FadeOut() { StartCoroutine(FadeOutCR); } private IEnumerator FadeOutCR() { float duration = 0.5f; //0.5 secs float currentTime = 0f; while(currentTime < duration) { float alpha = Mathf.Lerp(1f, 0f, currentTime/duration); text.color = new Color(text.color.r, text.color.g, text.color.b, alpha); currentTime += Time.deltaTime; yield return null; } yield break; } 
+2
source

here is my simpler solution if you use a text object. The code attenuates the text of the text object to which it is attached for input and output. speed can be changed using blinkStep. (to check, just make it publicly available). You can simply copy and paste it into a script named 'TextFlicker' or rename the class to whatever your script name is .; -)

 using UnityEngine; using UnityEngine.UI; using System.Collections; public class TextFlicker : MonoBehaviour { float blinkDurationSecs =1f; float blinkProgress =0f; float blinkStep = 0.01f; //Color txtColor = Color.black; Text blinkingText; // Use this for initialization void Start () { blinkingText = GetComponentInParent<Text>(); } // Update is called once per frame void Update () { if ((blinkProgress > 1)||(blinkProgress<0)) { blinkStep*=-1f; } blinkProgress+=blinkStep; blinkingText.color = Color.Lerp (Color.black, Color.white, blinkProgress);// or whatever color you choose } } 
0
source

Here is the Blink code for any text or interface elements.

 using UnityEngine; using UnityEngine.UI; using System.Collections; public class Blink : MonoBehaviour { // this is the UI.Text or other UI element you want to toggle public MaskableGraphic imageToToggle; public float interval = 1f; public float startDelay = 0.5f; public bool currentState = true; public bool defaultState = true; bool isBlinking = false; void Start() { imageToToggle.enabled = defaultState; StartBlink(); } public void StartBlink() { // do not invoke the blink twice - needed if you need to start the blink from an external object if (isBlinking) return; if (imageToToggle !=null) { isBlinking = true; InvokeRepeating("ToggleState", startDelay, interval); } } public void ToggleState() { imageToToggle.enabled = !imageToToggle.enabled; } } 
0
source

For everyone, you can use this script as a component for each text:

 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class FadeController : MonoBehaviour { public float fadeDuration = 0.5f; public float fadeDelay = 0f; public float fadeTo = 0f; public Text text; void Start () { // Fade with initial delay Invoke ("fade", fadeDelay); } public void fade () { // Fade in/out text.CrossFadeAlpha (fadeTo, fadeDuration, false); } } 
0
source

Source: https://habr.com/ru/post/980819/


All Articles