using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; public class LevelLoader : MonoBehaviour { [SerializeField] private AnimationClip fadeAnimation = null; [SerializeField][Min(0)] private float loadingScreenWaitTime = 1f; private static Animator transition; private static LevelLoader instance = null; public static LevelLoader Instance { get { return instance; } set { } } private float fadeTime = 1f; void Awake() { // Singleton if (instance == null) { instance = this; DontDestroyOnLoad(gameObject); transition = GameObject.FindGameObjectWithTag("FadeCanvas").GetComponent<Animator>(); if (fadeAnimation != null) { fadeTime = fadeAnimation.length; } SceneManager.sceneLoaded += OnLevelFinishedLoading; } else { DestroyImmediate(this); } } private void OnDisable() { SceneManager.sceneLoaded -= OnLevelFinishedLoading; } private void OnLevelFinishedLoading(Scene scene, LoadSceneMode loadMode) { GameObject o; if(o = GameObject.FindWithTag("FadeCanvas")) { if(o.TryGetComponent<Animator>(out Animator anim)) { transition = anim; } } } public void LoadLevel(string sceneName) { StartCoroutine(LoadingRoutine(sceneName)); } IEnumerator LoadingRoutine(string sceneName) { if(transition) transition.SetTrigger("Fade"); yield return new WaitForSeconds(fadeTime); SceneManager.LoadScene("Loading"); yield return new WaitForSeconds(loadingScreenWaitTime); SceneManager.LoadSceneAsync(sceneName); } }