1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 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 = "SteamAchievementUnlocked";
- public void Start()
- {
- if(listenForUnlockEvents) GenericEvents.StartListening(listenEventName, UnlockAchievement);
- if(broadcastEventOnUnlock) Steamworks.SteamUserStats.OnAchievementProgress += AchievementChanged;
- }
- 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);
- }
- }
- [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})");
- }
- }
- }
- }
|