UGUI脚本实现(图片或文本)闪烁

思路:

在固定秒数(比如3秒)内,让UI物体(图片或文本)的透明度逐渐降为0,然后,在相同秒数内,再让它的透明度变回来——如此循环,就闪烁起来了。

图片闪烁:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class TweenImageColor : MonoBehaviour
{

    public float blinkSpeed;//闪烁速度
    private bool isAddAlpha;//是否增加透明度
    private float timer;//计时器
    public float timeval = 1;//时间间隔

    private Image img;//指向自身图片

    private void Start()
    {
        img = GetComponent<Image>();
    }

    private void Update()
    {
        timer += Time.deltaTime;
        if (isAddAlpha)
        {
            img.color += new Color(0, 0, 0, Time.deltaTime * blinkSpeed);
            if (timer >= timeval)
            {
                img.color = new Color(img.color.r, img.color.g, img.color.b, 1);
                isAddAlpha = false;
                timer = 0;
            }
        }
        else
        {
            img.color -= new Color(0, 0, 0, Time.deltaTime * blinkSpeed);
            if (timer >= timeval)
            {
                img.color = new Color(img.color.r, img.color.g, img.color.b, 0);
                isAddAlpha = true;
                timer = 0;
            }
        }
    }


}

文本闪烁:

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


public class TweenTextColor : MonoBehaviour
{
    public float blinkSpeed;//闪烁速度
    private bool isAddAlpha;//是否增加透明度
    private float timer;//计时器
    public float timeval = 1;//时间间隔

    private Text t;//指向自身图片
    private void Start()
    {
       t  = GetComponent<Text>();
    }

    private void Update()
    {
        timer += Time.deltaTime;
        if (isAddAlpha)
        {
            t.color += new Color(0, 0, 0, Time.deltaTime * blinkSpeed);
            if (timer >= timeval)
            {
                t.color = new Color(t.color.r, t.color.g, t.color.b, 1);
                isAddAlpha = false;
                timer = 0;
            }
        }
        else
        {
            t.color -= new Color(0, 0, 0, Time.deltaTime * blinkSpeed);
            if (timer >= timeval)
            {
                t.color = new Color(t.color.r,t.color.g, t.color.b, 0);
                isAddAlpha = true;
                timer = 0;
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值