유니티 플레이팹 유저 인벤토리 가져오기 GetUserInventory 간단 사용법

반응형

유니티 플레이팹 유저 인벤토리 가져오기 GetUserInventory 간단 사용법

플레이팹 로그인이 되었다는 가정하에 진행

 

유니티 플레이팹 게스트 로그인 Playfab Sign In with Guest Login 간단 사용법

코드 작성 using PlayFab; using PlayFab.ClientModels; using PlayFab.Json; using PlayFab.ProfilesModels; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using EntityKey = PlayFab.ProfilesMod

parksh3641.tistory.com

 


코드 작성

using PlayFab;
using PlayFab.ClientModels;
using System;
using System.Collections.Generic;
using UnityEngine;

public class PlayfabManager : MonoBehaviour
{
    // 인벤토리 아이템 목록
    private List<ItemInstance> inventoryList = new List<ItemInstance>();
    
    // 가상 화폐 저장용 변수
    private int gold = 0;
    private int crystal = 0;
    
    // 인벤토리 정보 가져오는 이벤트
    public event Action<List<ItemInstance>> OnInventoryUpdated;
    public event Action<int, int> OnCurrencyUpdated; // gold, crystal
    
    // 에러 핸들러
    private void DisplayPlayfabError(PlayFabError error) => Debug.LogError("에러 발생: " + error.GenerateErrorReport());
    
    // 인벤토리 가져오기
    public void GetUserInventory()
    {
        Debug.Log("인벤토리 정보 요청 중...");
        
        PlayFabClientAPI.GetUserInventory(new GetUserInventoryRequest(), 
            result => {
                // 인벤토리 리스트 초기화
                inventoryList.Clear();
                
                // 가상 화폐 업데이트
                if (result.VirtualCurrency != null && result.VirtualCurrency.ContainsKey("GO"))
                {
                    gold = result.VirtualCurrency["GO"];
                }
                
                if (result.VirtualCurrency != null && result.VirtualCurrency.ContainsKey("ST"))
                {
                    crystal = result.VirtualCurrency["ST"];
                }
                
                Debug.Log($"가상 화폐 정보 업데이트 - 골드: {gold}, 크리스탈: {crystal}");
                
                // 가상 화폐 업데이트 이벤트 호출
                OnCurrencyUpdated?.Invoke(gold, crystal);
                
                // 인벤토리 아이템 처리
                if (result.Inventory != null && result.Inventory.Count > 0)
                {
                    // 인벤토리 목록 저장
                    inventoryList.AddRange(result.Inventory);
                    
                    Debug.Log($"{result.Inventory.Count}개의 아이템을 인벤토리에서 찾았습니다.");
                    
                    // 인벤토리 업데이트 이벤트 호출
                    OnInventoryUpdated?.Invoke(inventoryList);
                    
                    // 개별 아이템 디버그 로그 출력 (필요시)
                    foreach (ItemInstance item in inventoryList)
                    {
                        Debug.Log($"아이템: {item.DisplayName}, ID: {item.ItemId}, 클래스: {item.ItemClass}");
                    }
                }
                else
                {
                    Debug.Log("인벤토리가 비어있습니다.");
                    OnInventoryUpdated?.Invoke(new List<ItemInstance>());
                }
            }, 
            DisplayPlayfabError);
    }
    
    // 특정 아이템 ID로 인벤토리에서 검색
    public ItemInstance GetItemById(string itemId)
    {
        foreach (var item in inventoryList)
        {
            if (item.ItemId == itemId)
            {
                return item;
            }
        }
        return null;
    }
    
    // 특정 클래스의 아이템 목록 가져오기
    public List<ItemInstance> GetItemsByClass(string itemClass)
    {
        List<ItemInstance> result = new List<ItemInstance>();
        
        foreach (var item in inventoryList)
        {
            if (item.ItemClass == itemClass)
            {
                result.Add(item);
            }
        }
        
        return result;
    }
    
    // 현재 골드 반환
    public int GetGold()
    {
        return gold;
    }
    
    // 현재 크리스탈 반환
    public int GetCrystal()
    {
        return crystal;
    }
}

 


플레이팹 Api 참고

 

플레이어 인벤토리 사용 - PlayFab

PlayFab API를 사용하여 플레이어 인벤토리를 보고 작동하는 방법을 설명합니다.

docs.microsoft.com

 

반응형