_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Battle_Royale/FadeToBlack.cs

57 lines
1.3 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FadeToBlack : MonoBehaviour
{
public float fadeDuration = 1.5f; // <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
private Image blackImage;
2024-11-23 15:28:39 +08:00
public GameObject LightLinp;//<2F>ƹ<EFBFBD>
void Start()
{
blackImage = GetComponent<Image>();
}
// <20>ⲿ<EFBFBD><E2B2BF><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public void StartFade()
{
2024-11-23 15:28:39 +08:00
LightLinp.SetActive(false);
StartCoroutine(FadeToBlackCoroutine());
}
// <20>ⲿ<EFBFBD><E2B2BF><EFBFBD>ã<EFBFBD>ֱ<EFBFBD>ӱ<EFBFBD><D3B1><EFBFBD>
public void InstantBlack()
{
blackImage.color = new Color(0, 0, 0, 1f); // <20><><EFBFBD><EFBFBD>Ϊȫ<CEAA><C8AB>
}
2024-11-23 15:28:39 +08:00
public void ReturnWhite()
{
LightLinp.SetActive(true);
blackImage.color = new Color(0, 0, 0, 0f); // <20><><EFBFBD><EFBFBD>Ϊȫ<CEAA><C8AB>
}
// Э<>̣<EFBFBD><CCA3><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private IEnumerator FadeToBlackCoroutine()
{
float elapsedTime = 0f;
2024-11-23 15:28:39 +08:00
// Ŀ<><C4BF>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
float targetAlpha = 0.6f;
while (elapsedTime < fadeDuration)
{
elapsedTime += Time.deltaTime;
2024-11-23 15:28:39 +08:00
// <20><><EFBFBD>㵱ǰ͸<C7B0><CDB8><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>0<EFBFBD>𽥵<EFBFBD>Ŀ<EFBFBD><C4BF>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>0.6<EFBFBD><EFBFBD>
float alpha = Mathf.Lerp(0f, targetAlpha, elapsedTime / fadeDuration);
blackImage.color = new Color(0, 0, 0, alpha);
yield return null; // <20>ȴ<EFBFBD><C8B4><EFBFBD>һ֡
}
2024-11-23 15:28:39 +08:00
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD>մﵽĿ<EFB5BD><C4BF>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
blackImage.color = new Color(0, 0, 0, targetAlpha);
}
}