Exemplo n.º 1
0
        public SceneManager()
        {
            var        subscriber       = new Subscriber();
            var        currentSceneName = "";
            GameObject loading          = null;

            // Scene Loaded
            subscriber.Subscribe(EventTopics.SceneAwake, () => currentSceneName = SceneManagement.GetActiveScene().name);

            // Scene Change
            subscriber.Subscribe <string>(EventTopics.SceneChange, (triggerName) =>
            {
                SceneConfig scene = config.Scenes.FirstOrDefault(x => String.Equals(x.Name, currentSceneName, StringComparison.InvariantCultureIgnoreCase));
                if (scene == null)
                {
                    throw new Exception($"{this} no configuration for the current scene: {currentSceneName}");
                }

                SceneNext next = scene.LinkTo.FirstOrDefault(x => String.Equals(x.TriggerName, triggerName, StringComparison.InvariantCultureIgnoreCase));
                if (next == null)
                {
                    throw new Exception($"{this} no configuration for this trigger scene: {currentSceneName}, trigger: {triggerName}");
                }

                SceneConfig nextConfig = config.Scenes.FirstOrDefault(x => String.Equals(x.Name, next.SceneName.Name, StringComparison.InvariantCultureIgnoreCase));
                if (nextConfig == null)
                {
                    throw new Exception($"{this} no configuration for this scene: {next.SceneName.Name}");
                }

                if (nextConfig.Async)
                {
                    SceneManagement.LoadSceneAsync(next.SceneName.Name, UnityEngine.SceneManagement.LoadSceneMode.Single);
                }
                else
                {
                    SceneManagement.LoadScene(next.SceneName.Name);
                }
            });

            // Scene Loading
            subscriber.Subscribe(EventTopics.SceneLoading, () =>
            {
                loading = Object.Instantiate(config.LoadingPrefab);
                loading.GetComponent <CanvasGroup>().alpha = 1;
            });

            subscriber.Subscribe(EventTopics.SceneLoaded, () =>
            {
                loading.GetComponent <CanvasGroup>().alpha = 0;
                Object.Destroy(loading);
                loading = null;
            });

            // Additive Scene
            subscriber.Subscribe <string>(EventTopics.SceneLoadAdditive, (name) =>
            {
                SceneConfig scene = config.Scenes.FirstOrDefault(x => x.Name.ToLowerInvariant() == currentSceneName.ToLowerInvariant());
                if (scene == null)
                {
                    throw new Exception($"{this} no configuration for the current scene: {currentSceneName}");
                }

                SceneAdditiveConfig additive = scene.Additives.FirstOrDefault(x => x.Name.ToLowerInvariant() == name.ToLowerInvariant());
                if (additive == null)
                {
                    throw new Exception($"{this} no configuration for this additive scene: {currentSceneName}, additive: {name}");
                }

                currentScene.StartCoroutine(LoadAdditiveScene(additive.Name));
            });

            subscriber.Subscribe <string>(EventTopics.SceneLoadAdditiveAwake, (name) =>
            {
                // if the additive scene is opened directly
                if (currentScene == null)
                {
                    return;
                }

                SceneConfig scene = config.Scenes.FirstOrDefault(x => x.Name.ToLowerInvariant() == currentSceneName.ToLowerInvariant());
                if (scene == null)
                {
                    throw new Exception($"{this} no configuration for the current scene: {currentSceneName}");
                }

                SceneAdditiveConfig additive = scene.Additives.FirstOrDefault(x => x.Name.ToLowerInvariant() == name.ToLowerInvariant());
                if (additive == null)
                {
                    throw new Exception($"{this} no configuration for this additive scene: {currentSceneName}, additive: {name}");
                }

                if (additive.ActiveOnLoad)
                {
                    SceneManagement.SetActiveScene(SceneManagement.GetSceneByName(additive.Name));
                }
            });
        }