忽亮忽暗的文本/图片提示

4 篇文章 0 订阅
本文展示了如何在Unity中通过C#脚本来实现文字和图片的透明度变化,达到忽亮忽暗的视觉效果。代码包括两个类,TextTipsBrightCtrl用于控制文本的亮度变化,而TipsBrightCtrl则用于控制图片的亮度变化。这两个类都通过调整透明度来实现亮暗切换,当透明度达到一定阈值时,会反转增减方向,从而实现闪烁效果。
摘要由CSDN通过智能技术生成

 忽亮忽暗的文字提示代码如下:

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值