增加steam
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX
|
||||
#define STEAMWORKS_ENABLED
|
||||
#endif
|
||||
|
||||
#if STEAMWORKS_ENABLED
|
||||
using Steamworks;
|
||||
#endif
|
||||
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TcgEngine; // 添加TcgEngine命名空间引用以访问SteamCurrencyManager
|
||||
|
||||
namespace TcgEngine.UI
|
||||
{
|
||||
@@ -36,12 +44,141 @@ namespace TcgEngine.UI
|
||||
private bool clicked = false;
|
||||
|
||||
private static LoginMenu instance;
|
||||
|
||||
|
||||
#if STEAMWORKS_ENABLED
|
||||
private Callback<GameOverlayActivated_t> m_GameOverlayActivated;
|
||||
private Callback<UserStatsReceived_t> m_UserStatsReceived;
|
||||
private Callback<UserStatsStored_t> m_UserStatsStored;
|
||||
private Callback<SteamServersConnected_t> m_SteamServersConnected;
|
||||
private Callback<SteamServersDisconnected_t> m_SteamServersDisconnected;
|
||||
private Callback<ValidateAuthTicketResponse_t> m_ValidateAuthTicketResponse;
|
||||
#endif
|
||||
void Awake()
|
||||
{
|
||||
instance = this;
|
||||
#if STEAMWORKS_ENABLED
|
||||
// 确保SteamManager存在
|
||||
if (SteamManager.Initialized)
|
||||
{
|
||||
// 注册 Steam 回调
|
||||
m_GameOverlayActivated = Callback<GameOverlayActivated_t>.Create(OnGameOverlayActivated);
|
||||
m_UserStatsReceived = Callback<UserStatsReceived_t>.Create(OnUserStatsReceived);
|
||||
m_UserStatsStored = Callback<UserStatsStored_t>.Create(OnUserStatsStored);
|
||||
m_SteamServersConnected = Callback<SteamServersConnected_t>.Create(OnSteamServersConnected);
|
||||
m_SteamServersDisconnected = Callback<SteamServersDisconnected_t>.Create(OnSteamServersDisconnected);
|
||||
m_ValidateAuthTicketResponse = Callback<ValidateAuthTicketResponse_t>.Create(OnValidateAuthTicketResponse);
|
||||
|
||||
Debug.Log("Steam initialized through SteamManager");
|
||||
}
|
||||
// 初始化Steam货币系统
|
||||
if (SteamCurrencyManager.instance == null)
|
||||
{
|
||||
GameObject steamCurrencyGO = new GameObject("SteamCurrencyManager");
|
||||
SteamCurrencyManager currencyManager = steamCurrencyGO.AddComponent<SteamCurrencyManager>();
|
||||
currencyManager.InitializeSteamCurrency();
|
||||
}
|
||||
else
|
||||
{
|
||||
SteamCurrencyManager.instance.InitializeSteamCurrency();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if STEAMWORKS_ENABLED
|
||||
private void OnGameOverlayActivated(GameOverlayActivated_t pCallback)
|
||||
{
|
||||
if (pCallback.m_bActive != 0)
|
||||
{
|
||||
Debug.Log("Steam overlay now active");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Steam overlay now inactive");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnUserStatsReceived(UserStatsReceived_t pCallback)
|
||||
{
|
||||
if ((ulong)pCallback.m_nGameID == SteamUtils.GetAppID().m_AppId && pCallback.m_eResult == EResult.k_EResultOK)
|
||||
{
|
||||
Debug.Log("Received stats and achievements from Steam");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Failed to receive stats and achievements from Steam");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnUserStatsStored(UserStatsStored_t pCallback)
|
||||
{
|
||||
if (pCallback.m_eResult == EResult.k_EResultOK)
|
||||
{
|
||||
Debug.Log("Stored stats and achievements to Steam");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Failed to store stats and achievements to Steam");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSteamServersConnected(SteamServersConnected_t pCallback)
|
||||
{
|
||||
Debug.Log("Connected to Steam servers");
|
||||
}
|
||||
|
||||
private void OnSteamServersDisconnected(SteamServersDisconnected_t pCallback)
|
||||
{
|
||||
Debug.Log("Disconnected from Steam servers");
|
||||
}
|
||||
|
||||
private void OnValidateAuthTicketResponse(ValidateAuthTicketResponse_t pCallback)
|
||||
{
|
||||
Debug.Log("Auth ticket response received");
|
||||
}
|
||||
|
||||
public void UnlockAchievement(string achievementId)
|
||||
{
|
||||
if (SteamManager.Initialized)
|
||||
{
|
||||
bool ret = SteamUserStats.SetAchievement(achievementId);
|
||||
if (ret)
|
||||
{
|
||||
SteamUserStats.StoreStats();
|
||||
Debug.Log("Unlocked achievement: " + achievementId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateStat(string statName, int value)
|
||||
{
|
||||
if (SteamManager.Initialized)
|
||||
{
|
||||
bool ret = SteamUserStats.SetStat(statName, value);
|
||||
if (ret)
|
||||
{
|
||||
SteamUserStats.StoreStats();
|
||||
Debug.Log($"Updated stat {statName} to {value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string GetSteamUserName()
|
||||
{
|
||||
if (SteamManager.Initialized && SteamUser.GetSteamID().IsValid())
|
||||
{
|
||||
return SteamFriends.GetPersonaName();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public ulong GetSteamID()
|
||||
{
|
||||
if (SteamManager.Initialized && SteamUser.GetSteamID().IsValid())
|
||||
{
|
||||
return SteamUser.GetSteamID().m_SteamID;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
private void Start()
|
||||
{
|
||||
AudioTool.Get().PlayMusic("music", music);
|
||||
@@ -117,6 +254,27 @@ namespace TcgEngine.UI
|
||||
{
|
||||
SceneNav.GoTo("Menu");
|
||||
}
|
||||
#if STEAMWORKS_ENABLED
|
||||
else if (SteamManager.Initialized && SteamUser.GetSteamID().IsValid())
|
||||
{
|
||||
string steamID = SteamUser.GetSteamID().ToString();
|
||||
string personaName = SteamFriends.GetPersonaName();
|
||||
Debug.Log("Steam User: " + personaName+"("+steamID+")");
|
||||
|
||||
bool success2 = await Authenticator.Get().SteamLogin(personaName+"@shenynet.com", personaName, steamID+"@shenynet.com");
|
||||
if (success2)
|
||||
{
|
||||
Debug.Log("Steam User Success: " + personaName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Steam User Failure: " + Authenticator.Get().GetError());
|
||||
}
|
||||
// 请求当前用户的统计信息
|
||||
SteamUserStats.RequestUserStats(SteamUser.GetSteamID());
|
||||
SceneNav.GoTo("Menu");
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
login_panel.Show();
|
||||
|
||||
@@ -274,6 +274,22 @@ namespace TcgEngine.UI
|
||||
{
|
||||
SettingsPanel.Get().Show();
|
||||
}
|
||||
|
||||
public void OnClickRecharge()
|
||||
{
|
||||
#if STEAMWORKS_ENABLED
|
||||
if (SteamCurrencyManager.instance != null && SteamCurrencyManager.instance.IsInitialized())
|
||||
{
|
||||
SteamCurrencyManager.instance.PurchaseGoldPackage(100, 99);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Steam currency system not initialized");
|
||||
}
|
||||
#else
|
||||
Debug.LogWarning("Steamworks not enabled");
|
||||
#endif
|
||||
}
|
||||
|
||||
public void FadeToScene(string scene)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user