玩家背包
Use the SDK to manage player inventory items including adding, updating, and deleting items.
阅读时间5 分钟最后更新于 15 天前
使用
PlayerInventoryGetInventoryAsync
检索与当前登录玩家关联的当前背包物品实例。 此方法可采用GetInventoryOptionsPlayersInventoryItemPlayersInventoryItem下一个示例代码仅检索玩家的// Optional, defaults to 20GetInventoryOptions options = new GetInventoryOptions{ ItemsPerFetch = 5};GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);List<PlayersInventoryItem> firstFiveItems = inventoryResult.PlayersInventoryItems;// do something with your itemsif (inventoryResult.HasNext) { inventoryResult = await inventoryResult.GetNextAsync(5); List<PlayersInventoryItem> nextFiveItems = inventoryResult.PlayersInventoryItems; // do something with your items}
SWORD这些方法将返回GetInventoryOptions options = new GetInventoryOptions{ InventoryItemIds = new List<string>() { "SWORD" }};GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);List<PlayersInventoryItem> listOfItems = inventoryResult.PlayersInventoryItems;// ... etc
GetInventoryResultGetInventoryOptions
GetInventoryAsync- :字符串列表。默认为
PlayersInventoryItemIds。您要检索的玩家背包中的物品的nullID。PlayersInventoryItem - :字符串列表。默认为
InventoryItemIds。您要检索的物品的配置 ID。null - :整数。默认为 20。使用此设置可将每次调用获取的最大物品数量设置为 1 到 100 之间的数(包含 1 和 100)。
ItemsPerFetch
GetInventoryResult
通过GetInventoryResult- :包含当前获取物品的
PlayersInventoryItems。List<PlayersInventoryItem>
- :此方法可异步获取更多结果。可以通过一个可选参数来限制获取的结果数量(1 到 100,默认为 20)。这将返回新结果,
GetNextAsync(int itemsToFetch = 20)列表中同时包含原有物品和新获取的物品。如果没有更多结果可供获取,结果将为PlayersInventoryItems。null
AddInventoryItemAsync
将物品添加到玩家的背包中。 此方法可采用AddInventoryItemOptionsPlayersInventoryItemIdnullPlayersInventoryItemDictionary<string, object> instanceData = new Dictionary<string, object>{ { "rarity", "purple" }};AddInventoryItemOptions options = new AddInventoryItemOptions{ PlayersInventoryItemId = "customID", InstanceData = instanceData };PlayersInventoryItem createdInventoryItem = await EconomyService.Instance.PlayerInventory.AddInventoryItemAsync("SWORD", options);
AddInventoryItemOptions
AddInventoryItemAsync- :字符串。默认为
PlayersInventoryItemId。设置创建的null的 ID。如果未提供,则会生成一个。PlayersInventoryItem - :一个
InstanceData。用于设置实例数据的字典。Dictionary<string, object>
DeletePlayersInventoryItemAsync
从玩家的背包中删除物品。 此方法可采用一项用于设置写锁的DeletePlayersInventoryItemOptionsDeletePlayersInventoryItemOptions options = new DeletePlayersInventoryItemOptions{ WriteLock = "writeLock"};EconomyService.Instance.PlayerInventory.DeletePlayersInventoryItemAsync("playersInventoryItemID", options);
DeletePlayersInventoryItemOptions
DeletePlayersInventoryItemAsync- :字符串。默认为
WriteLock。使用它来设置写锁,以实现乐观并发。请参阅写锁。null
UpdatePlayersInventoryItemAsync
使用新实例数据更新物品。 返回更新后的玩家背包物品。 此方法可采用一项用于设置写锁的UpdatePlayersInventoryItemOptionsDictionary<string, object> instanceData = new Dictionary<string, object>{ { "rarity", "purple" }};UpdatePlayersInventoryItemOptions options = new UpdatePlayersInventoryItemOptions{ WriteLock = writeLock };PlayersInventoryItem playersInventoryItem = await EconomyService.Instance.PlayerInventory.UpdatePlayersInventoryItemAsync("playersInventoryItemID", instanceData, options);
UpdatePlayersInventoryItemOptions
UpdatePlayersInventoryItemAsync- :字符串。默认为
WriteLock。使用它来设置写锁,以实现乐观并发。请参阅写锁。null
PlayersInventoryItem
PlayersInventoryItem- :玩家背包中具有唯一性的此背包物品的 ID。
PlayersInventoryItemId - :此背包物品的配置 ID。
InstanceItemId - :与此玩家背包物品关联的任何实例数据。
InstanceData - :与此玩家背包物品关联的任何实例数据,形式为
InstanceDataDeserializable。请参阅使用 InstanceDataDeserializable。IDeserializable - :此玩家的背包物品的当前
WriteLock字符串。writelock - :此玩家的背包物品的创建日期。它是一个 EconomyDate 对象。
Created - :此玩家的背包物品的修改日期。它是一个 EconomyDate 对象。
Modified
GetItemDefinitionAsync
此方法获取该玩家的背包物品配置,类型为InventoryItemDefinitionPlayersInventoryItem playersInventoryItem = // ... fetch the players inventory itemInventoryItemDefinition itemDefinition = playersInventoryItem.GetItemDefinitionAsync();
PlayersInventoryItemUpdated
可以订阅此事件,以便在 SDK 更新玩家背包中的特定物品时收到通知。订阅者将收到已更新物品的playersInventoryItemEconomyService.Instance.PlayerInventory.PlayersInventoryItemUpdated += playersInventoryItemID => { Debug.Log($"The players inventory item that was updated was {playersInventoryItemID}");};
使用 InstanceDataDeserializable
使用InstanceDataDeserializableobjectIDeserializableInstanceDataDeserializable您可以反序列化实例数据,操作如下:class MyInstanceData { public int Durability; public string Rarity; }MyInstanceData myInstanceData = new MyInstanceData() { Durability = 100, Rarity = "purple"};PlayersInventoryItem updatedItem = await EconomyService.Instance.PlayerInventory.UpdatePlayersInventoryItemAsync("playersInventoryItemId", myInstanceData);
MyInstanceData fetchedInstanceData = updatedItem.InstanceData.GetAs<MyInstanceData>();int durability = fetchedInstanceData.Durability;string rarity = fetchedInstanceData.Rarity;