Get started
Follow this workflow to create a C# module in the Unity Editor and deploy it to the Cloud Code service.
Read time 6 minutesLast updated a day ago
This page describes the steps you need to create a C# module in the Unity Editor and deploy it to the Cloud Code service.
The examples in the workflow create a simple Hello World C# module. To get started with Cloud Code modules, follow these steps:
- Set up the Unity Editor: Link your Unity Gaming Services project with the Unity Editor, and install the required packages.
- Create a C# Module Reference file: Create a module reference file in the editor and link it to your module project.
- Generate a new module: Generate a new module from the Deployment window.
- Generate bindings: Generate client code from the solution.
- Deploy the module: Deploy the module to the Cloud Code service.
- Call the module function from your game: Call the module endpoint from your game client.
Video walkthrough
Watch the video below for a walkthrough of the steps to create a C# module in the Unity Editor and deploy it to the Cloud Code service.Set up the Unity Editor
To create Cloud Code modules within the Editor, you need to install the required packages and link your Unity Gaming Services project with the Unity Editor.Link project
Link your Unity Gaming Services project with the Unity Editor. You can find your UGS project ID in the Unity Dashboard. If you create a new Unity project, you can link your Unity Gaming Services project with the Unity Editor during the project creation process. If you are working with an existing Unity project, you can link your Unity Gaming Services project with the Unity Editor from the Services window.- 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.
- 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 dropdowns.
- Select Link project ID.
Install required packages
To create Cloud Code modules within the Editor, you need to install the following packages:- Deployment
- Cloud Code (2.6.1 or higher)
You can install and add these packages to your list of available packages from the Package Manager window in the Unity Editor:
- To open the Unity Editor’s Package Manager window by selecting Window > Package Manager.
- Select + (add) > Add package by name….
- Enter .
com.unity.services.deployment - Select Add.
- Repeat these steps for .
com.unity.services.cloudcode
Install and set up .NET
To deploy Cloud Code modules in the editor you need to install .NET. To set your default .NET path in editor, use the following steps:- In the Unity Editor, open the Preferences window.
- On Windows, select Edit > Preferences.. > Cloud Code.
- On macOS, select Unity > Settings.. > Cloud Code.
- In the .NET development environment section, modify your .NET path to the one you have installed.
- Select Apply.
Set up the Deployment window
The Deployment window allows you to deploy Cloud Code modules to your remote environment. If you installed the Deployment package, you can access it from the Unity Editor.- In Unity Editor versions 2021.3+, select Window > Deployment.
- In Unity Editor versions 2022+, select Services > Deployment.
- In the Unity Editor, open the Deployment window.
- In the Deployment window, select Deployment Settings.
- In the new Environments window, select an environment from the Editor Environment dropdown.
Create a C# Module Reference file
To create a Cloud Code module within the Unity Editor, you need to create a Cloud Code C# module reference file. This file is a reference to a solution containing the C# module project you want to deploy.- In the Unity Editor, right-click in the Project window, then select Create > Cloud Code C# Module Reference.
- Name the module reference file .
HelloWorld - Press Enter.
Generate a new module
A Cloud Code module is a simple .NET project which exposes endpoints that a game client can call. In the Unity Editor, generating a module creates a solution with a module template that you can modify. To learn more about how a Cloud Code module is structured, refer to module structure. With a module reference file, you can generate a new module from the Deployment window. The generated module contains the required basic setup for you to start to develop your module.- In the Unity Editor, select the module reference file in the Project window.
- In the Inspector window, click Generate Solution.
- In the Unity Editor, open the Deployment window.
- Right-click on the module reference file and select Generate Solution.
HelloWorldGenerate bindings
After you generate a new module, you can generate bindings. Bindings are type-safe client code that you can use to call your module endpoints from your game client.- In the Unity Editor, select the module reference file in the Project window.
- In the Inspector window, select Generate Bindings.
Deploy the module
To make your module endpoints accessible to the game client, you need to deploy the module to the Cloud Code service. You can use the Deployment window to deploy modules. To deploy the module:- In the Unity Editor, open the Deployment window.
- Select the module reference file in the Project window.
- In the Deployment window, click Deploy Selected.
Call a module function from your game
To verify that a deployment is successful, you can call the module endpoints from your game client. The generated module contains a default endpoint calledSayHelloSDK setup
To call a module function from your game, you first need to set up the Cloud Code SDK in your game client.- Ensure the service is enabled via the Cloud Code page in the Unity Dashboard.
- Ensure that you have installed both the Cloud Code and the Authentication SDKs from the Unity Package Manager.
- Create a new C# Monobehaviour script in Unity Editor. For more information, refer to Creating and using scripts in the Unity Manual.
Call the module function
The following example shows how to call the module functionSayHelloHelloWorldusing Unity.Services.Authentication;using Unity.Services.CloudCode;using Unity.Services.CloudCode.GeneratedBindings;using Unity.Services.Core;using UnityEngine;public class TestModule : MonoBehaviour{ private async void Start() { // Initialize the Unity Services Core SDK await UnityServices.InitializeAsync(); // Authenticate by logging into an anonymous account await AuthenticationService.Instance.SignInAnonymouslyAsync(); try { // Call the function within the module and provide the parameters we defined in there var module = new HelloWorldBindings(CloudCodeService.Instance); var result = await module.SayHello("World"); Debug.Log(result); } catch (CloudCodeException exception) { Debug.LogException(exception); } }}
Next steps
Learn about the next steps after deploying your first module.Topic | Description |
|---|---|
| Module structure | Familiarize yourself with module structure. |
| Integrate with other Unity services | Integrate modules with other Unity Gaming Services. |
| Integrate with external services | Integrate modules with external services. |
| Write unit tests | Write unit tests for your modules. |
| Best practices | Follow best practices for Cloud Code. |