创建UI Image,脚本直接拖上面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ImageAlpha : MonoBehaviour
{
private Image image;
float colorA;
bool IsColorA;
void Start()
{
image = GetComponent<Image>();
colorA = image.color.a;
}
// Update is called once per frame
void Update()
{
if (colorA>=1)//图片已显示
{
IsColorA = true;
}
else if (colorA <=0)//图片已隐藏
{
IsColorA = false;
}
if (IsColorA)//渐隐
{
colorA -= 1 * Time.deltaTime;
}
else//渐现
{
colorA += 1 * Time.deltaTime;
}
image.color = new Color(image.color.r, image.color.g, image.color.b, colorA);
Debug.Log(colorA);
}
}