Example #1
0
        /// <summary>
        /// Loading a scene from with method will raise the appropriate events to make sure that a SceneManager can execute any loading logic
        /// </summary>
        /// <param name="scene">The name of the scene to load</param>
        /// <param name="additive">Whether we should load this scene additively</param>
        /// <param name="additionalScenes">Additional scenes to load additively</param>
        /// /// <param name="useLoadingScreen">Whether the loading screen should be displayed or not</param>
        public static void LoadScene(string scene, bool additive, string[] additionalScenes, bool useLoadingScreen = true)
        {
            if (Instance == null)
            {
                return;
            }

            List <string> scenes = new List <string>()
            {
                scene
            };

            if (additionalScenes != null && additionalScenes.Length > 0)
            {
                foreach (var item in additionalScenes)
                {
                    scenes.Add(item);
                }
            }

            SceneTransitionSettings transition = new SceneTransitionSettings
            {
                scenes           = scenes,
                additive         = additive,
                useAsync         = true,
                useLoadingScreen = useLoadingScreen,
                parameter        = null,
            };

            Instance.StartCoroutine(Instance.Load(transition));
        }
Example #2
0
        IEnumerator Load(SceneTransitionSettings transition)
        {
            //validate transition settings
            if (transition == null)
            {
                yield break;
            }
            if (transition.scenes == null || transition.scenes.Count < 1)
            {
                yield break;
            }
            if (string.IsNullOrEmpty(transition.scenes[0]))
            {
                yield break;
            }

            //BEGIN THE TRANSITION

            //Clone transition settings and make them available to external objects
            _currentTransition = new SceneTransitionSettings()
            {
                scenes           = transition.scenes,
                additive         = transition.additive,
                useAsync         = transition.useAsync,
                useLoadingScreen = transition.useLoadingScreen,
                parameter        = transition.parameter,
            };

            //Raise "Load Scene" Event
            beginTransitionEvent.sceneName = transition.scenes[0];
            EventDispatcher.Event(beginTransitionEvent);

            //Add loading screen
            LoadingScreen loadingScreen = null;

            if (transition.useLoadingScreen)
            {
                //Add loading screen
                if (LoadingScreenPrefab != null)
                {
                    GameObject loadingScreenInstance = Instantiate(LoadingScreenPrefab, Vector3.zero, Quaternion.identity) as GameObject;
                    loadingScreen = loadingScreenInstance.GetComponent <LoadingScreen>();
                }
            }

            //Unload current scenes if scene load is not additive
            if (!transition.additive)
            {
                //Wait one frame to make sure any current transition has time to 'yield break' before we unload it
                yield return(null);

                foreach (SceneManager manager in _sceneManagers)
                {
                    manager.UnloadScene();
                }
            }

            //LOAD ALL THE SCENES
            string currentScene;

            for (int i = 0; i < transition.scenes.Count; i++)
            {
                currentScene = transition.scenes[i];

                //Load the scene
                if (Application.HasProLicense() && transition.useAsync)
                {
                    AsyncOperation asyncLoading;
                    if ((i > 0) || transition.additive)
                    {
                        asyncLoading = Application.LoadLevelAdditiveAsync(currentScene);
                    }
                    else
                    {
                        asyncLoading = Application.LoadLevelAsync(currentScene);
                    }

                    while (!asyncLoading.isDone)
                    {
                        yield return(null);
                    }
                }
                else
                {
                    if ((i > 0) || transition.additive)
                    {
                        Application.LoadLevelAdditive(currentScene);
                    }
                    else
                    {
                        Application.LoadLevel(currentScene);
                    }

                    yield return(null);//necesary to make sure SceneManagers can register themselves to the GameController before we continue
                }

                //Setup the scene via the scene manager
                SceneManager sceneManager = _sceneManagers.FirstOrDefault(x => x.SceneName == currentScene);
                if (sceneManager != null)
                {
                    if (i == 0 && !transition.additive)
                    {
                        _activeSceneManager = sceneManager;
                    }

                    sceneManager.OnLoadSceneStart();

                    Coroutine sceneLoadCoroutine = sceneManager.StartCoroutine(sceneManager.Load());
                    yield return(sceneLoadCoroutine);

                    sceneManager.OnLoadSceneCompleted();
                }
                else if (i == 0 && !transition.additive)
                {
                    _activeSceneManager = null;
                }
            }

            //END THE TRANSITION

            //Remove loading screen
            if (loadingScreen != null)
            {
                Destroy(loadingScreen.gameObject);
            }

            //Raise "Scene Loaded" Event
            completeTransitionEvent.sceneName = transition.scenes[0];
            EventDispatcher.Event(completeTransitionEvent);

            //remove current transition settings
            _currentTransition = null;
        }
Example #3
0
 /// <summary>
 /// Loading a scene from with method will raise the appropriate events to make sure that a SceneManager can execute any loading logic
 /// </summary>
 /// <param name="transition">The settings for the transition</param>
 public static void LoadScene(SceneTransitionSettings transition)
 {
     Instance.StartCoroutine(Instance.Load(transition));
 }