【Unity3D日常】UI文字渐变效果实现

一、前言

分享一段文字渐变的代码,先看一下效果吧:
【Unity3D日常】UI文字渐变效果实现_第1张图片

二、正文

直接贴代码吧 - -

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 += " + 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 += "";
                    }

                    string aStr = Convert.ToString(a, 16);
                    aStr = (aStr.Length == 1 ? "0" : "") + aStr;
                    this.text.text += " + RGBColorToHex(this.text.color) + aStr + ">" + text[i] + "";
                }
                if (a == 255 && start)
                {
     
                    Startime = Time.time;
                    this.text.text += "";
                    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;
    }
}

三、怎么用

1、将脚本随便附到一个对象上面
【Unity3D日常】UI文字渐变效果实现_第2张图片
2、将要显示文本的text拖入TextFade脚本的Text插槽中:
【Unity3D日常】UI文字渐变效果实现_第3张图片
然后,这个地方是要显示的文本:
在这里插入图片描述
大家可以测试一下效果了。。。

你可能感兴趣的:(#,Unity3D日常开发,Unity,Unity日常)