Cloud Code

A modern game requires a lot of components in order to be successful, such as an in-game economy, storage for player data, and dynamic settings configuration. Using these components from a player device is not always preferable because it can lead to security issues and introduce unnecessary complexity. Sometimes, you need to isolate your game logic away from the client device, such as when:

  • You need to instantly update the game logic without requiring the player to update their installed version.
  • You want to prevent malicious users from altering the client code, changing their state, and cheating. You might also want to prevent users from reverse-engineering your game logic.
  • You want to use multiple online services without making the game feel sluggish or difficult to write on the game client as you integrate with more server-side components.

Typically, you create and maintain a game server that can handle these use cases. However, effectively managing, scaling, and upgrading backend infrastructure can require a lot of effort. You also need to manage the compatibility of many components, including:

  • Hardware
  • Server security
  • The game API

By using Unity's Cloud Code to write stateless server-side code on a fully managed infrastructure, you can focus more on developing your game logic. Cloud Code automatically provisions server capacity based on load so you can ensure that your players receive a good experience without any lag or downtime. Cloud Code also ensures that you only pay for what you use because there are never any wasted resources when the game is not receiving traffic. In addition, because your code runs in a secure sandbox, you can protect your game logic from reverse engineering. Seamless integration with other Unity services allows you to write code that ties in multiple components without concern for authentication or compatibility. You can instantly deploy code to live games without rolling out a client update that would require users to update their installs.

Cloud Code functions are written in JavaScript. You can call these functions from games by using an HTTP request. Write, test, and debug functions from the Unity Dashboard in the same place you define the configuration for other services, such as Remote Config.

Typical workflow

The following process details a typical development cycle:

  1. Create a script. You can create a Cloud Code script through the Unity Dashboard. Select the type (currently only API scripts are supported) and add custom parameters.
  2. Write the code. Build from the example scripts or start from scratch. Easily integrate with other Unity services by using the Cloud Code API clients.
  3. Test and debug. Select Run on the dashboard and test the result of your function. Use the provided test players to validate the interaction with other services or add logging statements to debug issues. See Test running the script.
  4. Publish the script. Select Publish, makes the script available to the game client. This is the main mechanism for doing live updates on scripts, which are immediately displayed in the game.
  5. Integrate the script with your project. Import the Cloud Code SDK into your game and use one of the provided methods to call your script. See Unity Editor integration.

For more information about interacting with Cloud Code, see Cloud Code usage.

Example use cases

You can write much of your game logic with Cloud Code. However, it might not be obvious how to connect different services from within scripts or how doing so would fit in as a game mechanic. Explore the use cases to better understand Cloud Code scripts and what problems they can solve. Examples include server time anti-cheat mechanisms, in-game coupons, and daily rewards.

For more advanced use, download the Unity Gaming Services Samples project to learn how to solve common game development challenges with Cloud Code, and read the Advanced use cases.