using UnityEngine; namespace TcgEngine { [DisallowMultipleComponent] public class SteamManager : MonoBehaviour { #if UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX private static SteamManager s_instance; private bool m_bInitialized = false; public static bool Initialized { get { return s_instance != null && s_instance.m_bInitialized; } } void Awake() { // 单例检查 if (s_instance != null) { Destroy(gameObject); return; } s_instance = this; DontDestroyOnLoad(gameObject); try { #if STEAMWORKS_NET // 尝试加载 Steamworks.NET if (Steamworks.SteamAPI.RestartAppIfNecessary(new Steamworks.AppId_t(480))) { Debug.LogWarning("[SteamManager] Steam 重启请求,退出当前实例。"); Application.Quit(); return; } m_bInitialized = Steamworks.SteamAPI.Init(); if (m_bInitialized) { Debug.Log("[SteamManager] Steam 初始化成功。"); } else { Debug.LogWarning("[SteamManager] SteamAPI.Init() 失败,将以离线模式运行。"); } #else // 没有定义 STEAMWORKS_NET 宏 Debug.Log("[SteamManager] 未启用 Steamworks.NET,跳过初始化。"); #endif } catch (System.DllNotFoundException e) { Debug.LogWarning("[SteamManager] 未找到 steam_api64.dll,以离线模式运行。\n" + e.Message); m_bInitialized = false; } catch (System.Exception e) { Debug.LogError("[SteamManager] 初始化异常,将以离线模式运行。\n" + e); m_bInitialized = false; } } void Update() { #if STEAMWORKS_NET if (m_bInitialized) Steamworks.SteamAPI.RunCallbacks(); #endif } void OnDestroy() { if (s_instance != this) return; #if STEAMWORKS_NET if (m_bInitialized) { Steamworks.SteamAPI.Shutdown(); Debug.Log("[SteamManager] Steam 已正常关闭。"); } #endif s_instance = null; } #else // 非桌面平台(例如WebGL/Android/iOS) public static bool Initialized => false; #endif } }