Unity Editor integration
Once you create and publish a Cloud Code script in the Unity Dashboard, you can call it from your project in the Unity Editor, assuming you have linked your project to Cloud Code and installed the required Cloud Code SDK.
Typical workflow
You can call the Cloud Code SDK from a regular MonoBehaviour C# script within the Unity Editor.
- Create a C# MonoBehaviour script within Unity Engine. See Unity - Manual: Creating and Using Scripts.
- Within the script, configure the Unity Authentication service.
- Within the script, add a call to the Cloud Code SDK.
- Attach the newly made script to a game object. See Editor Scripting.
- Select Play and run the project to see Cloud Code in action.
Authentication
Before calling your Cloud Code script, you must complete an authentication flow using Unity Authentication. Apps typically need to know a user's identity to provide a variety of features and services to game developers and players to ensure security, consistency, and safety with every interaction. Cloud Code APIs require a valid player ID and an access token to access the Cloud Code services.
The Cloud Code API uses JSON Web Token (JWT) authentication. Unity Authentication provides anonymous authentication and platform-specific authentication solutions for supported platforms, including mobile and PC.
To get started, we recommend using anonymous authentication. The following example demonstrates how to start anonymous authentication using the authentication package and log the player ID in a script within Unity Editor.
C#
using Unity.Services.Authentication;
using System.Threading.Tasks;
using Unity.Services.Core;
using UnityEngine;
public class AuthenticationExample : MonoBehaviour
{
internal async Task Awake()
{
await UnityServices.InitializeAsync();
await SignInAnonymously();
}
private async Task SignInAnonymously()
{
AuthenticationService.Instance.SignedIn += () =>
{
var playerId = AuthenticationService.Instance.PlayerId;
Debug.Log("Signed in as: " + playerId);
};
AuthenticationService.Instance.SignInFailed += s =>
{
// Take some action here...
Debug.Log(s);
};
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
}
Calling a Cloud Code script
After including authentication in your newly created script, you are ready to call a Cloud Code script. Include the following namespace to integrate Cloud Code into your Unity Editor project:
C#
Using Unity.Services.CloudCode;
To run a created script, use the CallEndpointAsync
API client method, which you can find under CloudCodeService.Instance
.
C#
/// <summary>
/// Calls a Cloud Code function.
/// </summary>
/// <param name="function">Cloud Code function to call.</param>
/// <param name="args">Arguments for the cloud code function. Will be serialized to JSON.</param>
/// <typeparam name="TResult">Serialized from JSON returned by Cloud Code.</typeparam>
/// <returns>Serialized output from the called function.</returns>
/// <exception cref="CloudCodeException">Thrown if request is unsuccessful.</exception>
/// <exception cref="CloudCodeRateLimitedException">Thrown if the service returned rate limited error.</exception>
public async Task<TResult> CallEndpointAsync<TResult>(string function, Dictionary<string, object> args)
Note that the function
parameter is the name of your script, which can be found in the Cloud Code Dashboard, within the Details tab.
Unity Editor integration example
The following is a full example of how to integrate both Authentication and Cloud Code into your script.
C#
using UnityEngine;
using Unity.Services.Authentication;
using Unity.Services.CloudCode;
public class RollDiceExample : MonoBehaviour
{
// ResultType structure is the serialized response from the RollDice script in Cloud Code
private class ResultType
{
public int Roll;
public int Sides;
}
// Call this method to roll the dice (use a button)
public async void CallMethod()
{
await UnityServices.InitializeAsync();
// Sign in anonymously into the Authentication service
if (!AuthenticationService.Instance.IsSignedIn) await AuthenticationService.Instance.SignInAnonymouslyAsync();
// Call out to the Roll Dice script in Cloud Code
var response = await CloudCodeService.Instance.CallEndpointAsync<ResultType>("RollDice", null);
// Log the response of the script in console
Debug.Log($"You rolled {response.Roll} / {response.Sides}");
}
}
You can serialize the response from the Cloud Code script into your preferred type. To learn more about the Cloud Code SDK, see Cloud Code SDK API.