/黑屏淡入淡出///
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class MyFadeInOut : MonoBehaviour
{
public Image fadeImage;
[Tooltip("过渡颜色")]
public Color fadeColor = Color.black;
[Range(1, 100), Tooltip("淡入淡出的速度")]
public byte stepRate = 2;
float step;
void OnValidate()
{
if (fadeImage == null)
fadeImage = GetComponentInChildren<Image>();
}
void Start()
{
step = stepRate * 0.001f;
}
/// <summary>
/// 计算淡出/淡出时间。
/// </summary>
/// <returns>持续时间(秒)</returns>
public float GetDuration()
{
float frames = 1 / step;
float frameRate = Time.deltaTime;
float duration = frames * frameRate * 0.1f;
return duration;
}
public IEnumerator FadeIn()
{
float alpha = fadeImage.color.a;
while (alpha < 1)
{
yield return null;
alpha += step;
fadeColor.a = alpha;
fadeImage.color = fadeColor;
}
}
public IEnumerator FadeOut()
{
float alpha = fadeImage.color.a;
while (alpha > 0)
{
yield return null;
alpha -= step;
fadeColor.a = alpha;
fadeImage.color = fadeColor;
}
}
}
unity黑屏淡入淡出
最新推荐文章于 2024-08-16 09:48:40 发布