SteamworksAchievements.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. [System.Serializable]
  9. public class SteamworksAchievements
  10. {
  11. [Tooltip("Listen for internal game events for when an achievement is unlocked")]
  12. public bool listenForUnlockEvents = true;
  13. [ShowIf("@listenForUnlockEvents")] public string listenEventName = "AchievementUnlockedEvent";
  14. [Tooltip("Fire an event when an achievement is unlocked in the Steam backend")]
  15. public bool broadcastEventOnUnlock = true;
  16. [ShowIf("@broadcastEventOnUnlock")] public string broadcastEventName = "SteamAchievementUnlocked";
  17. public void Start()
  18. {
  19. if(listenForUnlockEvents) GenericEvents.StartListening(listenEventName, UnlockAchievement);
  20. if(broadcastEventOnUnlock) Steamworks.SteamUserStats.OnAchievementProgress += AchievementChanged;
  21. Timer.ExecuteRealTimeNotDisposable(1000, () => BroadcastAchievements() );
  22. }
  23. public void Stop()
  24. {
  25. if(listenForUnlockEvents) GenericEvents.StopListening(listenEventName, UnlockAchievement);
  26. if(broadcastEventOnUnlock) Steamworks.SteamUserStats.OnAchievementProgress -= AchievementChanged;
  27. }
  28. private void AchievementChanged( Steamworks.Data.Achievement ach, int currentProgress, int progress )
  29. {
  30. if ( ach.State )
  31. {
  32. Debug.Log( $"{ach.Name} WAS UNLOCKED!" );
  33. GenericEvents.Trigger(broadcastEventName, ach.Identifier);
  34. }
  35. }
  36. [Button("Unlock Achievement")]
  37. public void UnlockAchievement(string achievementId)
  38. {
  39. if(!SteamworksManager.HasInitialized()) return;
  40. var achievements = Steamworks.SteamUserStats.Achievements;
  41. foreach (var achievement in achievements)
  42. {
  43. if(achievement.Identifier == achievementId)
  44. {
  45. if(achievement.State == false)
  46. {
  47. achievement.Trigger();
  48. Steamworks.SteamUserStats.StoreStats();
  49. Debug.Log($"UNLOCKED achievement {achievementId} on Steam");
  50. }
  51. else Debug.Log($"Achievement {achievementId} already UNLOCKED on Steam");
  52. return;
  53. }
  54. }
  55. }
  56. [ButtonGroup("Buttons"),Button("Print Achievements")]
  57. public void PrintAchievementStatus()
  58. {
  59. if(!SteamworksManager.HasInitialized()) return;
  60. var achievements = Steamworks.SteamUserStats.Achievements;
  61. foreach (var achievement in achievements)
  62. {
  63. Debug.Log($"{achievement.Name} ({(achievement.State ? "UNLOCKED" : "LOCKED")} - {achievement.Identifier})");
  64. }
  65. }
  66. public void BroadcastAchievements()
  67. {
  68. if(!SteamworksManager.HasInitialized()) return;
  69. var achievements = Steamworks.SteamUserStats.Achievements;
  70. foreach (var achievement in achievements)
  71. {
  72. GenericEvents.Trigger("AchievementOverrideEvent", achievement.Identifier, achievement.State);
  73. }
  74. }
  75. }
  76. }