前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >做个文字闪烁效果,闪瞎小伙伴的双眼(Unity3D)

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

作者头像
恬静的小魔龙
发布2022-08-07 09:17:44
9160
发布2022-08-07 09:17:44
举报
文章被收录于专栏:Unity3DUnity3D

一、前言

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

在这里插入图片描述
在这里插入图片描述

二、正文

直接贴代码吧 - -

代码语言: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插槽中:

在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、正文
  • 三、怎么用
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档