Text alpha fade transition using c#

Updated on February 25, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on February 25, 2018

How can I create a transition effect on alpha component of UI text using c# without animating the text color using mecanim?

  • Liked by
Reply
1 on February 25, 2018
public class TextFade : MonoBehaviour
{
    UnityEngine.UI.Text m_text;
    private void Start()
    {
        m_text = GetComponent<UnityEngine.UI.Text>();
    }
    private void Update()
    {
        Color c = m_text.color;
        c.a = Mathf.Abs(Mathf.Sin(Time.time));
        m_text.color = c;
    }
}

This code just makes it fade in and out, but you can adapt it to be whatever.

But to answer your question directly, you just want to store the current text color in a variable, change that color’s alpha to whatever you want, and then set the original text’s color to the new color variable you just made.

on February 25, 2018

Thanks, much obliged. This gave me the design I wanted, although using mecanim was an option but I wanted to achieve it using scripting. Thanks again

Show more replies
  • Liked by
Reply
Cancel