Get started

This page describes the steps you need to create a C# module in the Unity Editor and deploy it to the Cloud Code service.

Note: Only Unity 2021.3 and above support Unity Editor integration for modules.

The examples in the workflow create a simple Hello World C# module. To get started with Cloud Code modules, follow these steps:

  1. Set up the Unity Editor: Link your Unity Gaming Services project with the Unity Editor, and install the required packages.
  2. Create a C# Module Reference file: Create a module reference file in the editor and link it to your module project.
  3. Generate a new module: Generate a new module from the Deployment window.
  4. Generate bindings: Generate client code from the solution.
  5. Deploy the module: Deploy the module to the Cloud Code service.
  6. Call the module function from your game: Call the module endpoint from your game client.

Note: For alternative authoring methods to create and deploy modules, refer to Write modules.

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 your Unity Gaming Services project with the Unity Editor. You can find your UGS project ID in the Unity Cloud 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.

  1. In Unity Editor, select Edit > Project Settings > Services.

  2. Link your project.
    If your project doesn't have a Unity project ID:

    1. Select Create a Unity Project ID > Organizations, then select an organization from the dropdown.
    2. Select Create project ID.


    If you have an existing Unity project ID:

    1. Select Use an existing Unity project ID.
    2. Select an organization and a project from the dropdowns.
    3. Select Link project ID.

Your Unity Project ID appears, and the project is now linked to Unity services.

Install required packages

To create Cloud Code modules within the Editor, you need to install the following packages:

  • Deployment
  • Cloud Code (2.5.0 or higher)

Note: Check Unity - Manual: Package Manager window to familiarize yourself with the Unity Package Manager interface.

You can install and add these packages to your list of available packages from the Unity Editor Package Manager window:

  1. To open the Unity Editor’s Package Manager window by selecting Window > Package Manager.
  2. Select + (add) > Add package by name….
  3. Enter com.unity.services.deployment.
  4. Select Add.
  5. 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, follow the steps below:

  1. In the Unity Editor, open the Preferences window.
    • On Windows, select Edit > Preferences.. > Cloud Code.
    • On macOS, select Unity > Settings.. > Cloud Code.
  2. In the .NET development environment section, modify your .NET path to the one you have installed.
  3. 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.

Before you can use the Deployment window, you need to select the environment you want to deploy to:

  1. In the Unity Editor, open the Deployment window.
  2. In the Deployment window, select Deployment Settings.
  3. 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.

  1. In the Unity Editor, right-click in the Project window, then select Create > Cloud Code C# Module Reference.
  2. Name the module reference file HelloWorld.
  3. Press Enter.

The new module reference is now visible in the Project window and in the Deployment window.

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.

  1. In the Unity Editor, select the module reference file in the Project window.
  2. In the Inspector window, click Generate Solution.

You can also generate a new module directly from the Deployment window.

  1. In the Unity Editor, open the Deployment window.
  2. Right-click on the module reference file and select Generate Solution.

You can find the generated module in the root directory of your Unity project, in the HelloWorld folder. The module is a .NET project that you can modify and extend.

Note: Cloud Code modules are .NET projects, so they use the NuGet package manager to manage dependencies. Cloud Code modules require the Cloud Code Authoring package to enable you to create endpoints you can call out to. This is a NuGet package that you can install into your project, and it's already included in the generated module.

Generate 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.

  1. In the Unity Editor, select the module reference file in the Project window.
  2. 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:

  1. In the Unity Editor, open the Deployment window.
  2. Select the module reference file in the Project window.
  3. In the Deployment window, click Deploy Selected.

For more information, check the Deployment package manual.

A successful deployment shows a green checkmark next to the module reference file in the Project window.

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 called SayHello.

SDK setup

To call a module function from your game, you first need to set up the Cloud Code SDK in your game client.

  1. Ensure the service is enabled via the Cloud Code page in the Unity Cloud Dashboard.
  2. Ensure that you have installed both the Cloud Code and the Authentication SDKs from the Unity Package Manager.
  3. Create a new C# Monobehaviour script in Unity Editor. For more information, refer to Creating and using scripts in the Unity Manual.

Note: Players must have a valid player ID and access token to access the Cloud Code services. You need to authenticate players with the Authentication SDK before you use any of the Cloud Code APIs.

Call the module function

The following example shows how to call the module function SayHello from the module HelloWorld within your game client.

C#

using 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);
        }
    }
}

Important: If a module hasn't received any traffic in the last 15 minutes, you might experience a cold start latency. Any subsequent calls to the module are faster.

Next steps

Learn about the next steps after deploying your first module.

TopicDescription
Module structureFamiliarize yourself with module structure.
Integrate with other Unity servicesIntegrate modules with other Unity Gaming Services.
Integrate with external servicesIntegrate modules with external services.
Write unit testsWrite unit tests for your modules.