Implementation
To get Cloud Code working with your project, you first need to link your project to the Unity Dashboard. Afterward, access Unity services, enable your project in the Dashboard, and install the SDK in Unity Editor.
Link project
To use the Cloud Code service, you must link your project with the Unity Dashboard.
In Unity Editor, select Edit > Project Settings > Services.
Link your project.
If your project doesn't have a Unity project ID:
- Select Create a Unity Project ID > Organizations, then select an organization from the dropdown menu.
- Select Create project ID.
If you have an existing Unity project ID:
- Select Use an existing Unity project ID.
- Select an organization and a project from the dropdown menus.
- Select Link project ID.
Your Unity Project ID appears, and the project is now linked to Unity services. You can also access your project ID in a Unity Editor script using UnityEditor.CloudProjectSettings.projectId
.
Unity Dashboard
You can access Cloud Code directly from the Unity Dashboard:
- Go to the Unity Dashboard.
- Sign in by using your Unity ID.
- In the Main Menu, select Explore Services (if not already selected).
- In the main window, under either the All Solutions or LiveOps tabs, select Cloud Code.
SDK installation
To install the latest Cloud Code package for Unity:
- In the Unity Editor, open Window > Package Manager.
- In the Package Manager, select the Unity Registry list view.
- Search for
com.unity.services.cloudcode
, or locate the Cloud Code package in the list. - Select the package, then click Install.
See the Package Manager documentation for more information.
For detailed SDK documentation, see Cloud Code SDK API.
SDK setup
The Cloud Code SDK is ready to use once you have signed in with the Authentication SDK. The Cloud Code SDK requires authoring one or more cloud scripts in the Cloud Code dashboard. You can then call any Cloud Code methods to start interacting with your data.
Before you continue, make sure you complete the SDK installation.
To get started with the Cloud Code SDK:
- Ensure the service is enabled via the Cloud Code service dashboard page.
- Ensure that you have installed both the Cloud Code and the Authentication SDKs.
- Sign into your cloud project from within Unity Editor by selecting Edit > Project Settings... > Services.
- Create a new C# Monobehaviour script in Unity Editor. See Creating and using scripts in the Unity Manual.
- In the script, initialize the Core SDK using await
UnityServices.InitializeAsync()
. - In the script, initialize the Authentication SDK.
Players must have a valid player ID and access token to access the Cloud Code services. You must authenticate players with the Authentication SDK before using any of the Cloud Code APIs. You can do this with the following code snippet for anonymous authentication or see the documentation for the Authentication SDK for more details and other sign-in methods.
C#
await AuthenticationService.Instance.SignInAnonymouslyAsync();
See Unity Editor integration for examples and information about setting up the script.
Authentication
Apps typically need to know the identity of a user to provide a variety of features and services to both game developers and players to ensure security, consistency, and safety with every interaction. Each player must have a valid player ID and an access token to access the Cloud Code services. See Unity Editor integration: Authentication.
Writing your first script
You can define a simple "hello world" script in Cloud Code to test the service. Use the Unity Dashboard to create a new script with a name
parameter of type string
. For more detailed instructions, see Script creation.
You can then use the following snippet as your code.
JavaScript
module.exports = async ({ params, logger }) => {
const name = params.name;
const message = `Hello, ${name}. Welcome to Cloud Code!`
logger.debug(message);
return {
welcomeMessage: message
};
};
Save the changes and publish your script. Once you publish it, you can call a Cloud Code script from a project using one of the SDK endpoints, as shown in the following example.
C#
using System.Collections.Generic;
using Unity.Services.Authentication;
using Unity.Services.CloudCode;
using Unity.Services.Core;
using UnityEngine;
/*
* Note: You must have a published script to use the Cloud Code SDK.
* You can publish a script from the Unity Dashboard - https://dashboard.unity3d.com/
*/
public class CloudCodeExample : MonoBehaviour
{
/*
* CloudCodeResponse represents the response from the script, used for deserialization.
* In this example, the script returns a JSON in the format
* {"welcomeMessage": "Hello, arguments['name']. Welcome to Cloud Code!"}
*/
class CloudCodeResponse
{
public string welcomeMessage;
}
/*
* Initialize all Unity Services and Sign In an anonymous player.
* You can perform this operation in a more centralized spot in your project
*/
public async void Awake()
{
await UnityServices.InitializeAsync();
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
/*
* Populate a Dictionary<string,object> with the arguments and invoke the script.
* Deserialize the response into a CloudCodeResponse object
*/
public async void OnClick()
{
var arguments = new Dictionary<string, object> { { "name", "Unity" } };
var response = await CloudCodeService.Instance.CallEndpointAsync<CloudCodeResponse>("hello-world", arguments);
}
}
See Integration with other Unity services to learn how to use Cloud Code with other Unity services.