文档

Cloud Code

Run a local Cloud Code server

Start a Cloud Code server directly on your local machine.
阅读时间6 分钟最后更新于 6 天前

A local Cloud Code server lets you run and debug C# modules directly on your machine, so you can iterate on game server logic without pushing every change to a remote environment. You can also attach a debugger to the local process to set breakpoints, step through execution, and inspect memory while in Play mode.

Prerequisites

Before you start, make sure your project meets the following prerequisites:
  1. Unity 6.7 or later.
  2. The Unity Editor is set up to use Cloud Code modules.
  3. An environment is selected in the Deployment window. Refer to Set up the Deployment window.
  4. If you have existing modules, they reference the following NuGet package versions. Modules you create in the Unity Editor reference these packages automatically.
    • com.unity.services.cloudcode.apis
      v0.0.27 or later
    • com.unity.services.cloudcode.core
      v0.0.9 or later
  5. You have the ASP.NET Core Runtime 10.0 (Microsoft) installed on your machine. The .NET Runtime download alone isn't enough to start the local server.
注意
Local Cloud Code servers are only supported for Cloud Code C# modules. Cloud Code JavaScript scripts aren't supported.

Enable the Cloud Code toolbar button

You can access all local server settings and operations through the Cloud Code toolbar button. This button is disabled by default.
To enable the Cloud Code toolbar button, follow these steps:
  1. Select the More (⋮) menu in the toolbar.
  2. Select Services > Cloud Code.
The Cloud Code toolbar button now appears in the Editor's toolbar.
Screenshot showing how to enable Cloud Code in the Editor from the toolbar.

Enabling Cloud Code in the Editor from the toolbar

Execute modules on the local server

To execute C# server functions on the local server from your game, select the Cloud Code toolbar button, then Start Local Server.
Screenshot showing the placement of the Start Local Server button in the toolbar.

The Start Local Server button in the toolbar

Starting the local server automatically compiles and deploys all Cloud Code modules in your project onto that server. The Cloud Code toolbar button, and the window that button opens, displays the state of the local server, for example, idle, starting, running, and its process ID (PID).
Screenshot showing that the Start Local Server button changes to the Stop Local Server button when the server is running.

The Stop Local Server button in the toolbar

Alternatively, you can reload subsequent code changes to your modules onto the started server by restarting the server, or redeploying the desired module through the Deployment window.
Screenshot showing the options menu to redeploy a module in the Deployment window.

Redeploying a module in the Deployment window

With your deployed C# modules on the local server, server calls made from your game in Play mode are now redirected to the local server. Note that the determination of a local versus remote server call is made immediately when you enter Play mode in the Editor. If the local server is running, all server calls are directed locally. Likewise, if the local server isn't running as you enter Play mode, all server calls are directed remotely.
注意
If your project uses Multiplayer Play Mode, the main Editor and additional Editor instances can call the local server. Local Instances need extra configuration. Refer to Multiplayer Play Mode integration.

Reset local state

The default behavior of scoped data on a local server is to persist even after you restart the server or redeploy a module. However, in certain situations, your development scenario might require you to reset the data of your
Player
or
MultiplayerSession
scope.
To reset your local scope state, follow these steps:
  1. Select the Cloud Code toolbar button.
  2. If your server is currently running, select Stop Local Server.
  3. For the Local Server State option, select Clear.
Screenshot showing the Clear button used to reset local state from all scopes.

The Local Server State Clear button in the toolbar

Clearing the state removes all persisted scope data from the local server.
警告
The Clear button doesn't target a specific state. Selecting Clear deletes the state for all scopes.

Find the local server's logs and data

The local server keeps its files in your project folder:

Folder

Contents

Logs/CloudCodeLocalServer
One
server-<date>-<time>.log
file for each local server start. The Editor keeps the log files from the five most recent starts.
Library/CloudCodeLocalServer
The modules deployed to the local server. The
ScopeState
subfolder contains the scope state.
To open a folder, select the Cloud Code toolbar button, then select Show Logs Folder or Show Modules Folder.
Each project has its own local server data, so two projects open at the same time don't share deployed modules or scope state. Deleting the project's
Library
folder also deletes the local server's modules and scope state.
注意
Earlier versions of the Cloud Code package kept the local server's files in a
UnityCloudCode
folder in your user data folder. After you update the package, the local server redeploys your modules and starts with empty scope state the first time it starts. You can delete the old
UnityCloudCode
folder.

Configure additional local server settings

To configure additional settings for local Cloud Code servers, follow these steps:
  1. In the Editor, select Edit > Project Settings > Services > Cloud Code.
  2. To show the local server's internal system logs in the Console window, in addition to your module's own logs, enable Verbose Logging. This setting applies to the entire Cloud Code package and is disabled by default.
  3. If the default values don't work for your project, adjust the following settings in the Local Debugging section before you start the server:
    • Port: The local port on your machine on which the local server listens for calls. The default port is
      14750
      .
    • Secrets File: A JSON asset containing key-value secret pairs to be retrieved in your Cloud Code functions. If no file is assigned, select Create and assign to generate
      Assets/CloudCode/localServerSecret.json
      in the project. If you've already assigned a file, select Edit to open it in your external editor. The file must be a flat JSON object whose values are all strings. Nested objects and numbers aren't supported.
重要
The generated secrets file contains placeholder values only. Replace them with your own values locally, and keep real secrets out of source control. Unity creates the file in your project's
Assets/CloudCode
folder, which your version control system tracks by default.
The following example shows the expected format of a secrets file:
{ "SECRET_EXAMPLE": "replace-with-your-secret-value"}
The following code shows how to update
ModuleConfig
to reference the latest APIs for local Cloud Code servers to retrieve secrets in Cloud Code modules.
using Unity.Services.CloudCode.Apis.Extensions;using Unity.Services.CloudCode.Core;public class ModuleConfig : ICloudCodeSetup{ public void Setup(ICloudCodeConfig config) { config.AddGameApiClient(); }}
注意
In the
Unity.Services.CloudCode.Apis.Extensions
namespace,
config.AddGameApiClient()
is available from
com.unity.services.cloudcode.core
version 0.0.4 and
com.unity.services.cloudcode.apis
version 0.0.24.
In earlier versions, do the following to register the client manually:
config.Dependencies.AddSingleton<IGameApiClient>(GameApiClient.Create());

Additional resources