玩家余额
Use the SDK to manage player currency balances.
阅读时间4 分钟最后更新于 15 天前
PlayerBalancesGetBalancesAsync
检索当前用户的货币余额。可采用GetBalancesOptionsGetBalancesOptions这些方法将返回// Optional, defaults to 20GetBalancesOptions options = new GetBalancesOptions{ ItemsPerFetch = 5};GetBalancesResult getBalancesResult = await EconomyService.Instance.PlayerBalances.GetBalancesAsync(options);List<PlayerBalance> firstFiveBalances = getBalancesResult.Balances;// do something with your balancesif (getBalancesResult.HasNext) { getBalancesResult = await getBalancesResult.GetNextAsync(options.ItemsPerFetch); List<PlayerBalance> nextFiveBalances = getBalancesResult.Balances; // do something with your balances}
GetBalancesResultGetBalancesOptions
GetBalancesAsync- :整数。默认为 20。使用此设置可将每次调用获取的最大余额数量设置为 1 到 100(包含 1 和 100)之间的数。
ItemsPerFetch
GetBalancesResult
通过GetBalancesResult- :包含当前获取余额的
Balances。List<PlayerBalance>
- :此方法可异步获取更多结果。可以通过一个可选参数来限制获取的结果数量(1 到 100,默认为 20)。这将返回新结果,
GetNextAsync(int itemsToFetch = 20)列表中同时包含原有物品和新获取的物品。Balances
SetBalanceAsync
将指定货币的余额设置为指定值。 此方法可采用一项用于设置写锁的SetBalancesOptionswriteLockwriteLockwriteLockstring currencyID = "GOLD_BARS";int newAmount = 1000;string writeLock = "someLockValueFromPreviousRequest";SetBalanceOptions options = new SetBalanceOptions{ WriteLock = writeLock};PlayerBalance newBalance = await EconomyService.Instance.PlayerBalances.SetBalanceAsync(currencyID, newAmount);// ORPlayerBalance otherNewBalance = await EconomyService.Instance.PlayerBalances.SetBalanceAsync(currencyID, newAmount, options);
SetBalanceOptions
SetBalanceAsync- :字符串。默认为
WriteLock。使用它来设置写锁,以实现乐观并发。请参阅写锁。null
IncrementBalanceAsync
将指定货币的余额增加指定值。 此方法可采用一项用于设置写锁的IncrementBalancesOptionswriteLockwriteLockwriteLockstring currencyID = "GOLD_BARS";int incrementAmount = 1000;string writeLock = "someLockValueFromPreviousRequest";IncrementBalanceOptions options = new IncrementBalanceOptions{ WriteLock = writeLock};PlayerBalance newBalance = await EconomyService.Instance.PlayerBalances.IncrementBalanceAsync(currencyID, newAmount);// ORPlayerBalance otherNewBalance = await EconomyService.Instance.PlayerBalances.IncrementBalanceAsync(currencyID, newAmount, options);
IncrementBalanceOptions
IncrementBalanceAsync- :字符串。默认为
WriteLock。使用它来设置写锁,以实现乐观并发。请参阅写锁。null
DecrementBalanceAsync
将指定货币的余额减少指定值。 此方法可采用一项用于设置写锁的DecrementBalanceOptionswriteLockwriteLockwriteLockstring currencyID = "GOLD_BARS";int decrementAmount = 1000;string writeLock = "someLockValueFromPreviousRequest";DecrementBalanceOptions options = new DecrementBalanceOptions{ WriteLock = writeLock};PlayerBalance newBalance = await EconomyService.Instance.PlayerBalances.DecrementBalanceAsync(currencyID, newAmount);// ORPlayerBalance otherNewBalance = await EconomyService.Instance.PlayerBalances.DecrementBalanceAsync(currencyID, newAmount, options);
DecrementBalanceOptions
DecrementBalanceAsync- :字符串。默认为
WriteLock。使用它来设置写锁,以实现乐观并发。请参阅写锁。null
PlayerBalance
玩家余额代表玩家的一项货币余额。它包含以下字段:- :该余额所代表的货币的 ID。
CurrencyId - :玩家拥有的该货币的整数数量。
Balance - :当前的
WriteLock字符串。writeLock - :此余额的创建日期。它是一个 EconomyDate 对象。
Created - :此余额的修改日期。它是一个 EconomyDate 对象。
Modified
GetCurrencyDefinitionAsync
这是获取此余额相关货币的货币定义的便捷方法。它返回 CurrencyDefinition。PlayerBalance myPlayerBalance = ... // Get a player balance from one of the above methodsCurrencyDefinition currencyDefForMyPlayerBalance = myPlayerBalance.GetCurrencyDefinitionAsync();
BalanceUpdated
可以订阅此事件,以便在 SDK 更新特定货币余额时收到通知。订阅者将收到已更新余额的货币 ID。EconomyService.Instance.PlayerBalances.BalanceUpdated += currencyID => { Debug.Log($"The currency that was updated was {currencyID}");};