Files
tcg-client/Assets/TcgEngine/Scripts/Steam/SteamManager.cs
2025-10-09 16:51:02 +08:00

96 lines
2.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}
}