12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- namespace KairoEngine.SteamIntegration
- {
- [System.Serializable]
- public class SteamworksAchievements
- {
- [Tooltip("Listen for internal game events for when an achievement is unlocked")]
- public bool listenForUnlockEvents = true;
- [ShowIf("@listenForUnlockEvents")] public string listenEventName = "AchievementUnlockedEvent";
- [Tooltip("Fire an event when an achievement is unlocked in the Steam backend")]
- public bool broadcastEventOnUnlock = true;
- [ShowIf("@broadcastEventOnUnlock")] public string broadcastEventName = "AchievementOverrideEvent";
- public void Start()
- {
- if(listenForUnlockEvents) GenericEvents.StartListening(listenEventName, UnlockAchievement);
- if(broadcastEventOnUnlock) Steamworks.SteamUserStats.OnAchievementProgress += AchievementChanged;
- Timer.ExecuteRealTimeNotDisposable(1000, () => BroadcastAchievements() );
- }
- public void Stop()
- {
- if(listenForUnlockEvents) GenericEvents.StopListening(listenEventName, UnlockAchievement);
- if(broadcastEventOnUnlock) Steamworks.SteamUserStats.OnAchievementProgress -= AchievementChanged;
- }
- private void AchievementChanged( Steamworks.Data.Achievement ach, int currentProgress, int progress )
- {
- if ( ach.State )
- {
- Debug.Log( $"{ach.Name} WAS UNLOCKED!" );
- GenericEvents.Trigger(broadcastEventName, ach.Identifier, ach.State);
- }
- }
- [Button("Unlock Achievement")]
- public void UnlockAchievement(string achievementId)
- {
- if(!SteamworksManager.HasInitialized()) return;
- var achievements = Steamworks.SteamUserStats.Achievements;
- foreach (var achievement in achievements)
- {
- if(achievement.Identifier == achievementId)
- {
- if(achievement.State == false)
- {
- achievement.Trigger();
- Steamworks.SteamUserStats.StoreStats();
- Debug.Log($"UNLOCKED achievement {achievementId} on Steam");
- }
- else Debug.Log($"Achievement {achievementId} already UNLOCKED on Steam");
- return;
- }
- }
- }
- [ButtonGroup("Buttons"),Button("Print Achievements")]
- public void PrintAchievementStatus()
- {
- if(!SteamworksManager.HasInitialized()) return;
- var achievements = Steamworks.SteamUserStats.Achievements;
- foreach (var achievement in achievements)
- {
- Debug.Log($"{achievement.Name} ({(achievement.State ? "UNLOCKED" : "LOCKED")} - {achievement.Identifier})");
- }
- }
- public void BroadcastAchievements()
- {
- if(!SteamworksManager.HasInitialized()) return;
- var achievements = Steamworks.SteamUserStats.Achievements;
- foreach (var achievement in achievements)
- {
- GenericEvents.Trigger("AchievementOverrideEvent", achievement.Identifier, achievement.State);
- }
- }
- }
- }
|