Files
plugin-library/Assets/00.StaryEvo/Runtime/Tool/loading/SplashScreen.cs
2025-09-04 11:43:35 +08:00

48 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SplashScreen : MonoBehaviour
{
private Image image;
async void Start()
{
image = GetComponentInChildren<Image>();
// 初始完全透明
image.color = new Color(1, 1, 1, 0);
// 渐显效果2秒
await Fade(0, 1, 0.5f);
// 保持显示原2秒等待
await UniTask.Delay(TimeSpan.FromSeconds(1));
// 渐隐效果1秒
await Fade(1, 0, 0.5f);
await SceneManager.LoadSceneAsync(1);
if (transform != null)
Destroy(gameObject);
}
private async UniTask Fade(float startAlpha, float endAlpha, float duration)
{
float elapsed = 0f;
while (elapsed < duration)
{
float alpha = Mathf.Lerp(startAlpha, endAlpha, elapsed / duration);
image.color = new Color(1, 1, 1, alpha);
elapsed += Time.deltaTime;
await UniTask.Yield();
}
image.color = new Color(1, 1, 1, endAlpha);
}
}