Files
Emberwild/Assets/GAME/Script/UI/SplashScreenController.cs
T
2026-06-22 16:18:34 +02:00

96 lines
2.9 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using DG.Tweening;
namespace Ashwild.UI
{
/// <summary>
/// Plays a full-screen splash video at launch, then fades out to reveal the menu underneath.
/// Decoupled from UIManager: it lives on its own high-sorting-order Canvas and simply covers
/// the menu until the clip ends.
/// </summary>
[RequireComponent(typeof(CanvasGroup))]
public class SplashScreenController : MonoBehaviour
{
[Header("References")]
[SerializeField] private VideoPlayer videoPlayer;
[SerializeField] private RawImage videoImage;
[SerializeField] private RenderTexture renderTexture;
[Header("Fade Out")]
[SerializeField] private float fadeOutDuration = 0.5f;
[SerializeField] private Ease fadeOutEase = Ease.InQuad;
private CanvasGroup canvasGroup;
private bool finished;
private void Awake()
{
canvasGroup = GetComponent<CanvasGroup>();
// Cover everything immediately so the menu initializes hidden underneath.
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
canvasGroup.interactable = true;
}
private void OnEnable()
{
if (videoPlayer != null)
videoPlayer.loopPointReached += OnVideoFinished;
}
private void OnDisable()
{
if (videoPlayer != null)
videoPlayer.loopPointReached -= OnVideoFinished;
}
private void Start()
{
if (videoPlayer == null || videoPlayer.clip == null)
{
Debug.LogWarning("SplashScreenController: no VideoPlayer/clip assigned, skipping splash.");
FadeOut();
return;
}
videoPlayer.isLooping = false;
videoPlayer.renderMode = VideoRenderMode.RenderTexture;
videoPlayer.targetTexture = renderTexture;
// Prepare first to avoid a black flash / stutter on the first frame.
videoPlayer.prepareCompleted += OnPrepared;
videoPlayer.Prepare();
}
private void OnPrepared(VideoPlayer vp)
{
vp.prepareCompleted -= OnPrepared;
if (renderTexture != null)
renderTexture.DiscardContents();
vp.Play();
}
private void OnVideoFinished(VideoPlayer vp) => FadeOut();
private void FadeOut()
{
if (finished) return;
finished = true;
canvasGroup.blocksRaycasts = false;
canvasGroup.interactable = false;
canvasGroup.DOFade(0f, fadeOutDuration)
.SetEase(fadeOutEase)
.OnComplete(() =>
{
if (videoPlayer != null) videoPlayer.Stop();
gameObject.SetActive(false);
});
}
}
}