using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; using DG.Tweening; namespace Ashwild.UI { /// /// 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. /// [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(); // 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); }); } } }