|
@@ -1,120 +0,0 @@
|
|
|
-using System.Collections;
|
|
|
-using System.Collections.Generic;
|
|
|
-using UnityEngine;
|
|
|
-using Sirenix.OdinInspector;
|
|
|
-
|
|
|
-namespace KairoEngine.Utilities.Statistics
|
|
|
-{
|
|
|
- [HideMonoScript]
|
|
|
- public class Statistics : SerializedMonoBehaviour
|
|
|
- {
|
|
|
- #region Singleton
|
|
|
- private static Statistics statistics;
|
|
|
- public static Statistics instance
|
|
|
- {
|
|
|
- get {
|
|
|
- if(!statistics)
|
|
|
- {
|
|
|
- statistics = FindObjectOfType (typeof(Statistics)) as Statistics;
|
|
|
- if(!statistics)
|
|
|
- {
|
|
|
- Debug.LogError("There need to one active Statistics script on the scene.");
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
- return statistics;
|
|
|
- }
|
|
|
- }
|
|
|
- #endregion
|
|
|
-
|
|
|
- [InlineEditor(InlineEditorObjectFieldModes.Boxed)] public StatisticsLibrary db;
|
|
|
-
|
|
|
- void Awake()
|
|
|
- {
|
|
|
- if(instance != null && instance != this)
|
|
|
- {
|
|
|
- Destroy(this.gameObject);
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- void Start()
|
|
|
- {
|
|
|
- if(db == null)
|
|
|
- {
|
|
|
- Debug.LogError("Statistics component is missing the StatisticsList.\nPlease configure the statistics module in the Game config file.");
|
|
|
- return;
|
|
|
- }
|
|
|
- LoadStatistics();
|
|
|
- }
|
|
|
-
|
|
|
- void OnDestroy()
|
|
|
- {
|
|
|
- SaveStatistics();
|
|
|
- }
|
|
|
-
|
|
|
- public static StatisticData GetData(string title)
|
|
|
- {
|
|
|
- if(instance == null) return null;
|
|
|
- if(instance.db == null) return null;
|
|
|
- for (int i = 0; i < instance.db.data.Count; i++)
|
|
|
- {
|
|
|
- if(instance.db.data[i].title == title) return instance.db.data[i];
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- private void LoadStatistics()
|
|
|
- {
|
|
|
- if(instance == null) return;
|
|
|
- if(instance.db == null) return;
|
|
|
- for (int i = 0; i < instance.db.data.Count; i++)
|
|
|
- {
|
|
|
- StatisticData stat = instance.db.data[i];
|
|
|
- if(stat.persistent == false) continue;
|
|
|
- switch (stat.category)
|
|
|
- {
|
|
|
- case StatisticType.time:
|
|
|
- float time = PlayerPrefs.GetFloat(stat.title, 0f);
|
|
|
- stat.SetTime(time);
|
|
|
- break;
|
|
|
- case StatisticType.integer:
|
|
|
- int integer = PlayerPrefs.GetInt(stat.title, 0);
|
|
|
- stat.SetInteger(integer);
|
|
|
- break;
|
|
|
- case StatisticType.text:
|
|
|
- string text = PlayerPrefs.GetString(stat.title, "");
|
|
|
- stat.SetText(text);
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void SaveStatistics()
|
|
|
- {
|
|
|
- if(instance == null) return;
|
|
|
- if(instance.db == null) return;
|
|
|
- for (int i = 0; i < instance.db.data.Count; i++)
|
|
|
- {
|
|
|
- StatisticData stat = instance.db.data[i];
|
|
|
- if(stat.persistent == false) continue;
|
|
|
- switch (stat.category)
|
|
|
- {
|
|
|
- case StatisticType.time:
|
|
|
- PlayerPrefs.SetFloat(stat.title, stat.GetTime());
|
|
|
- break;
|
|
|
- case StatisticType.integer:
|
|
|
- PlayerPrefs.SetInt(stat.title, stat.GetInteger());
|
|
|
- break;
|
|
|
- case StatisticType.text:
|
|
|
- PlayerPrefs.SetString(stat.title, stat.GetText());
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|