Exemple #1
0
    public void LoadData()
    {
        if (File.Exists(savePath))
        {
            Save save;

            var binaryFormatter = new BinaryFormatter();

            using (var fileStream = File.Open(savePath, FileMode.Open))
            {
                save = (Save)binaryFormatter.Deserialize(fileStream);
            }

            slController.LoadGameState(save);

            Debug.Log("Data Loaded");
        }
        else
        {
            Debug.LogWarning("Save file does not exist");
        }
    }
        private IEnumerator <float> _LoadScenes()
        {
            //--- 0. Prevent multiple simultaneous loads/unloads
            if (isLoading || isUnloading)
            {
                yield break;
            }
            isLoading = true;

            //--- 1. Show loading screen ---
            int        debugLogCounter       = 1; //Kann gelöscht werden, wenn die ganzen ---- 1. Show Loading Screen ---- Debug.Logs entfernt werden
            GameObject loadingScreenInstance = null;

            if (loadingScreen != null)
            {
                Debug.Log($"---- {(debugLogCounter++)}. Show Loading Screen ----");
                loadingScreenInstance = Instantiate(loadingScreen);
                DontDestroyOnLoad(loadingScreenInstance);
                yield return(Timing.WaitUntilDone(loadingScreenInstance.GetComponent <LoadingScreenBase>()._ShowLoadingScreen()));

                Debug.Log($"---- {(debugLogCounter++)}. Loading Screen is ready ----");
            }

            //--- 2. Unload scenes ---
            if (unloadScenesBefore)
            {
                foreach (var scene in scenesToUnloadBefore)
                {
                    if (SceneUtilitiesW.IsSceneLoaded(scene))
                    {
                        Debug.Log("unload Scene: " + scene.ScenePath);
                        yield return(Timing.WaitUntilDone(SceneManager.UnloadSceneAsync(scene)));
                    }
                    else
                    {
                        Debug.LogWarning($"Can't unload scene \"{(string)scene}\" because it's not loaded. Make sure to unload only loaded scenes.");
                    }
                }
            }

            //--- 2.1 Wait for one frame (not sure if necessary, just to make sure that all OnDestroy of the unloaded scene are finished)
            yield return(Timing.WaitForOneFrame);

            //--- 2.2 Wait the additional fake loading time which can artificially extend the loading process
            if (additionalFakeLoadingTime != 0f)
            {
                yield return(Timing.WaitForSeconds(additionalFakeLoadingTime));
            }

            //--- 3. Load single scene if not additively ---
            Debug.Log($"---- {(debugLogCounter++)}. Load Scenes ----");
            if (!loadAdditively)
            {
                Debug.Log("load Scene: " + newScene.ScenePath);
                yield return(Timing.WaitUntilDone(SceneManager.LoadSceneAsync(newScene, LoadSceneMode.Single)));
            }

            //--- 4. Load additive scenes ---
            foreach (var scene in scenesToLoadAdditively)
            {
                Debug.Log("load Scene additively: " + scene.ScenePath);
                yield return(Timing.WaitUntilDone(SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive)));
            }

            //--- 5. Load save game data if there is any ---
            if (loadSaveGameData && saveLoadController != null && saveLoadController.loadingPrepared)
            {
                Debug.Log($"---- {(debugLogCounter++)}. Load SaveGameData ----");
                saveLoadController.loadingPrepared = false;
                //yield return Timing.WaitUntilDone(Timing.RunCoroutine(saveLoadController.LoadGameState()));
                saveLoadController.LoadGameState();
            }

            //--- 6. After all scenes are loaded: search for all initializers and initialize all those who want to be initialized at the scene start ---
            Debug.Log($"---- {(debugLogCounter++)}. Initialize Scene ----");
            InitializeScenes();

            //--- 7. Hide loading screen ---
            Debug.Log($"---- {(debugLogCounter++)}. Hide LoadingScreen ----");

            if (loadingScreenInstance != null)
            {
                loadingScreenInstance.GetComponent <LoadingScreenBase>()?.HideLoadingScreen();
            }

            //--- 8. Loading finished ---
            isLoading = false;
        }