123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.Core;
- using Sirenix.OdinInspector;
- namespace KairoEngine.SteamIntegration
- {
- [System.Serializable]
- public class SteamworksStats
- {
- public bool broadcastStatsOnStart = true;
- public string statisticsBroadcastEvent = "SetStatisticData";
- public bool listenForStatisticsEvents = true;
- public string statisticsEvent = "StatisticDataChanged";
- [InlineEditor(InlineEditorObjectFieldModes.Boxed), PropertySpace(4,0)]
- public SteamworksStatLibrary steamStats;
- public void Start(SteamworksStatLibrary steamStatsLibrary)
- {
- if(steamStats == null) steamStats = steamStatsLibrary;
- if(broadcastStatsOnStart) BroadcastStats();
- if(listenForStatisticsEvents)
- {
- for (int i = 0; i < steamStats.library.Count; i++)
- {
- var stat = steamStats.library[i];
- if(stat.statType == SteamStatType.Integer) GenericEvents.StartListening($"{statisticsEvent}", SetStatInt);
- else if(stat.statType == SteamStatType.Float) GenericEvents.StartListeningForStringFloat($"{statisticsEvent}", SetStatFloat);
- }
- }
-
- }
- public void Stop()
- {
- Steamworks.SteamUserStats.StoreStats();
- if(listenForStatisticsEvents)
- {
- for (int i = 0; i < steamStats.library.Count; i++)
- {
- var stat = steamStats.library[i];
- if(stat.statType == SteamStatType.Integer) GenericEvents.StopListening($"{statisticsEvent}", SetStatInt);
- else if(stat.statType == SteamStatType.Float) GenericEvents.StopListeningForStringFloat($"{statisticsEvent}", SetStatFloat);
- }
- }
-
- }
- public SteamworksStatData GetSteamStatData(string statName)
- {
- for (int i = 0; i < steamStats.library.Count; i++)
- {
- if(steamStats.library[i].name == statName) return steamStats.library[i];
- }
- Debug.LogWarning($"Could not find stat with name \"{statName}\" in SteamStatLibrary");
- return null;
- }
- [Button("Print Stat")]
- public void PrintStat(string statName)
- {
- SteamworksStatData statData = GetSteamStatData(statName);
- if(statData == null) return;
- string value = "";
- if(statData.statType == SteamStatType.Integer) value += Steamworks.SteamUserStats.GetStatInt(statData.name);
- else if(statData.statType == SteamStatType.Float) value += Steamworks.SteamUserStats.GetStatFloat(statData.name);
- Debug.Log($"{statData.name}: {value}");
- }
-
- [Button("Set Stat Integer")]
- public void SetStatInt(string statName, int newValue)
- {
- SteamworksStatData statData = GetSteamStatData(statName);
- if (statData == null) return;
- if (statData.statType != SteamStatType.Integer) return;
- Steamworks.SteamUserStats.SetStat(statName, newValue);
- if(Steamworks.SteamUserStats.GetStatInt(statName) != newValue) Debug.LogWarning($"Stat \"{statName}\" does not exist on Steamworks");
- else Steamworks.SteamUserStats.StoreStats();
- }
- [Button("Set Stat Float")]
- public void SetStatFloat(string statName, float newFloatValue)
- {
- SteamworksStatData statData = GetSteamStatData(statName);
- if (statData == null) return;
- if (statData.statType != SteamStatType.Float) return;
- Steamworks.SteamUserStats.SetStat(statName, newFloatValue);
- if(Steamworks.SteamUserStats.GetStatFloat(statName) != newFloatValue) Debug.LogWarning($"Stat \"{statName}\" does not exist on Steamworks");
- else Steamworks.SteamUserStats.StoreStats();
- }
- public void BroadcastStats()
- {
- for (int i = 0; i < steamStats.library.Count; i++)
- {
- var stat = steamStats.library[i];
- if(stat.statType == SteamStatType.Integer)
- {
- int value = Steamworks.SteamUserStats.GetStatInt(stat.name);
- GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value);
- }
- else if(stat.statType == SteamStatType.Float)
- {
- float value = Steamworks.SteamUserStats.GetStatFloat(stat.name);
- GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value);
- }
- }
- }
- }
- }
|