48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
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);
|
||
}
|
||
} |