DataManager.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. namespace KairoEngine.CharacterSystem
  7. {
  8. /// <summary>
  9. /// Singleton that stores serialized data from scenes.
  10. /// </summary>
  11. public class DataManager : SerializedMonoBehaviour
  12. {
  13. #region Singleton
  14. private static DataManager dataManager;
  15. public static DataManager instance
  16. {
  17. get {
  18. if(!dataManager)
  19. {
  20. dataManager = FindObjectOfType (typeof(DataManager)) as DataManager;
  21. if(!dataManager)
  22. {
  23. //Debug.LogError("There need to one active DataManager script on the scene.");
  24. return null;
  25. }
  26. }
  27. return dataManager;
  28. }
  29. }
  30. #endregion
  31. public List<DataScene> scenesData = new List<DataScene>();
  32. public DataScene currentSceneData;
  33. void Awake()
  34. {
  35. //DontDestroyOnLoad(gameObject);
  36. }
  37. void OnEnable()
  38. {
  39. TransitionEvents.OnLocationName += ChangeCurrentSceneData;
  40. }
  41. void Ondisable()
  42. {
  43. TransitionEvents.OnLocationName += ChangeCurrentSceneData;
  44. }
  45. /// <summary>
  46. /// Function executed after OnLocation event, for changing the current sceneData object
  47. /// </summary>
  48. /// <param name="locationName">The name of the new location</param>
  49. private void ChangeCurrentSceneData(string locationName)
  50. {
  51. currentSceneData = GetSceneDataByname(locationName);
  52. if(currentSceneData == null)
  53. {
  54. currentSceneData = new DataScene();
  55. currentSceneData.locationName = locationName;
  56. scenesData.Add(currentSceneData);
  57. }
  58. currentSceneData.visited += 1;
  59. }
  60. /// <summary>
  61. /// Find a DataScene with using a location name.
  62. /// </summary>
  63. /// <param name="locationName">The name of the location to be searched</param>
  64. /// <returns>Requested DataScene object</returns>
  65. public DataScene GetSceneDataByname(string locationName)
  66. {
  67. DataScene dataScene = null;
  68. for (int i = 0; i < scenesData.Count; i++)
  69. {
  70. if(scenesData[i].locationName == locationName)
  71. {
  72. dataScene = scenesData[i];
  73. break;
  74. }
  75. }
  76. return dataScene;
  77. }
  78. }
  79. }