SteamworksManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. public static bool hasInitialized = false;
  24. private string playerName;
  25. private void OnDestroy()
  26. {
  27. stats.Stop();
  28. achievements.Stop();
  29. Steamworks.SteamClient.Shutdown();
  30. hasInitialized = false;
  31. }
  32. private void Start()
  33. {
  34. if(appId == 0)
  35. {
  36. if(showDebug) Debug.LogWarning("Missing AppId for Steamworks manager. Please configure Steam Module in GameConfig.");
  37. return;
  38. }
  39. try
  40. {
  41. Steamworks.SteamClient.Init( appId, true );
  42. hasInitialized = true;
  43. playerName = Steamworks.SteamClient.Name;
  44. var playerSteamId = Steamworks.SteamClient.SteamId;
  45. if(showDebug) Debug.Log($"{playerName} ({playerSteamId})");
  46. if(useStats) stats.Start();
  47. if(useAchievements) achievements.Start();
  48. RefreshStats();
  49. //PrintFriendsList();
  50. }
  51. catch ( System.Exception e )
  52. {
  53. Debug.LogError(e.ToString());
  54. }
  55. }
  56. public static bool HasInitialized()
  57. {
  58. if(hasInitialized == false)
  59. {
  60. Debug.LogWarning("Not connected to Steam");
  61. return false;
  62. }
  63. return true;
  64. }
  65. public static void RefreshStats()
  66. {
  67. if(!HasInitialized()) return;
  68. bool received = Steamworks.SteamUserStats.RequestCurrentStats();
  69. if(showDebug)
  70. {
  71. if(received) Debug.Log("Received current stats from Steam");
  72. else Debug.LogWarning("Error requesting current Steam stats");
  73. }
  74. }
  75. // [ButtonGroup("Buttons"), Button("Print Friends List")]
  76. // public void PrintFriendsList()
  77. // {
  78. // if(!HasInitialized()) return;
  79. // List<string> friends = new List<string>();
  80. // string friendsText = "";
  81. // foreach ( var player in Steamworks.SteamFriends.GetFriends() )
  82. // {
  83. // friends.Add(player.Name);
  84. // }
  85. // for (int i = 0; i < friends.Count; i++)
  86. // {
  87. // if(i < friends.Count - 1) friendsText += $"{friends[i]}, ";
  88. // else friendsText += $"{friends[i]}";
  89. // }
  90. // Debug.Log($"Friends of {playerName}: {friendsText}");
  91. // }
  92. [Button("Reset Player Stats & Achievements"), GUIColor("@Color.red"), PropertyTooltip("The password is RESETDATA")]
  93. public void ResetSteamStats(string password, bool resetAchievements = true)
  94. {
  95. if(password != "RESETDATA")
  96. {
  97. Debug.LogWarning("Incorrect password");
  98. return;
  99. }
  100. if(!SteamworksManager.HasInitialized()) return;
  101. Steamworks.SteamUserStats.ResetAll( resetAchievements ); // true = wipe achivements too
  102. Steamworks.SteamUserStats.StoreStats();
  103. SteamworksManager.RefreshStats();
  104. Debug.Log($"Steamworks data for player {playerName} has been reset!");
  105. }
  106. public void ShowDebug(bool debug) => showDebug = debug;
  107. }
  108. }