SceneManager.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using Sirenix.OdinInspector;
  6. namespace KairoEngine.Core
  7. {
  8. // Todo: Refactor class to be a Singleton
  9. // Todo: Create a public function to trigger the change scene action
  10. // Todo: The OnChangeScene event should be fired from this script after the scene change starts
  11. // Todo: Change functionality in TriggerChangeScene to use this singleton instead of the OnChangeScene event
  12. // Todo: The game location should be query to the GlobalMap
  13. /// <summary>Handles changing scenes in the game.</summary>
  14. public class SceneManager : MonoBehaviour
  15. {
  16. /// <summary>Name of the current location from the Global Map.</summary>
  17. [ReadOnly] public string currentLocation = "";
  18. private SceneChangeData nextSceneData;
  19. private float time;
  20. void OnEnable()
  21. {
  22. TransitionEvents.OnChangeScene += OnChangeScene;
  23. UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
  24. TransitionEvents.OnLocationName += SetCurrentLocation;
  25. }
  26. void OnDisable()
  27. {
  28. TransitionEvents.OnChangeScene -= OnChangeScene;
  29. UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
  30. TransitionEvents.OnLocationName -= SetCurrentLocation;
  31. }
  32. private void OnChangeScene(SceneChangeData sceneChangeData, float time)
  33. {
  34. nextSceneData = sceneChangeData;
  35. this.time = time;
  36. TransitionEvents.FadeIn(time);
  37. TransitionEvents.OnTransitionFinish += ChangeScene;
  38. }
  39. private void ChangeScene()
  40. {
  41. TransitionEvents.OnTransitionFinish -= ChangeScene;
  42. SerializationEvents.SerializeData();
  43. TransitionEvents.GetLocationName();
  44. Debug.Log("Loading scene " + nextSceneData.sceneName);
  45. UnityEngine.SceneManagement.SceneManager.LoadScene(nextSceneData.sceneName, LoadSceneMode.Single);
  46. }
  47. private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  48. {
  49. Debug.Log("Finished loading scene " + scene.name);
  50. TransitionEvents.FadeOut(time);
  51. string link = "";
  52. if(nextSceneData != null) link = nextSceneData.sceneLink;
  53. TransitionEvents.SceneStart(link);
  54. }
  55. private void SetCurrentLocation(string locationName)
  56. {
  57. currentLocation = locationName;
  58. }
  59. }
  60. }