38 lines
1.0 KiB
C#
38 lines
1.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
// 控制图片透明度的循环脚本
|
|
public class ImageFadeLoop : MonoBehaviour
|
|
{
|
|
private Image targetImage; // 需要控制透明度的Image组件
|
|
public float fadeSpeed = 1f; // 透明度变化的速度
|
|
public float minAlpha = 0.2f; // 最低透明度
|
|
public float maxAlpha = 1f; // 最高透明度
|
|
|
|
private float time;
|
|
|
|
void Start()
|
|
{
|
|
targetImage = GetComponent<Image>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// 使用 Mathf.PingPong 来在 0 到 1 之间循环
|
|
time += Time.deltaTime * fadeSpeed;
|
|
|
|
// Mathf.PingPong 返回一个在 [0, 1] 范围内循环的值
|
|
float alpha = Mathf.PingPong(time, 1f);
|
|
|
|
// 使用线性插值将 alpha 值转换到 minAlpha 和 maxAlpha 范围
|
|
alpha = Mathf.Lerp(minAlpha, maxAlpha, alpha);
|
|
|
|
// 修改图片的透明度
|
|
Color color = targetImage.color;
|
|
color.a = alpha;
|
|
targetImage.color = color;
|
|
}
|
|
}
|