1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using Sirenix.OdinInspector;
- namespace KairoEngine.Core
- {
- // Todo: Refactor class to be a Singleton
- // Todo: Create a public function to trigger the change scene action
- // Todo: The OnChangeScene event should be fired from this script after the scene change starts
- // Todo: Change functionality in TriggerChangeScene to use this singleton instead of the OnChangeScene event
- // Todo: The game location should be query to the GlobalMap
- /// <summary>Handles changing scenes in the game.</summary>
- public class SceneManager : MonoBehaviour
- {
- /// <summary>Name of the current location from the Global Map.</summary>
- [ReadOnly] public string currentLocation = "";
- private SceneChangeData nextSceneData;
- private float time;
-
- void OnEnable()
- {
- TransitionEvents.OnChangeScene += OnChangeScene;
- UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
- TransitionEvents.OnLocationName += SetCurrentLocation;
- }
- void OnDisable()
- {
- TransitionEvents.OnChangeScene -= OnChangeScene;
- UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
- TransitionEvents.OnLocationName -= SetCurrentLocation;
- }
- private void OnChangeScene(SceneChangeData sceneChangeData, float time)
- {
- nextSceneData = sceneChangeData;
- this.time = time;
- TransitionEvents.FadeIn(time);
- TransitionEvents.OnTransitionFinish += ChangeScene;
- }
- private void ChangeScene()
- {
- TransitionEvents.OnTransitionFinish -= ChangeScene;
- SerializationEvents.SerializeData();
- TransitionEvents.GetLocationName();
- Debug.Log("Loading scene " + nextSceneData.sceneName);
- UnityEngine.SceneManagement.SceneManager.LoadScene(nextSceneData.sceneName, LoadSceneMode.Single);
- }
- private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
- {
- Debug.Log("Finished loading scene " + scene.name);
- TransitionEvents.FadeOut(time);
- string link = "";
- if(nextSceneData != null) link = nextSceneData.sceneLink;
- TransitionEvents.SceneStart(link);
- }
- private void SetCurrentLocation(string locationName)
- {
- currentLocation = locationName;
- }
- }
- }
|