private void Handler(SceneMetadata metadata)
 {
     On?.Invoke(metadata);
 }
 public void AddScene(SceneMetadata scene)
 {
     Game.StartRoutine(LoadSceneAsync(scene, false, false));
 }
        private IEnumerator LoadSceneAsync(SceneMetadata scene, bool showLoadingScreen = true, bool mainScene = true)
        {
            var activeSceneName = SceneManager.GetActiveScene().name.ToLower();

            if (scene == null)
            {
                yield break;
            }
            if (showLoadingScreen)
            {
                yield return(loadingScreen.ShowAsync());
            }

            if (mainScene)
            {
                var current = History.Current;
                if (current != null)
                {
                    if (current.sceneRef.IsValid())
                    {
                        current.sceneRef.UnLoadScene();
                    }
                    else
                    {
                        SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene());
                    }
                }

                History.Push(scene);
            }

            if (scene.sceneRef.IsValid())
            {
                scene.sceneRef.ReleaseAsset();
            }
            var handle = scene.sceneRef.LoadSceneAsync(LoadSceneMode.Additive, true, 0);

            while (!handle.IsDone)
            {
                // TODO: Push progress into loading screen? handle.PercentComplete
                yield return(null);
            }

            if (mainScene)
            {
                SceneManager.SetActiveScene(handle.Result.Scene);
                if (activeSceneName == "bootstrap")
                {
                    SceneManager.UnloadSceneAsync(0);
                }
            }
            while (!GameSettings.SceneSettings.sceneLoaded.IsValid())
            {
                yield return(null);
            }

            if (GameSettings.SceneSettings.sceneLoaded.OperationHandle.Result is SceneChannel casted)
            {
                casted.Raise(scene);
            }
            if (showLoadingScreen)
            {
                yield return(new WaitForSeconds(loadingScreenHideDelay));

                yield return(loadingScreen.HideAsync());
            }
        }
 public void LoadScene(SceneMetadata scene)
 {
     Game.StartRoutine(LoadSceneAsync(scene, true));
 }