Statistics.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.Utilities.Statistics
  6. {
  7. [HideMonoScript]
  8. public class Statistics : SerializedMonoBehaviour
  9. {
  10. #region Singleton
  11. private static Statistics statistics;
  12. public static Statistics instance
  13. {
  14. get {
  15. if(!statistics)
  16. {
  17. statistics = FindObjectOfType (typeof(Statistics)) as Statistics;
  18. if(!statistics)
  19. {
  20. Debug.LogError("There need to one active Statistics script on the scene.");
  21. return null;
  22. }
  23. }
  24. return statistics;
  25. }
  26. }
  27. #endregion
  28. [InlineEditor(InlineEditorObjectFieldModes.Boxed)] public StatisticsLibrary db;
  29. void Awake()
  30. {
  31. if(instance != null && instance != this)
  32. {
  33. Destroy(this.gameObject);
  34. return;
  35. }
  36. }
  37. void Start()
  38. {
  39. if(db == null)
  40. {
  41. Debug.LogError("Statistics component is missing the StatisticsList.\nPlease configure the statistics module in the Game config file.");
  42. return;
  43. }
  44. LoadStatistics();
  45. }
  46. void OnDestroy()
  47. {
  48. SaveStatistics();
  49. }
  50. public static StatisticData GetData(string title)
  51. {
  52. if(instance == null) return null;
  53. if(instance.db == null) return null;
  54. for (int i = 0; i < instance.db.data.Count; i++)
  55. {
  56. if(instance.db.data[i].title == title) return instance.db.data[i];
  57. }
  58. return null;
  59. }
  60. private void LoadStatistics()
  61. {
  62. if(instance == null) return;
  63. if(instance.db == null) return;
  64. for (int i = 0; i < instance.db.data.Count; i++)
  65. {
  66. StatisticData stat = instance.db.data[i];
  67. if(stat.persistent == false) continue;
  68. switch (stat.category)
  69. {
  70. case StatisticType.time:
  71. float time = PlayerPrefs.GetFloat(stat.title, 0f);
  72. stat.SetTime(time);
  73. break;
  74. case StatisticType.integer:
  75. int integer = PlayerPrefs.GetInt(stat.title, 0);
  76. stat.SetInteger(integer);
  77. break;
  78. case StatisticType.text:
  79. string text = PlayerPrefs.GetString(stat.title, "");
  80. stat.SetText(text);
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. }
  87. private void SaveStatistics()
  88. {
  89. if(instance == null) return;
  90. if(instance.db == null) return;
  91. for (int i = 0; i < instance.db.data.Count; i++)
  92. {
  93. StatisticData stat = instance.db.data[i];
  94. if(stat.persistent == false) continue;
  95. switch (stat.category)
  96. {
  97. case StatisticType.time:
  98. PlayerPrefs.SetFloat(stat.title, stat.GetTime());
  99. break;
  100. case StatisticType.integer:
  101. PlayerPrefs.SetInt(stat.title, stat.GetInteger());
  102. break;
  103. case StatisticType.text:
  104. PlayerPrefs.SetString(stat.title, stat.GetText());
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. }