using UnityEngine; using UnityEngine.SceneManagement; using System.Collections; using TMPro; using UnityEngine.UI; public class LoadingScene : MonoBehaviour { public static LoadingScene Instance; public CanvasGroup canvasGroup; public float fadeDuration = 1f; public bool playMiniGame = false; void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } public void Fading(float start, float end) { StartCoroutine(Fade(start, end)); } public void FadeAndLoad(string sceneName) { StartCoroutine(FadeRoutine(sceneName)); } public void BeginMiniGame() { StartCoroutine(StartMiniGame()); } IEnumerator StartMiniGame() { yield return StartCoroutine(Fade(0, 1)); playMiniGame = true; yield return new WaitForSeconds(1.5f); yield return StartCoroutine(Fade(1, 0)); playMiniGame = false; } // Starts the scene but spawns the player in the mini-game starting position public void LoadMiniGame(string sceneName) { playMiniGame = true; StartCoroutine(FadeRoutine(sceneName)); } IEnumerator FadeRoutine(string sceneName) { canvasGroup.interactable = false; canvasGroup.blocksRaycasts = true; yield return StartCoroutine(Fade(0, 1)); yield return new WaitForSeconds(2f); // Load the scene AsyncOperation op = SceneManager.LoadSceneAsync(sceneName); while (!op.isDone) { yield return null; } // Wait for the scene to load yield return StartCoroutine(Fade(1, 0)); canvasGroup.interactable = true; canvasGroup.blocksRaycasts = false; } IEnumerator Fade(float start, float end) { float t = 0f; while (t < fadeDuration) { t += Time.deltaTime; canvasGroup.alpha = Mathf.Lerp(start, end, t / fadeDuration); yield return null; } canvasGroup.alpha = end; } // Public method for external scripts to trigger fading public IEnumerator FadeCoroutine(float start, float end) { yield return StartCoroutine(Fade(start, end)); } // Public method for simple fade calls public void StartFade(float start, float end) { StartCoroutine(Fade(start, end)); } }