思路:
在固定秒数(比如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;
}
}
}
}