Search Unity

TextMesh Pro Real fade of Text Mesh Pro

Discussion in 'UGUI & TextMesh Pro' started by MicDE, Jan 29, 2019.

  1. MicDE

    MicDE

    Joined:
    Dec 15, 2015
    Posts:
    16
    Hi,

    I was wondering how I can do a real fade of a Text created with Text Mesh Pro. Not only letting characters reveal or vanish by showing them or hiding them but fade the whole text in / out by changing its transparency.
    I looked at the material and see a lot of color properties which I can modify. To do a fade out, I need to get to all those color fields and changing their alpha to 0 over time. I want to do this in Unity's timeline but I am not happy to write a custom playable to do that.
    Is there a more easy way to fade? I guess I am not the only one who would like to fade TExt Mesh Pro in the described way.
    Best
    Mic
     
    tonytopper likes this.
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    There are a few posts around providing some example of how this could be done.

    Here is one such post.
     
  3. pedrociva

    pedrociva

    Joined:
    Sep 27, 2019
    Posts:
    2
    Stephan If you read his request you should've seen that he asked for the exact opposite of what you actually answered.
    He asked for "not only letting characters reveal but fade the whole text".
    This post shows only how to fade letters :)
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Changing the Vertex Color alpha of the text object will fade everything. This way you don't have to worry about any of the material properties.
     
    sleepypancakes likes this.
  5. tingwen55555

    tingwen55555

    Joined:
    Mar 4, 2017
    Posts:
    2
    Hey, I was trying to get the same thing working for more than an hour but I finally found a solution.

    You grab the material from the specific TextMeshPro object using "FontMaterial" (text.material grabs some defaultUIMaterial the text isn't using). text.Color doesn't work either because its property isn't called _color under the hood.


    Anyways, just do this:

    IEnumerator FadeCoroutine()
    {
    float waitTime = 0;
    while (waitTime < 1)
    {
    text.fontMaterial.SetColor("_FaceColor", Color.Lerp(Color.clear, Color.white, waitTime));
    yield return null;
    waitTime += Time.deltaTime / fadeTime;

    }

    }
     
  6. ewanewan27

    ewanewan27

    Joined:
    Feb 26, 2020
    Posts:
    1
    hye , can you share your full script of this, cause im a bit confuse. thanks. stuck for couple hours now hehe.
     
  7. Scrowneck

    Scrowneck

    Joined:
    Nov 18, 2018
    Posts:
    2
    I made a script and put the code below into it. I then added the script to the Text (TMP) that you find under the Canvas. I also clicked on the color of the text and set the alpha to 0. I was doing a Fade in wait and then fade out. So, skip this if you are just doing fade out. This step isn't wholly necessary anyway. Then I dragged the Text (TMP) I was working with in the Hierarchy into the field for the public Text Display in the script I added. I hope this helps any one that comes to this page looking for the answer like I did. To change from fade out to fade in you can use the same code and just switch the 1 and 0 in the line float alpha = Mathf.Lerp(1f, 0f, currentTime / duration);

    Code (csharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using TMPro;
    4.  
    5. public class OpeningText : MonoBehaviour
    6. {
    7.     public TextMeshProUGUI textDisplay;
    8.     public void Start()
    9.     {
    10.         StartCoroutine(FadeOut());
    11.     }
    12.  
    13.     private IEnumerator FadeOut()
    14.     {
    15.         float duration = 2f; //Fade out over 2 seconds.
    16.         float currentTime = 0f;
    17.         while (currentTime < duration)
    18.         {
    19.             float alpha = Mathf.Lerp(1f, 0f, currentTime / duration);
    20.             textDisplay.color = new Color(textDisplay.color.r, textDisplay.color.g, textDisplay.color.b, alpha);
    21.             currentTime += Time.deltaTime;
    22.             yield return null;
    23.         }
    24.         yield break;
    25.     }
    26. }

    God bless.
     
    Ghosthowl and michcros1220 like this.
  8. sleepypancakes

    sleepypancakes

    Joined:
    Jan 26, 2021
    Posts:
    1
    There's a built-in function for this that lets you fade the alpha quite easily:
    Code (CSharp):
    1.  
    2. public void FadeText()
    3. {
    4.     myTextMeshProGUI.CrossFadeAlpha(0, 2f, true);
    5. }
     
    titch_stewart and jwcowand like this.