SteamworksManager.cs 4.7 KB

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