Statistics.cs 5.4 KB

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