question

Opticgamer avatar image
Opticgamer asked

How to Display Player's Inventory In Unity

Hello, I wanted to know how I could display the player's Playfab inventory ingame. Like you press a button and it brings up a menu showing all your Playfab items.

apisPlayer DataPlayer Inventory
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image
Seth Du answered

If you want to get the inventory of a player, the proper API is GetUserInventory, and as you can see, GetUserInventoryResult contains 3 properties, where inventory is stored as a list of

ItemInstance. ItemInstance is a property created by PlayFab and it has been predefined and contained in Unity SDK. You can find the documentation on https://docs.microsoft.com/en-us/rest/api/playfab/client/player-item-management/getuserinventory?view=playfab-rest#iteminstance.

Please understand that in the PlayFab community forum, we are glad to help with PlayFab related questions like API usage, property type, etc. In terms of UI design on Unity Engine, we cannot provided very detailed and professional support since we are not expert on it. There are Unity samples on PlayFab official repository, however, we will still suggest finding more help on Unity developer community if you need dedicated support.

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Opticgamer avatar image Opticgamer commented ·

Thank you I really don't need help like attaching an inventory to the UI or anything my main question was how to return all items in the players inventory on Playfab without using static classes.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Opticgamer commented ·

May I ask what do you mean by static classes? Do you mean the pre-defined types that defined in PlayFab SDK? Can you provide an example?

PlayFab Unity SDK also includes PlayFabSimpleJson methods, you can stringify any returned objects in the callback result.

0 Likes 0 ·
Opticgamer avatar image Opticgamer Seth Du ♦ commented ·

Like in this example:

https://github.com/PlayFab/UnicornBattle/blob/master/UnicornBattle/Assets/Scripts/PF_StaticsAndHelpers/PF_PlayerData.cs#L78

I already have something that can display VC without static classes but im not sure how I can change it to return inventory items:



0 Likes 0 ·
Show more comments
Seth Du avatar image
Seth Du answered

Please always refer to the API document: GetUserInventory.

The successful callback GetUserInventoryResult contains 3 properties, which are Inventory, VirtualCurrency and VirtualCurrencyRechargeTimes, where Inventory is a list of ItemInstance and VirtualCurrency is an Object.

Hence, to retrieve the inventory:

var itemList = GetResult.Inventory;

In terms of ItemInstance[], it is pre-defined in the SDK :

public List<ItemInstance> Inventory;

and ItemInstance is pre-fined in PlayFabClientModels.cs

    public class ItemInstance : PlayFabBaseModel
    {
        public string Annotation;
        public List<string> BundleContents;
        public string BundleParent;
        public string CatalogVersion;
        public Dictionary<string,string> CustomData;
        public string DisplayName;
        public DateTime? Expiration;
        public string ItemClass;
        public string ItemId;
        public string ItemInstanceId;
        public DateTime? PurchaseDate;
        public int? RemainingUses;
        public string UnitCurrency;
        public uint UnitPrice;
        public int? UsesIncrementedBy;
    }

Basically you may retrieve anything you want.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Opticgamer avatar image Opticgamer commented ·

Hmm it doesn't seem to return the items to List. Do I need a Dictionary like I had at the top of my GetVC function?

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Opticgamer commented ·

I think it is necessary.

GetResult.VirtualCurrency is a normal Dictionary<string, int> type. However, you may also use Implicitly type via "var".

Meanwhile, List<ItemInstance> is defined by PlayFab SDK. If you need to use externally, it will be better to declare definition in the begining.

0 Likes 0 ·
Opticgamer avatar image Opticgamer Seth Du ♦ commented ·

Hmm I don't know how to make a definition for "ItemInstance".

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.