SteamworksManager.cs 4.6 KB

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