56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;//注意添加RawImage命名空间
|
|
|
|
public class FadeInOut : MonoBehaviour
|
|
{
|
|
[HideInInspector]
|
|
public bool isBlack = false;//不透明状态
|
|
|
|
private float fadeSpeed = 5f;//透明度变化速率
|
|
public RawImage rawImage;
|
|
public RectTransform rectTransform;
|
|
|
|
private AsyncLoadingScene asyncLoading;
|
|
void Start()
|
|
{
|
|
rawImage.gameObject.SetActive(true);
|
|
rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);//使背景满屏
|
|
rawImage.color = Color.clear;
|
|
OpenImage(false);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
|
|
if (isBlack)
|
|
{
|
|
rawImage.color = Color.Lerp(rawImage.color, Color.black, Time.deltaTime * fadeSpeed);//渐暗
|
|
if (rawImage.color.a > 0.98f)
|
|
{
|
|
rawImage.color = Color.black;
|
|
if (asyncLoading)
|
|
asyncLoading.canLoad = true;
|
|
else
|
|
transform.GetComponent<AsyncLoadingScene>().canLoad = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
rawImage.color = Color.Lerp(rawImage.color, Color.clear, Time.deltaTime * fadeSpeed * 0.5f);//渐亮
|
|
|
|
if (rawImage.color.a < 0.05f)
|
|
{
|
|
rawImage.color = Color.clear;
|
|
rawImage.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void OpenImage(bool isOpen,AsyncLoadingScene async = null)
|
|
{
|
|
rawImage.gameObject.SetActive(true);
|
|
isBlack = isOpen;
|
|
asyncLoading = async;
|
|
}
|
|
} |