using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; public class LevelManager : MonoBehaviour { // A serializable structure that holds a UI button and its corresponding scene [System.Serializable] public class LevelEntry { public Button levelButton; public string sceneName; public string jumpPoint; } [SerializeField] private string jumpPointGrottoEssen; [SerializeField] private string jumpPointGrottoKueche; // A list of LevelEntry objects that can be filled in the inspector public List levels = new List(); private void Awake() { // Reset entry level points EntryLevel.GoToGrottoKueche = false; EntryLevel.GoToGrottoEssen = false; // Loop through each level entry and hook up the button to load the correct scene foreach (var entry in levels) { // Check if both the button and scene name are set if (entry.levelButton != null && !string.IsNullOrEmpty(entry.sceneName)) { // Store the scene name locally to avoid closure issues in the lambda string sceneToLoad = entry.sceneName; entry.levelButton.onClick.AddListener(() => OnClickLoadLevel(sceneToLoad)); string goToJumpPoint = entry.jumpPoint; entry.levelButton.onClick.AddListener(() => SetEntryLevel(goToJumpPoint)); } } } // This method is called when a button is clicked, and loads the given scene by name public void OnClickLoadLevel(string sceneName) { StartCoroutine(LoadAsyncScene(sceneName)); } IEnumerator LoadAsyncScene(string sceneName) { AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName); // Wait until the asynchronous scene fully loads while (!asyncLoad.isDone) { yield return null; } } public void SetEntryLevel(string entryPoint) { if (string.IsNullOrEmpty(entryPoint)) { return; } if (entryPoint == jumpPointGrottoEssen) { EntryLevel.GoToGrottoEssen = true; return; } if (entryPoint == jumpPointGrottoKueche) { EntryLevel.GoToGrottoKueche = true; return; } } }