예제 #1
0
        // ********************************************************************
        #endregion
        // ********************************************************************


        // ********************************************************************
        #region Private Methods
        // ********************************************************************
        public IEnumerator CR_LoadScene(string _newScene, int _buildIndex = 0, bool _save = false)
        {
            Debug.Log("Loading Scene: " + _newScene);
            m_loading = true;

            string oldScene = SceneManager.GetActiveScene().name;

            m_sceneHistory.Add(oldScene);

            // COVERING_SCREEN
            {
                //Debug.Log("Loading Scene state: " + LoadingState.COVERING_SCREEN);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.COVERING_SCREEN, _newScene, oldScene);
                }

                // Fade to black
                yield return(m_blackness.FadeIn());
            }

            // OPENING_LOADING_SCREEN
            {
                //Debug.Log("Loading Scene state: " + LoadingState.OPENING_LOADING_SCREEN);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.OPENING_LOADING_SCREEN, _newScene, oldScene);
                }

                // Load loading screen
                yield return(SceneManager.LoadSceneAsync(_save ? m_loadingSceneSave : m_loadingScene, LoadSceneMode.Additive));

                if (_save)
                {
                    // Inform player of save
                    TMP_Text saveText = GameObject.Find(m_saveTextObjectName).GetComponent <TMP_Text>();
                    saveText.text = "Saving...";
                }
            }

            // HIDING_SCREEN_COVER
            {
                //Debug.Log("Loading Scene state: " + LoadingState.HIDING_SCREEN_COVER);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.REVEALING_LOADING_SCREEN, _newScene, oldScene);
                }

                // Fade to loading screen
                yield return(m_blackness.FadeOut());
            }

            // CLOSING_PANELS
            {
                //Debug.Log("Loading Scene state: " + LoadingState.CLOSING_PANELS);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.CLOSING_PANELS, _newScene, oldScene);
                }

                PanelManager.CloseAllPanels();
                while (PanelManager.NumPanelsOpen() > 0)
                {
                    yield return(null);
                }
            }

            // UNLOADING_OLD_SCENE
            {
                //Debug.Log("Loading Scene state: " + LoadingState.UNLOADING_OLD_SCENE);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.UNLOADING_OLD_SCENE, _newScene, oldScene);
                }

                // !!! unload old screen
                yield return(SceneManager.UnloadSceneAsync(oldScene));
            }

            // UNLOADING_ASSETS
            {
                //Debug.Log("Loading Scene state: " + LoadingState.UNLOADING_ASSETS);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.UNLOADING_ASSETS, _newScene, oldScene);
                }

                // Clear databses to allow asset unloading
                Events.Raise(new ClearDatabaseForSceneChangeEvent());
            }

            // SAVING_GAME
            if (_save)
            {
                //Debug.Log("Loading Scene state: " + LoadingState.SAVING_GAME);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.SAVING_GAME, _newScene, oldScene);
                }

                // Figure out how long to wait
                float saveEndTime = Time.time + m_saveDisplayDuration;

                // Triggering save
                ProfileManager.Save();

                // Wait a minimum time for player to see message
                while (Time.time < saveEndTime)
                {
                    yield return(null);
                }
            }

            // GAME_SAVED
            if (_save)
            {
                //Debug.Log("Loading Scene state: " + LoadingState.GAME_SAVED);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.GAME_SAVED, _newScene, oldScene);
                }

                // Inform player of save complete
                TMP_Text saveText = GameObject.Find(m_saveTextObjectName).GetComponent <TMP_Text>();
                saveText.text = "Game saved!";

                // Wait a minimum time for player to see message
                yield return(new WaitForSeconds(m_saveDisplayDuration));
            }

            float endTime = Time.time + m_minDuration;

            // LOADING_NEW_SCENE
            {
                //Debug.Log("Loading Scene state: " + LoadingState.LOADING_NEW_SCENE);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.LOADING_NEW_SCENE, _newScene, oldScene);
                }

                // Load level async
                if (_newScene.NullOrEmpty())
                {
                    yield return(SceneManager.LoadSceneAsync(_buildIndex, LoadSceneMode.Additive));
                }
                else
                {
                    yield return(SceneManager.LoadSceneAsync(_newScene, LoadSceneMode.Additive));
                }

                if (_newScene.NullOrEmpty())
                {
                    SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(_buildIndex));
                }
                else
                {
                    SceneManager.SetActiveScene(SceneManager.GetSceneByName(_newScene));
                }
            }

            // PROCESSING_INCREMENTAL_LOADERS
            {
                //Debug.Log("Loading Scene state: " + LoadingState.PROCESSING_INCREMENTAL_LOADERS);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.PROCESSING_INCREMENTAL_LOADERS, _newScene, oldScene);
                }

                while (m_loaders.Count > 0)
                {
                    yield return(null);
                }
            }


            // NEW_SCENE_LOADED
            {
                //Debug.Log("Loading Scene state: " + LoadingState.NEW_SCENE_LOADED);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.NEW_SCENE_LOADED, _newScene, oldScene);
                }

                while (Time.time < endTime)
                {
                    yield return(null);
                }
            }

            // HIDING_LOADING_SCREEN
            {
                //Debug.Log("Loading Scene state: " + LoadingState.HIDING_LOADING_SCREEN);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.HIDING_LOADING_SCREEN, _newScene, oldScene);
                }

                // Fade to black
                yield return(m_blackness.FadeIn());
            }

            // UNLOADING_LOADING_SCREEN
            {
                //Debug.Log("Loading Scene state: " + LoadingState.UNLOADING_LOADING_SCREEN);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.UNLOADING_LOADING_SCREEN, _newScene, oldScene);
                }

                // !!! unload loading screen
                yield return(SceneManager.UnloadSceneAsync(_save ? m_loadingSceneSave : m_loadingScene));
            }

            // REVEALING_NEW_SCENE
            {
                //Debug.Log("Loading Scene state: " + LoadingState.REVEALING_NEW_SCENE);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.REVEALING_NEW_SCENE, _newScene, oldScene);
                }

                // Fade to new screen
                yield return(m_blackness.FadeOut());
            }

            // LOADING_COMPLETE
            {
                //Debug.Log("Loading Scene state: " + LoadingState.LOADING_COMPLETE);
                if (OnStateChanged != null)
                {
                    OnStateChanged(LoadingState.LOADING_COMPLETE, _newScene, oldScene);
                }
            }

            m_loading = false;
        }