Integrate with other Unity services
Integrate Cloud Code with various Unity Gaming Services using C# Software Development Kits or REST APIs.
Read time 4 minutesLast updated 18 hours ago
To unlock the full potential of Cloud Code, you can seamlessly integrate it with various Unity Gaming Services using either the C# Service SDKs or REST APIs. The C# SDKs offer a simpler and more consistent experience, providing convenient access to Unity Gaming Services. If the Unity Gaming Service you wish to utilize already has a Cloud Code C# SDK, you can take advantage of it for a smoother integration. However, in cases where a specific UGS service lacks a Cloud Code C# SDK, you have the flexibility to connect with it directly through its REST API. For some services that where the state doesn't change frequently like Remote Config, it can be benefical to introduce an in-memory cache to reduce the number of HTTP requests. To read more about in-memory caches refer to the Best practices page.
Connect to UGS through the UGS SDKs
To access the Cloud Code C# services SDK from NuGet, search for Com.Unity.Services.CloudCode.Apis. Once you install the package, you can use it within your C# modules to simplify the way you connect with other Unity services.
For a full list of Cloud Code C# SDKs, refer to the Available Libraries page.
API Clients
TheGameApiClientAdminApiClientUse the GameApiClient
class
The GameApiClientGameApiClientGameApiClientGameApiClientICloudCodeSetuppublic class ModuleConfig : ICloudCodeSetup{ public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton(GameApiClient.Create()); }}
Usage Example
You can use theGameApiClientCloudCodeFunctionGameApiClientIGameApiClientusing System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using Unity.Services.CloudCode.Apis;using Unity.Services.CloudCode.Core;using Unity.Services.CloudCode.Shared;using Unity.Services.CloudSave.Model;namespace ExampleModule;public class CloudSaveSdkSample{ private readonly ILogger<CloudSaveSdkSample> _logger; public CloudSaveSdkSample(ILogger<CloudSaveSdkSample> logger) { _logger = logger; } [CloudCodeFunction("SavePlayerData")] public async Task SavePlayerData(IExecutionContext context, IGameApiClient gameApiClient, string key, string value) { try { await gameApiClient.CloudSaveData.SetItemAsync( context, context.AccessToken!, context.ProjectId!, context.PlayerId!, new SetItemBody(key, value)); _logger.LogInformation("Successfully saved data for key: {Key}", key); } catch (ApiException ex) { _logger.LogError("Failed to save data for key {Key}. Error: {Error}", key, ex.Message); throw new Exception($"Unable to save player data: {ex.Message}"); } } [CloudCodeFunction("GetPlayerData")] public async Task<string> GetPlayerData(IExecutionContext context, IGameApiClient gameApiClient, string key) { try { var result = await gameApiClient.CloudSaveData.GetItemsAsync( context, context.AccessToken!, context.ProjectId!, context.PlayerId!, new List<string> { key }); var data = result.Data.Results.FirstOrDefault()?.Value?.ToString() ?? string.Empty; _logger.LogInformation("Successfully retrieved data for key: {Key}", key); return data; } catch (ApiException ex) { _logger.LogError("Failed to retrieve data for key {Key}. Error: {Error}", key, ex.Message); throw new Exception($"Unable to retrieve player data: {ex.Message}"); } }}public class ModuleConfig : ICloudCodeSetup{ public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton(GameApiClient.Create()); }}
Use the AdminApiClient
class
The AdminApiClientAdminApiClient
To use the
AdminApiClientAdminApiClientICloudCodeSetuppublic class ModuleConfig : ICloudCodeSetup{ public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton(AdminApiClient.Create()); }}
Usage Example
Similar to theGameApiClientAdminApiClientAdminApiClientIAdminApiClient
The following example demonstrates how to create a Leaderboard:
using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using Unity.Services.CloudCode.Apis;using Unity.Services.CloudCode.Apis.Admin;using Unity.Services.CloudCode.Core;using Unity.Services.CloudCode.Shared;using Unity.Services.Leaderboards.Admin.Model;namespace ExampleModule;public class ModuleMain{ private static ILogger<ModuleMain> _logger; public ModuleMain(ILogger<ModuleMain> logger) { _logger = logger; } [CloudCodeFunction("CreateLeaderboard")] public async Task CreateLeaderboard(IExecutionContext context, IAdminApiClient adminApiClient) { try { await adminApiClient.Leaderboards.CreateLeaderboardAsync( executionContext: context, serviceAccountKey: "YOUR_SERVICE_ACCOUNT_KEY", serviceAccountSecret: "YOUR_SERVICE_ACCOUNT_SECRET", projectId: Guid.Parse(context.ProjectId), environmentId: Guid.Parse(context.EnvironmentId), leaderboardIdConfig: new LeaderboardIdConfig( id: "new-leaderboard", name: "new-leaderboard", sortOrder: SortOrder.Asc, updateType: UpdateType.KeepBest ) ); } catch (ApiException ex) { _logger.LogError("Failed to create a Leaderboard. Error: {Error}", ex.Message); throw new Exception($"Failed to create a Leaderboard. Error: {ex.Message}"); } } public class ModuleConfig : ICloudCodeSetup { public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton(AdminApiClient.Create()); } }}
Connect to UGS through REST APIs
If you want to use a service that doesn't have a C# SDK yet, you can also connect with the services directly through their REST API.Authentication
Depending on your use case, you can use either theAccessTokenServiceTokenServiceToken