#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX #define STEAMWORKS_ENABLED #endif #if STEAMWORKS_ENABLED using Steamworks; #endif using UnityEngine; namespace TcgEngine { /// /// Can send requests and receive responses /// public class SteamCurrencyManager : MonoBehaviour { #if STEAMWORKS_ENABLED private bool m_bInitialized = false; private Callback m_SteamInventoryResultReady; private Callback m_SteamInventoryFullUpdate; private Callback m_SteamInventoryDefinitionUpdate; public static SteamCurrencyManager instance; void Awake() { if (instance == null) { instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); return; } } public void InitializeSteamCurrency() { // 使用SteamManager来检查Steam是否已初始化 if (!SteamManager.Initialized) { Debug.LogError("Steam not initialized through SteamManager"); return; } m_bInitialized = true; // 注册回调 m_SteamInventoryResultReady = Callback.Create(OnSteamInventoryResultReady); m_SteamInventoryFullUpdate = Callback.Create(OnSteamInventoryFullUpdate); m_SteamInventoryDefinitionUpdate = Callback.Create(OnSteamInventoryDefinitionUpdate); Debug.Log("Steam Currency Manager initialized"); } // 获取用户库存 public void GetUserInventory() { if (!m_bInitialized || !SteamManager.Initialized) return; SteamInventoryResult_t resultHandle; if (SteamInventory.GetAllItems(out resultHandle)) { Debug.Log("Getting user inventory..."); } else { Debug.LogError("Failed to get user inventory"); } } // 购买金币包 public void PurchaseGoldPackage(int goldAmount, int priceInCents) { if (!m_bInitialized || !SteamManager.Initialized) { Debug.LogError("Steam not initialized"); return; } // 启动购买流程 SteamItemDef_t[] itemDefs = new SteamItemDef_t[] { new SteamItemDef_t(GetItemDefIDForGold(goldAmount)) }; uint[] quantities = new uint[] { 1 }; SteamAPICall_t callHandle = SteamInventory.StartPurchase(itemDefs, quantities, 1); if (callHandle != SteamAPICall_t.Invalid) { Debug.Log("Purchase request sent"); } else { Debug.LogError("Failed to start purchase"); } } // 根据金币数量获取对应的物品ID private int GetItemDefIDForGold(int goldAmount) { // 这些ID需要在Steam后台配置 switch (goldAmount) { case 100: return 1001; // 100金币包 case 500: return 1002; // 500金币包 case 1000: return 1003; // 1000金币包 case 2000: return 1004; // 2000金币包 default: return 1001; // 默认100金币包 } } // 购买结果回调 private void OnPurchaseResult(EResult result, ulong orderID) { if (result == EResult.k_EResultOK) { Debug.Log($"Purchase initiated successfully. Order ID: {orderID}"); // 显示购买确认UI ShowPurchaseConfirmation(orderID); } else { Debug.LogError($"Purchase failed with result: {result}"); // 显示错误信息 ShowPurchaseError(result); } } // 处理库存结果就绪 private void OnSteamInventoryResultReady(SteamInventoryResultReady_t callback) { Debug.Log($"Inventory result ready: {callback.m_result}"); if (callback.m_result == EResult.k_EResultOK) { ProcessInventoryResult(callback.m_handle); } else { Debug.LogError($"Inventory result failed: {callback.m_result}"); } // 清理结果句柄 SteamInventory.DestroyResult(callback.m_handle); } // 处理完整库存更新 private void OnSteamInventoryFullUpdate(SteamInventoryFullUpdate_t callback) { Debug.Log("Full inventory update received"); ProcessInventoryResult(callback.m_handle); SteamInventory.DestroyResult(callback.m_handle); } // 处理物品定义更新 private void OnSteamInventoryDefinitionUpdate(SteamInventoryDefinitionUpdate_t callback) { Debug.Log("Inventory definitions updated"); } // 处理库存结果 private void ProcessInventoryResult(SteamInventoryResult_t resultHandle) { if (!m_bInitialized || !SteamManager.Initialized) return; uint itemCount = 0; // 获取物品数量 if (!SteamInventory.GetItemsByID(out resultHandle, null, itemCount)) { Debug.LogError("Failed to get item count"); return; } if (itemCount == 0) { Debug.Log("Inventory is empty"); return; } // 获取物品详情 SteamItemInstanceID_t[] itemIDs = new SteamItemInstanceID_t[itemCount]; SteamItemDef_t[] itemDefs = new SteamItemDef_t[itemCount]; uint[] quantities = new uint[itemCount]; if (SteamInventory.GetItemsByID(out resultHandle, itemIDs, itemCount)) { // 先获取物品的数量信息 SteamInventory.GetItemsWithPrices(out itemDefs, out quantities, itemCount); for (int i = 0; i < itemCount; i++) { // 获取物品属性 string goldAmount = ""; uint goldLength = 32; SteamInventory.GetItemDefinitionProperty(itemDefs[i], "gold_amount", out goldAmount, ref goldLength); Debug.Log($"Item: {itemDefs[i].m_SteamItemDef}, Quantity: {quantities[i]}, Gold: {goldAmount}"); // 如果是金币包,添加到用户账户 if (!string.IsNullOrEmpty(goldAmount)) { int goldToAdd; if (int.TryParse(goldAmount, out goldToAdd)) { AddGoldToUser(goldToAdd); // 消费物品(标记为已使用) ConsumeItem(itemIDs[i], 1); } } } } } // 添加金币到用户账户 private async void AddGoldToUser(int amount) { // 获取当前用户数据 UserData userData = Authenticator.Get().UserData; if (userData != null) { userData.coins += amount; Debug.Log($"Added {amount} gold to user. Total: {userData.coins}"); // 保存用户数据 await Authenticator.Get().SaveUserData(); // 更新UI UpdateGoldUI(userData.coins); // 解锁相关成就 CheckGoldAchievements(userData.coins); } } // 消费物品(标记为已使用) private void ConsumeItem(SteamItemInstanceID_t itemID, uint quantity) { if (!m_bInitialized || !SteamManager.Initialized) return; SteamInventoryResult_t resultHandle; if (SteamInventory.ConsumeItem(out resultHandle, itemID, quantity)) { Debug.Log($"Consumed item {itemID} x{quantity}"); } } // 更新金币UI private void UpdateGoldUI(int newGoldAmount) { // 这里可以调用您的UI更新方法 // 例如:MainMenu.Get().RefreshUserData(); } // 检查金币相关成就 private void CheckGoldAchievements(int totalGold) { if (!m_bInitialized || !SteamManager.Initialized) return; if (totalGold >= 1000) { // 解锁"首次充值"成就 SteamUserStats.SetAchievement("ACH_FIRST_PURCHASE"); SteamUserStats.StoreStats(); } if (totalGold >= 10000) { // 解锁"大富豪"成就 SteamUserStats.SetAchievement("ACH_BIG_SPENDER"); SteamUserStats.StoreStats(); } } // 显示购买确认 private void ShowPurchaseConfirmation(ulong orderID) { // 在这里显示购买确认UI Debug.Log($"Purchase confirmed. Order: {orderID}"); } // 显示购买错误 private void ShowPurchaseError(EResult result) { // 在这里显示错误信息UI Debug.LogError($"Purchase error: {result}"); } // 更新Steam回调 private void Update() { // SteamAPI.RunCallbacks()现在由SteamManager处理,不需要在这里调用 } // 清理 private void OnDestroy() { // Steam API清理现在由SteamManager处理 // 不需要在这里调用SteamAPI.Shutdown() m_bInitialized = false; } public bool IsInitialized() { return m_bInitialized; } #else public static SteamCurrencyManager instance; void Awake() { // 在不支持Steamworks的平台上创建一个空实例 if (instance == null) { instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); return; } } public void InitializeSteamCurrency() { Debug.LogWarning("SteamCurrencyManager is not available on this platform"); } public bool IsInitialized() { return false; } // 非Steam平台的空实现 public void GetUserInventory() { } public void PurchaseGoldPackage(int goldAmount, int priceInCents) { } // public bool IsInitialized() { return false; } private void Update() { } private void OnDestroy() { } #endif } }