[Unity3D daily] UI text gradient effect

I. Introduction

Share a piece of text gradient code, first look at the effect:
Insert picture description here

Second, the text

Paste the code directly--

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

public class TextFade : MonoBehaviour
{
    public Text text;
    public float dt = 0.07f;//打字间隔时间
    public float showingTime = 2f;//显示使用的时间
    public float duration = 3f; // 显示时长

    void Start()
    {
        Show("测试效果");
    }

    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;  // 文字长度索引
        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) //十进制转十六进制
    {
        int r = Mathf.RoundToInt(color.r * 255.0f);
        int g = Mathf.RoundToInt(color.g * 255.0f);
        int b = Mathf.RoundToInt(color.b * 255.0f);
        string hex = string.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
        return hex;
    }
}

3. How to use

1. Attach the script casually to an object
Insert picture description here
2. Drag the text to be displayed into the Text slot of the TextFade script:
Insert picture description here
Then, this place is the text to be displayed:
Insert picture description here
you can test the effect . .

Published 226 original articles · praised 509 · 530,000 views

Guess you like

Origin blog.csdn.net/q764424567/article/details/101029495