Unity控制Image或RawImage渐变

Unity控制Image或RawImage渐变_第1张图片

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

public class UIcontrol : MonoBehaviour {

	//该组件的Alpha控制UI的显示或隐藏
	public CanvasGroup canvasGroup;
	//UI默认是显示的
	private float targetAlpha = 1 ;
	//渐变速度
	private float smoothing = 1;
	void Update()
	{
		//利用插值函数控制UI显示的渐变过程
		if (canvasGroup.alpha != targetAlpha)
		{
			canvasGroup.alpha = Mathf.Lerp(canvasGroup.alpha, 
				targetAlpha,smoothing*Time.deltaTime);
			//当前alpha与目标alpha很接近时表示已达到目标值
			if (Mathf.Abs(canvasGroup.alpha - targetAlpha) < 0.01f)
			{
				canvasGroup.alpha = targetAlpha;
			}
		}
	}
	public void Hide()
	{

		targetAlpha=0;
	}

	public void Show()
	{
		targetAlpha = 1;
	}

}

 

你可能感兴趣的:(Unity,初识小知识)