忽亮忽暗的文字提示代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 忽亮忽暗的文字提示
/// </summary>
public class TextTipsBrightCtrl : MonoBehaviour
{
private Text text;
private void Awake()
{
text = GetComponent<Text>();
}
/// <summary>
/// 透明度
/// </summary>
float alpha = 0.9f;
bool toBright;
void Update()
{
if(alpha >= 1.0f)
toBright = false;
else if(alpha <= 0.3f)
toBright = true;
if(toBright)
alpha += 0.02f;
else
alpha -= 0.02f;
text.color = new Color(text.color.r, text.color.g, text.color.b, alpha);
}
}
忽亮忽暗的图片提示代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 忽亮忽暗的提示
/// </summary>
public class TipsBrightCtrl : MonoBehaviour
{
private Image img;
private void Awake()
{
img = GetComponent<Image>();
}
/// <summary>
/// 透明度
/// </summary>
float alpha = 0.9f;
bool toBright;
void Update()
{
if (alpha >= 1.0f)
toBright = false;
else if (alpha <= 0.3f)
toBright = true;
if (toBright)
alpha += 0.02f;
else
alpha -= 0.02f;
img.color = new Color(img.color.r, img.color.g, img.color.b, alpha);
}
}