Ejemplo n.º 1
0
        /// <summary>
        /// Initializes and loads the next scene asynchrounsouly
        /// </summary>
        /// <param name="nextScene">The scene to load</param>
        /// <param name="silent">True, if no loading scene is shown, otherwise false</param>
        public async void SwitchScene(Scene nextScene, bool silent = false)
        {
            if (IsLoading)
            {
                return;
            }

            IsLoading = true;

            var oldScene = CurrentScene;

            if (!silent)
            {
                oldScene?.OnUnloading();
                CurrentScene = loadingScene;
                CurrentScene.OnShown();
            }

            // made the method async, such that instead of freezing the game, exceptions now crash the game.
            await Task.Run(() =>
            {
                UnloadScene(oldScene);
                nextScene.Initialize();
                nextScene.LoadContent();

                GC.Collect();
                GC.WaitForPendingFinalizers();

                // swap scene
                this.Dispatch(() =>
                {
                    CurrentScene.OnUnloading();
                    IsLoading    = false;
                    CurrentScene = nextScene;
                    CurrentScene.OnShown();
                });
            });
        }