做个文字闪烁效果,闪瞎小伙伴的双眼(Unity3D)

2022-08-07 09:17:44 浏览数 (1)

一、前言

分享一段文字渐变的代码,先看一下效果吧:

二、正文

直接贴代码吧 - -

代码语言:javascript复制
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 &amp;&amp; i < text.Length; i  )
                {
                    a = (int)(aTime * (index - i));
                    a = Mathf.Clamp(a, 0, 255);

                    if (a == 255 &amp;&amp; i == 0 &amp;&amp; start == false)
                    {
                        this.text.text  = "<color=#"   RGBColorToHex(this.text.color)   "ff>";
                        start = true;
                    }
                    if (a == 255 &amp;&amp; start)
                    {
                        this.text.text  = text[i];
                        continue;
                    }
                    if (a != 255 &amp;&amp; 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 &amp;&amp; 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;
    }
}

三、怎么用

1、将脚本随便附到一个对象上面

2、将要显示文本的text拖入TextFade脚本的Text插槽中:

然后,这个地方是要显示的文本:

大家可以测试一下效果了。。。

0 人点赞