3

I was looking for a way to fade the alpha value of TextMesh-Text in Unity, but I could not finde a solution online nor in the LeanTween Documentation.

  • LeanTween.alphaText() does only work with the normal UI-Text (not TextMesh)
  • LeanTween.alpha() doesn't do anything for me on Text.
3
  • Well have you tried using a Coroutine and simply fade the text.color.a value over time without using LeanTween?
    – derHugo
    Mar 3, 2020 at 12:40
  • Yes, I did. That works fine. But like the topic says, it is about a solution for LeanTween. So you can benefit from the LeanTween features, like setEase() for smooth curves, setDelay() and so on. Mar 3, 2020 at 12:45
  • You can also do all of these using Coroutines ;) and apparently more flexible when new types are introduced ;)
    – derHugo
    Mar 3, 2020 at 12:46

3 Answers 3

3

You can also extend the LeanTween extension methods file (LeanTweenExt.cs) with the follow lines. This method will allow you to apply the LeanAlphaText on textMeshes like you're used to do on UI Texts.

public static LTDescr LeanAlphaText (this TextMesh textMesh, float to, float time) {
    var _color = textMesh.color;
    var _tween = LeanTween
        .value (textMesh.gameObject, _color.a, to, time)
        .setOnUpdate ((float _value) => {
            _color.a = _value;
            textMesh.color = _color;
        });
    return _tween;
}
2
  • That looks very nice and advanced. I gave it a try, without success. I found LeanTweenExt.cs in my Project. I added your code in line 19, below "public static LTDescr LeanAlphaText(this RectTransform rectTransform, float (...)", and saved it. In a new Script, attached to a GameObject with a TextMeshPro component, I coded in Start(): TextMesh textMesh = this.GetComponent<TextMesh>(); LeanTween.textAlpha(textMesh, 0f, 1f); Visual Studio marks the variable textMesh: Can not convert 'UnityEngine.TextMesh' to 'UnityEngine.RectTransform'. It looks like my changes did not have any effect. Apr 4, 2020 at 10:06
  • I got it to work with the following method: copy and paste your code into LeanTweenExt.sc. I had to change the type in your code from TextMesh to TMP_Text and add 'using TMPro;' on top. Now I can use your code by calling it from an other script, by typing: TMP_Text text = this.GetComponent<TMP_Text>(); text.LeanAlphaText(0, 1); Is this how you intended it? Apr 4, 2020 at 14:55
3

After looking briefly through the API I guess a better way than introducing CanvasGroups just for fading one single text would rather be using LeanTwean.value for setting its color. CanvasGroup is a bit overkill here in my opinion.

(example adopted from API)

TextMeshProUGUI text;

void Start()
{
    text = GetComponent<TextMeshProUGUI>();
    var color = text.color;
    var fadeoutcolor = color;
    fadeoutcolor.a = 0;
    LeanTween.value(gameObject, updateValueExampleCallback, fadeoutcolor, color, 1f).setEase(LeanTweenType.easeOutElastic).setDelay(2f);
}


void updateValueExampleCallback(Color val)
{
    text.color = val;
}
1

I came up with my own solution.

Instead of changing the alpha of the TextMesh-Component directly, I add a CanvasGroup to the Gameobject that holds my TextMesh-Component. Then I manipulate the alpha value of the CanvasGroup instead.

To use my example code:

  1. On your Canvas go: [RightClick] > UI > Text - TextMeshPro
  2. Attach my example Script to this Gameobject. (This creates the required CanvasGroup automatically)
  3. Press Play (Ctrl + P)

After a delay of 2 Seconds (because of.setDelay(2f) the Text should fade in.

Examplecode:

using UnityEngine;
using TMPro;

[RequireComponent(typeof(CanvasGroup))]
public class LeanTweenTextFade : MonoBehaviour
{          
    private void Start()
    {
        CanvasGroup canvasgroup = this.gameObject.GetComponent<CanvasGroup>();
        TextMeshProUGUI infoTextTMPro = this.gameObject.GetComponent<TextMeshProUGUI>();

        canvasgroup.alpha = 0f;
        infoTextTMPro.text = "This Text should fade in.";

        float duration = 1f;
        LeanTween.alphaCanvas(canvasgroup, 1.0f, duration).setDelay(2f);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.