文档

​
​

Development

User Acquisition

Monetization

工业

Cloud Code

Open Unity Dashboard

Cloud Code

LiveOps
​
​
Cloud Code
  • Overview
  • Get started
  • Server authority
  • Cloud Code C# modules
    • Overview
    • Get started
    • Concepts
    • Tutorials
      • Run modules
      • Write modules
      • Development essentials
        • Write unit tests
        • Dependency injection
        • Custom serialization
        • Batch requests
        • In-memory cache
      • Automate deployment
      • Integrate services
      • Advanced configuration
      • Use Cases
    • Reference
  • Cloud Code JavaScript scripts
  • Logging
  • Use-case samples
  • Privacy and consent
  1. Cloud Code

批处理请求

Batch your requests to execute UGS service calls in parallel for efficiency.
阅读时间4 分钟
最后更新于 9 个月前

在调用 UGS 服务时可以批量处理您的请求,以便它们能够并行执行。
您可以使用
Task.WhenAll
方法并行调用 UGS 服务。此方法返回一个当参数列表中的所有任务都完成时才完成的
Task
。
注意
注意: 所有失败的请求都会记录到日志中。您可以在 Unity Dashboard 的 Cloud Code 日志中找到它们。要了解有关日志记录的更多信息,请参阅日志记录。
注意
注意: 如果某些请求失败,不会回滚成功的请求。

调用和返回结果

您可以使用下面的示例来调用服务并返回请求结果。
下面的示例调用了 Leaderboards 和 Economy 服务。如果您的项目中没有配置排行榜或货币,这些调用将会失败。因此,您可以测试 Cloud Code 模块中的错误处理。
该方法返回一个字典,其中包含请求的结果,以发出请求的方法的签名作为键,以请求的结果作为值。如果请求失败,会说明哪个请求失败了。这些失败结果也会记录到日志中。
using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using Unity.Services.CloudCode.Apis;using Unity.Services.CloudCode.Core;using Unity.Services.CloudSave.Model;using Unity.Services.Economy.Model;namespace BatchingRequests;public class BatchingRequests{ private const string ExperienceKey = "experience"; private const string ProgressXpKey = "progressXP"; private const string FailedMessage = "Failed. Check logs."; private readonly ILogger<BatchingRequests> _logger; public BatchingRequests(ILogger<BatchingRequests> logger) { _logger = logger; } [CloudCodeFunction("HandleMultipleRequests")] public async Task<Dictionary<string, object>> HandleMultipleRequests(IGameApiClient gameApiClient, IExecutionContext ctx) { async Task<object> GetCloudSaveData() { var res = await gameApiClient.CloudSaveData.GetItemsAsync(ctx, ctx.AccessToken, ctx.ProjectId, ctx.PlayerId, new List<string> { ExperienceKey, ProgressXpKey }); return res.Data; } async Task<object> SetCloudSaveData() { var res = await gameApiClient.CloudSaveData.SetItemAsync(ctx, ctx.AccessToken, ctx.ProjectId, ctx.PlayerId, new SetItemBody(ExperienceKey, 1)); return res.Data; } async Task<object> GetLeaderboardScore() { const string leaderboardId = "leaderboard"; var res = await gameApiClient.Leaderboards.GetLeaderboardPlayerScoreAsync(ctx, ctx.AccessToken, Guid.Parse(ctx.ProjectId), leaderboardId, ctx.PlayerId); return res.Data; } async Task<object> GetEconomyConfig() { var res = await gameApiClient.EconomyConfiguration.GetPlayerConfigurationAsync(ctx, ctx.AccessToken, ctx.ProjectId, ctx.PlayerId); return res.Data; } async Task<object> SetEconomyConfig() { const string currencyId = "NOT_REAL"; var res = await gameApiClient.EconomyCurrencies.IncrementPlayerCurrencyBalanceAsync(ctx, ctx.AccessToken, ctx.ProjectId, ctx.PlayerId, currencyId, new CurrencyModifyBalanceRequest(currencyId, 1)); return res.Data; } // Create a list of UGS tasks that return data var tasksWithObjects = new List<Func<Task<object>>>() { GetCloudSaveData, GetLeaderboardScore, SetCloudSaveData, GetEconomyConfig, SetEconomyConfig }; var results = await AwaitBatch(tasksFns); foreach (var item in results) { if (item is Exception) { _logger.LogError($"AwaitBatch failed status {item}"); } else if (item is CurrencyBalanceResponse) { _logger.LogInformation($"AwaitBatch CurrencyBalanceResponse status {item.GetType()} = {item}"); } else { _logger.LogInformation($"AwaitBatch success status {item.GetType()} = {item}"); } } // Alternatively to looping through all results, you may access function results directly // The order of function results follows the order of the function input e.g. // - GetCloudSaveData corresponds to results[0]: GetItemsResponse || Exception // - GetLeaderboardScore corresponds to results[1]: LeaderboardEntryWithUpdatedTime || Exception // ... etc // This can also help ascertain where the Exception originated from } private async Task<object[]> AwaitBatch(List<Func<Task<object>>> tasks) { var tasksToRun = tasks.Select(Run); var results = await Task.WhenAll(tasksToRun); return results; } async Task<object> Run(Func<Task<object>> fn) { try { return await fn(); } catch (Exception e) { return e; } }}public class ModuleConfig : ICloudCodeSetup{ public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton(GameApiClient.Create()); }}

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • 调用和返回结果


报告此页面的问题