Unity_触摸屏_实现图片渐显效果

将下列脚本挂载到需要渐显的图片上

using UnityEngine;
using UnityEngine.UI;

public class JianXian : MonoBehaviour
{
	//声明imgFillAmount
	private Image imgFillAmount;
	//是否开始读条
	bool isPlay = true;
	//计时用:初始时间
	float timer = 0;
	//计时用:读条所用的全部时间
	float duration = 1;


	void Start()
	{
		//获取到刚刚修改Image Type为Filled的Image
		imgFillAmount = GetComponent<Image>();
	}

	void Update()
	{
		//判断是否开始读条
		if (isPlay)
		{
			//使timer根据时间增长
			timer += Time.deltaTime;
			//修改FillAmount的值
			//(使当前时间占全部时间的比例为FillAmount中0到1之间的值)
			imgFillAmount.fillAmount = Mathf.Lerp(0, 1, timer / duration);

			//计时器
			if (timer >= duration)
			{
				//停止读条
				isPlay = false;
				//将timer还原为0,为下一次计时做准备
				timer = 0;
			}
		}


	}

    private void OnEnable()
    {
        isPlay = true;
    }
}

渐显的图片需要修改成如下 设置

猜你喜欢

转载自blog.csdn.net/weixin_42137574/article/details/103763421
今日推荐