One, foreword

Let’s share a snippet of text gradient code to see what it looks like:

Second, the body

Just post the code

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class TextFade : MonoBehaviour
{
    public Text text;
    public float dt = 0.07 f;// Typing interval
    public float showingTime = 2f;// Display the usage time
    public float duration = 3f; // Displays the duration

    void Start()
    {
        Show("Test effect");
    }

    public void Show(string text)
    {
        if (duration <= 0)
        {
            transform.parent.gameObject.SetActive(false);
            return;
        }
        StartCoroutine(FadeIn(text));
    }

    private IEnumerator FadeIn(string text)
    {
        float Startime = Time.time, aTime, dTime = Time.time;

        int index = 0;  // Text length index
        float timeScale;
        int a = 0;
        bool start = false;

        if (this.text)
        {
            while (index < text.Length || a < 255)
            {
                this.text.text = "";
                timeScale = 256 / (index * showingTime);
                aTime = (Time.time - Startime) * timeScale;
                for (int i = 0; i <= index && i < text.Length; i++)
                {
                    a = (int)(aTime * (index - i));
                    a = Mathf.Clamp(a, 0.255);

                    if (a == 255 && i == 0 && start == false)
                    {
                        this.text.text += "<color=#" + RGBColorToHex(this.text.color) + "ff>";
                        start = true;
                    }
                    if (a == 255 && start)
                    {
                        this.text.text += text[i];
                        continue;
                    }
                    if(a ! =255 && start)
                    {
                        start = false;
                        this.text.text += "</color>";
                    }

                    string aStr = Convert.ToString(a, 16);
                    aStr = (aStr.Length == 1 ? "0" : "") + aStr;
                    this.text.text += "<color=#" + RGBColorToHex(this.text.color) + aStr + ">" + text[i] + "</color>";
                }
                if (a == 255 && start)
                {
                    Startime = Time.time;
                    this.text.text += "</color>";
                    do
                    {
                        this.text.text = "";
                        //transform.parent.gameObject.SetActive(false);
                        yield return 0;
                    } while (Time.time - Startime > duration);
                }
                if (Time.time - dTime >= dt)
                {
                    dTime = Time.time;
                    index++;
                }
                yield return 0; }}}private string RGBColorToHex(Color color) // Convert decimal to hexadecimal
    {
        int r = Mathf.RoundToInt(color.r * 255.0 f);
        int g = Mathf.RoundToInt(color.g * 255.0 f);
        int b = Mathf.RoundToInt(color.b * 255.0 f);
        string hex = string.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
        returnhex; }}Copy the code

Three, how to use

1. Attach the script to a random object

2. Drag the text to display into the text slot of the TextFade script:

Then, here is the text to display:

Now you can test the effect…