SteamworksManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. namespace KairoEngine.SteamIntegration
  7. {
  8. [HideMonoScript]
  9. public class SteamworksManager : MonoBehaviour
  10. {
  11. [BoxGroup("Configurations"), LabelText("Steamworks App ID")] public uint appId = 0;
  12. [BoxGroup("Configurations"), LabelText("Use Stats")] public bool useStats = true;
  13. [BoxGroup("Configurations"), LabelText("Use Achievements"), ShowIf("@useStats")] public bool useAchievements = true;
  14. [BoxGroup("Configurations"), ShowInInspector] public static bool showDebug = false;
  15. [FoldoutGroup("Stats"), InlineProperty, HideLabel, ShowIf("@useStats")]
  16. public SteamworksStats stats = new SteamworksStats();
  17. [FoldoutGroup("Achievements"), InlineProperty, HideLabel, ShowIf("@useAchievements && useStats")]
  18. public SteamworksAchievements achievements = new SteamworksAchievements();
  19. [HideInInspector] public SteamworksStatLibrary steamStatsLibray;
  20. public static bool hasInitialized = false;
  21. private string playerName;
  22. private void OnDestroy()
  23. {
  24. stats.Stop();
  25. achievements.Stop();
  26. Steamworks.SteamClient.Shutdown();
  27. hasInitialized = false;
  28. }
  29. private void Start()
  30. {
  31. if(appId == 0)
  32. {
  33. if(showDebug) Debug.LogWarning("Missing AppId for Steamworks manager. Please configure Steam Module in GameConfig.");
  34. return;
  35. }
  36. try
  37. {
  38. Steamworks.SteamClient.Init( appId, true );
  39. hasInitialized = true;
  40. RefreshStats();
  41. playerName = Steamworks.SteamClient.Name;
  42. var playerSteamId = Steamworks.SteamClient.SteamId;
  43. if(showDebug) Debug.Log($"{playerName} ({playerSteamId})");
  44. Timer.ExecuteRealTimeNotDisposable(1000, () => {
  45. if(useStats) stats.Start(steamStatsLibray, showDebug);
  46. if(useAchievements) achievements.Start();
  47. });
  48. //PrintFriendsList();
  49. }
  50. catch ( System.Exception e )
  51. {
  52. Debug.LogError(e.ToString());
  53. }
  54. }
  55. public static bool HasInitialized()
  56. {
  57. if(hasInitialized == false)
  58. {
  59. Debug.LogWarning("Not connected to Steam");
  60. return false;
  61. }
  62. return true;
  63. }
  64. public static void RefreshStats()
  65. {
  66. if(!HasInitialized()) return;
  67. bool received = Steamworks.SteamUserStats.RequestCurrentStats();
  68. if(showDebug)
  69. {
  70. if(received) Debug.Log("Received current stats from Steam");
  71. else Debug.LogWarning("Error requesting current Steam stats");
  72. }
  73. }
  74. // [ButtonGroup("Buttons"), Button("Print Friends List")]
  75. // public void PrintFriendsList()
  76. // {
  77. // if(!HasInitialized()) return;
  78. // List<string> friends = new List<string>();
  79. // string friendsText = "";
  80. // foreach ( var player in Steamworks.SteamFriends.GetFriends() )
  81. // {
  82. // friends.Add(player.Name);
  83. // }
  84. // for (int i = 0; i < friends.Count; i++)
  85. // {
  86. // if(i < friends.Count - 1) friendsText += $"{friends[i]}, ";
  87. // else friendsText += $"{friends[i]}";
  88. // }
  89. // Debug.Log($"Friends of {playerName}: {friendsText}");
  90. // }
  91. [Button("Reset Player Stats & Achievements"), GUIColor("@Color.red"), PropertyTooltip("The password is RESETDATA")]
  92. public void ResetSteamStats(string password, bool resetAchievements = true)
  93. {
  94. if(password != "RESETDATA")
  95. {
  96. Debug.LogWarning("Incorrect password");
  97. return;
  98. }
  99. if(!SteamworksManager.HasInitialized()) return;
  100. Steamworks.SteamUserStats.ResetAll( resetAchievements ); // true = wipe achivements too
  101. Steamworks.SteamUserStats.StoreStats();
  102. SteamworksManager.RefreshStats();
  103. Debug.Log($"Steamworks data for player {playerName} has been reset!");
  104. }
  105. public void ShowDebug(bool debug) => showDebug = debug;
  106. }
  107. }