using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using System; public class LoadManager : MonoBehaviour { public bool IsEnteringNewGame; public bool LoadingGame; public bool LoadingMainMenu; public event Action OnScenesLoaded; public bool noneAdditiveScenes; private int remainingAdditiveScenes; // Μεταβλητό πεδίο της κλάσης private static LoadManager _instance; public static LoadManager GetInstance() { return _instance; } private void Awake() { if (!_instance) { _instance = this; } } public void NewGame() { IsEnteringNewGame = true; } public void LoadMainMenu() { LoadingMainMenu = true; SaveSlotManager.GetInstance().LoadingScreen.SetActive(true); SaveSlotManager.GetInstance().loadingUIAnimator.SetBool("FadeIn", true); SceneManager.LoadScene("MainMenu"); } public void LoadGame() { //PlayerData loadedData = SaveSlotManager.LoadPlayerData(0); //if (loadedData != null) //{ // // Use the loaded data to set the player's state // // Example: // PlayerManager.GetInstance().PlayerGO.transform.position = loadedData.playerPosition; // LoadingGame = true; //} GameSaveData loadedData = SaveSlotManager.LoadPlayerData(SaveSlotManager.GetInstance().selectedSaveSlotID /*Change later to take the ID of the currently selected Save slot*/); // Load the scene first LoadingGame = true; //LoadSceneAsync(loadedData.gameProgressData.currentActiveScene); //New way of loading multiple scenes: LoadMainAndAdditiveScenesAsync(loadedData.gameProgressData.currentActiveScene, loadedData.gameProgressData.currentAdditiveScenes); //LoadScene.LoadMainAndAdditiveScenesTogether(loadedData.gameProgressData.currentActiveScene, loadedData.gameProgressData.currentAdditiveScenes); //StartCoroutine(LoadScene.LoadMainAndAdditiveScenesTogetherCoroutine(loadedData.gameProgressData.currentActiveScene, loadedData.gameProgressData.currentAdditiveScenes)); } public void LoadMainAndAdditiveScenesAsync(string MainSceneName, string[] additionalScenes) { // StartCoroutine(LoadMainAndAdditiveScenesAsyncEnumerator(MainSceneName, additionalScenes)); //Time.timeScale = 0; StartCoroutine(LoadMainAndAdditiveScenesAsyncREAL(MainSceneName, additionalScenes)); } #region OLD private IEnumerator LoadMainAndAdditiveScenesAsyncEnumerator(string MainSceneName, string[] additionalScenes) { yield return StartCoroutine(AsyncLoader.LoadMainAndAdditiveScenesAsync(MainSceneName, additionalScenes)); } private void LoadSceneAsync( string sceneName) { StartCoroutine(LoadSceneAsyncEnumerator(sceneName)); } private IEnumerator LoadSceneAsyncEnumerator(string sceneName) { yield return StartCoroutine(AsyncLoader.LoadSceneAsync(sceneName, OnProgress, OnComplete)); } private void OnProgress(float progress) { Debug.Log("Loading progress: " + (progress * 100) + "%"); } private void OnComplete() { Debug.Log("Scene loaded!"); } #endregion public IEnumerator LoadMainAndAdditiveScenesAsyncREAL(string mainSceneName, string[] additionalScenes) { remainingAdditiveScenes = additionalScenes.Length; // Ορίζουμε το αρχικό πλήθος if (additionalScenes.Length <= 0) { noneAdditiveScenes = true; } // Load the main scene asynchronously AsyncOperation mainSceneLoading = SceneManager.LoadSceneAsync(mainSceneName); // Load additional scenes additively asynchronously foreach (string sceneName in additionalScenes) { AsyncOperation additionalSceneLoading = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); StartCoroutine(WaitForAsyncOperationAdditive(additionalSceneLoading)); // Χωρίς ref } // Wait until the main scene is fully loaded yield return WaitForAsyncOperationMainScene(mainSceneLoading, noneAdditiveScenes); } private IEnumerator WaitForAsyncOperationMainScene(AsyncOperation operation, bool noAdditiveScenes) { while (!operation.isDone) { float progress = Mathf.Clamp01(operation.progress / 0.9f); Debug.Log("Loading scene progress: " + (progress * 100) + "%"); yield return null; } if (noAdditiveScenes) { print("IM DONE NO ADDITIVE SCENES !!!!!!!!!!!!!!!!!!!!! WE LITERALLY HIT 15 HOURS OF WORK TODAY (AGAIN)"); // Notify subscribers that scenes are loaded OnScenesLoaded?.Invoke(); //Time.timeScale = 1; noneAdditiveScenes = false; } print("Main scene loaded!"); } private IEnumerator WaitForAsyncOperationAdditive(AsyncOperation operation) { while (!operation.isDone) { float progress = Mathf.Clamp01(operation.progress / 0.9f); Debug.Log("Loading additive scene progress: " + (progress * 100) + "%"); yield return null; } remainingAdditiveScenes--; // Μειώνουμε τον counter print($"Additive scene loaded! Remaining: {remainingAdditiveScenes}"); if (remainingAdditiveScenes <= 0) // Μόνο όταν όλες οι additive σκηνές φορτωθούν { print("All additive scenes loaded! Calling OnScenesLoaded..."); OnScenesLoaded?.Invoke(); } } }