代码的目的是在unity中让TextmeshPro字体不停闪烁变色的脚本,比如说我运行之后,有一行TextmeshPro的文字标题在黄色和白色之间反复闪烁,提醒用户注意。

首先需要在project中新建一个script,具体操作方式就是在project窗口内空白处右键——create——C#script

把新建的脚本命名为:tmpcolor

 

然后双击打开新建的tmpcolor文件,把原有的代码删除,复制这个代码进去:

using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using static System.Net.Mime.MediaTypeNames;
using System.Xml.Linq;

public class tmpcolor : MonoBehaviour
{
    public TMP_Text UIText;
    private float timer;
    void Start()
    {
        timer = 0.0f;
    }
    // 通过update获得每帧运算
    void Update()
    {
        timer += Time.deltaTime * 2;
        if (timer % 2 > 1.0f)
        {
            UIText.color = Color.yellow;//文字颜色变黄
        }
        else
        {
            UIText.color = Color.white;//文字颜色变白
        }

    }

}

要注意:public class tmpcolor : MonoBehaviour 中 tmpcolor要和你的脚本名称保持一致
然后把这个代码附加到你需要修改颜色的TMP物体上就可以了
具体方法是:在Hierarchy中点击物体,在右侧点击Add Component,输入tmpcolor并选择,然后将需要操作的TextmeshPro物体从Hierarchy拖到右侧的inspector中text后面的框框内

其他unity相关文章:

其他unity内容: https://www.gongyesheji.org/?cat=860

UNITY如何删除插件(包/packages)

Hololens2+unity实现QRCODE二维码识别

unity如何实现点击按钮后文字改变

通过UNITY中的Textmeshpro(TMPro)UI显示当前系统时间

unity实现点击按钮多次延迟切换背景图片/界面

Unity物体在原位置上下浮动效果的代码

Unity延迟2秒后让TextmeshPro字体变色的脚本

 

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    发表回复

    您的邮箱地址不会被公开。 必填项已用 * 标注