Example #1
0
        private void SceneManagerOnSceneLoaded(Scene scene, LoadSceneMode mode)
        {
            if (scene.name == MenuSceneName)
            {
                if (_resetNoFail)
                {
                    var resultsViewController =
                        Resources.FindObjectsOfTypeAll <ResultsViewController>().FirstOrDefault();
                    if (resultsViewController != null)
                    {
                        resultsViewController.continueButtonPressedEvent +=
                            ResultsViewControllerOnContinueButtonPressedEvent;
                    }
                }

                if (SettingsObject != null)
                {
                    return;
                }

                var volumeSettings = Resources.FindObjectsOfTypeAll <VolumeSettingsController>().FirstOrDefault();

                if (volumeSettings == null)
                {
                    return;
                }

                volumeSettings.gameObject.SetActive(false);
                SettingsObject = Object.Instantiate(volumeSettings.gameObject);
                SettingsObject.SetActive(false);
                volumeSettings.gameObject.SetActive(true);

                if (SettingsObject == null)
                {
                    return;
                }

                var volume = SettingsObject.GetComponent <VolumeSettingsController>();
                ReflectionUtil.CopyComponent(volume, typeof(IncDecSettingsController),
                                             typeof(SpeedSettingsController), SettingsObject);
                Object.DestroyImmediate(volume);

                SettingsObject.GetComponentInChildren <TMP_Text>().text = "SPEED";
                Object.DontDestroyOnLoad(SettingsObject);
            }
            else if (scene.name == GameSceneName)
            {
                CustomEffectPoolsInstaller customEffectPoolsInstaller = null;
                var effectPoolsInstaller = Resources.FindObjectsOfTypeAll <EffectPoolsInstaller>().FirstOrDefault();
                if (effectPoolsInstaller != null)
                {
                    customEffectPoolsInstaller = (CustomEffectPoolsInstaller)ReflectionUtil.CopyComponent(effectPoolsInstaller,
                                                                                                          typeof(EffectPoolsInstaller), typeof(CustomEffectPoolsInstaller), effectPoolsInstaller.gameObject);
                }

                SceneContext          sceneContext          = null;
                SceneDecoratorContext sceneDecoratorContext = null;

                foreach (var gameObject in scene.GetRootGameObjects())
                {
                    if (sceneContext == null)
                    {
                        sceneContext = gameObject.GetComponentInChildren <SceneContext>(true);
                    }
                }

                foreach (var gameObject in SceneManager.GetSceneByName(ContextSceneName).GetRootGameObjects())
                {
                    if (sceneDecoratorContext == null)
                    {
                        sceneDecoratorContext = gameObject.GetComponentInChildren <SceneDecoratorContext>(true);
                    }
                }


                if (sceneContext != null && sceneDecoratorContext != null)
                {
                    var prop           = typeof(Context).GetField("_installers", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    var installersList = (List <MonoInstaller>)prop.GetValue(sceneDecoratorContext);
                    installersList.Remove(effectPoolsInstaller);
                    Object.DestroyImmediate(effectPoolsInstaller);
                    installersList.Add(customEffectPoolsInstaller);
                }

                if (_mainGameSceneSetupData == null)
                {
                    _mainGameSceneSetupData = Resources.FindObjectsOfTypeAll <MainGameSceneSetupData>().FirstOrDefault();
                    if (_mainGameSceneSetupData == null)
                    {
                        return;
                    }
                    _mainGameSceneSetupData.didFinishEvent += MainGameSceneSetupDataOnDidFinishEvent;
                }

                if (_lastLevelId != _mainGameSceneSetupData.difficultyLevel.level.levelID &&
                    !string.IsNullOrEmpty(_lastLevelId))
                {
                    PlayingNewSong      = true;
                    HasTimeScaleChanged = false;
                    TimeScale           = 1;
                    _lastLevelId        = _mainGameSceneSetupData.difficultyLevel.level.levelID;
                }
                else
                {
                    PlayingNewSong = false;
                }

                if (IsEqualToOne(TimeScale))
                {
                    HasTimeScaleChanged = false;
                }

                _lastLevelId = _mainGameSceneSetupData.difficultyLevel.level.levelID;

                AudioTimeSync = Resources.FindObjectsOfTypeAll <AudioTimeSyncController>().FirstOrDefault();
                _songAudio    = AudioTimeSync.GetPrivateField <AudioSource>("_audioSource");
                NoFail        = !_mainGameSceneSetupData.gameplayOptions.validForScoreUse;

                if (!NoFail)
                {
                    TimeScale = Mathf.Clamp(TimeScale, 1, MaxSize);
                }

                var canvas = Resources.FindObjectsOfTypeAll <HorizontalLayoutGroup>()
                             .FirstOrDefault(x => x.name == "Buttons")
                             ?.transform.parent;

                if (canvas == null)
                {
                    return;
                }

                _uiElementsCreator = canvas.gameObject.AddComponent <UIElementsCreator>();
                _uiElementsCreator.ValueChangedEvent += UIElementsCreatorOnValueChangedEvent;
                _uiElementsCreator.Init();
                TimeScale = TimeScale;
            }
        }