123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- namespace KairoEngine.SteamIntegration
- {
- // Todo: Separete concerns into different scripts - manager, stats, achievements, friends, screenshoots...
- // Todo: Update stats
- // Todo: Sync stats with KairoEngine Statistics
- // Todo: Steam screenshoot
- // Todo: Send user data to Analytics system
- [HideMonoScript]
- public class SteamworksManager : MonoBehaviour
- {
- [BoxGroup("Configurations"), LabelText("Steamworks App ID")] public uint appId = 0;
- [BoxGroup("Configurations"), LabelText("Use Stats")] public bool useStats = true;
- [BoxGroup("Configurations"), LabelText("Use Achievements"), ShowIf("@useStats")] public bool useAchievements = true;
- [BoxGroup("Configurations"), ShowInInspector] public static bool showDebug = false;
- [FoldoutGroup("Stats"), InlineProperty, HideLabel, ShowIf("@useStats")]
- public SteamworksStats stats = new SteamworksStats();
- [FoldoutGroup("Achievements"), InlineProperty, HideLabel, ShowIf("@useAchievements && useStats")]
- public SteamworksAchievements achievements = new SteamworksAchievements();
- [HideInInspector] public SteamworksStatLibrary steamStatsLibray;
- public static bool hasInitialized = false;
- private string playerName;
- private void OnDestroy()
- {
- stats.Stop();
- achievements.Stop();
- Steamworks.SteamClient.Shutdown();
- hasInitialized = false;
- }
-
- private void Start()
- {
- if(appId == 0)
- {
- if(showDebug) Debug.LogWarning("Missing AppId for Steamworks manager. Please configure Steam Module in GameConfig.");
- return;
- }
- try
- {
- Steamworks.SteamClient.Init( appId, true );
- hasInitialized = true;
- playerName = Steamworks.SteamClient.Name;
- var playerSteamId = Steamworks.SteamClient.SteamId;
- if(showDebug) Debug.Log($"{playerName} ({playerSteamId})");
- if(useStats) stats.Start(steamStatsLibray);
- if(useAchievements) achievements.Start();
- RefreshStats();
- //PrintFriendsList();
- }
- catch ( System.Exception e )
- {
- Debug.LogError(e.ToString());
- }
- }
- public static bool HasInitialized()
- {
- if(hasInitialized == false)
- {
- Debug.LogWarning("Not connected to Steam");
- return false;
- }
- return true;
- }
- public static void RefreshStats()
- {
- if(!HasInitialized()) return;
- bool received = Steamworks.SteamUserStats.RequestCurrentStats();
- if(showDebug)
- {
- if(received) Debug.Log("Received current stats from Steam");
- else Debug.LogWarning("Error requesting current Steam stats");
- }
-
- }
- // [ButtonGroup("Buttons"), Button("Print Friends List")]
- // public void PrintFriendsList()
- // {
- // if(!HasInitialized()) return;
- // List<string> friends = new List<string>();
- // string friendsText = "";
- // foreach ( var player in Steamworks.SteamFriends.GetFriends() )
- // {
- // friends.Add(player.Name);
- // }
- // for (int i = 0; i < friends.Count; i++)
- // {
- // if(i < friends.Count - 1) friendsText += $"{friends[i]}, ";
- // else friendsText += $"{friends[i]}";
- // }
- // Debug.Log($"Friends of {playerName}: {friendsText}");
- // }
- [Button("Reset Player Stats & Achievements"), GUIColor("@Color.red"), PropertyTooltip("The password is RESETDATA")]
- public void ResetSteamStats(string password, bool resetAchievements = true)
- {
- if(password != "RESETDATA")
- {
- Debug.LogWarning("Incorrect password");
- return;
- }
- if(!SteamworksManager.HasInitialized()) return;
- Steamworks.SteamUserStats.ResetAll( resetAchievements ); // true = wipe achivements too
- Steamworks.SteamUserStats.StoreStats();
- SteamworksManager.RefreshStats();
- Debug.Log($"Steamworks data for player {playerName} has been reset!");
- }
- public void ShowDebug(bool debug) => showDebug = debug;
- }
- }
|