# Cloud Code JavaScript 文件

> Manage your Cloud Code scripts from the Unity Editor using script files and the Deployment package.

使用 Cloud Code JavaScript 文件从 Unity 编辑器管理您的脚本。您可以使用 [Deployment 包](https://docs.unity3d.com/Packages/com.unity.services.deployment@latest)将这些脚本部署到 Unity Dashboard。

## 先决条件##prerequisites

要使用 Cloud Code 脚本，您必须安装 [Deployment 包](https://docs.unity3d.com/Packages/com.unity.services.deployment@latest) (`com.unity.services.deployment`)。

## 创建 Cloud Code 脚本文件##create-cloud-code-script-files

要创建 Cloud Code 脚本文件，请执行以下操作：

1. 在 Unity 编辑器中，右键单击 Project（项目）窗口，然后选择 **Create（创建）**> **Services（服务）**> **Cloud Code Js Script（Cloud Code Js 脚本）**。

## 文件格式##file-format

文件格式遵循 .js 标准。

> **Warning:**
>
> **重要**：在设置参数之前，必须分配 `module.exports` 属性。

### 示例##example

```javascript
/*
 *  -------- Example Cloud Code Script --------
 *
 *  Roll a 6-sided dice and get back the result
 *
 * --------------------------------------------
 * Additional imports are limited to:
 *  - lodash-4.17
 *  - axios-0.21
 *  - @unity-services/cloud-save-1.0
 *  - @unity-services/economy-2.0
 *  - @unity-services/remote-config-1.0
 */
 // Include the lodash module for convenient functions such as "random"
const _ = require("lodash-4.17");

const NUMBER_OF_SIDES = 6;

/*
 * CommonJS wrapper for the script. It receives a single argument, which can be destructured into:
 *  - params: Object containing the parameters provided to the script, accessible as object properties
 *  - context: Object containing the projectId, environmentId, environmentName, playerId and accessToken properties.
 *  - logger: Logging client for the script. Provides debug(), info(), warning() and error() log levels.
 */
module.exports = async ({ params, context, logger }) => {
  // Log an info message with the parameters provided to the script and the invocation context
  logger.info("Script parameters: " + JSON.stringify(params));
  logger.info("Authenticated within the following context: " + JSON.stringify(context));

  const roll = rollDice(NUMBER_OF_SIDES);

  if (roll > NUMBER_OF_SIDES) {
    // Log an error message with information about the exception
    logger.error("The roll is greater than the number of sides: " + roll);
    // Return an error back to the client
    throw Error("Unable to roll dice!");
  }

  // Return the JSON result to the client
  return {
    sides: NUMBER_OF_SIDES,
    roll: roll,
  };
};

// Functions can exist outside of the script wrapper
function rollDice(sides) {
  return _.random(1, sides);
}
```

如需了解更多创作选项，请参阅[在编辑器中编写脚本](./write-scripts/unity-editor.md)。

## 部署文件##deploy-files

创建 Cloud Code 脚本文件后，您可以将它们部署到您选择的环境中。

要部署文件，请执行以下操作：

1. 在 Unity 编辑器中，选择 **Services（服务）**> **Deployment（部署）**。对于 2022 之前的 Unity 版本，请选择 **Window（窗口）**> **Deployment（部署）**。Deployment（部署）窗口会显示您的本地 Cloud Code 脚本文件。
2. 选择您所选的 Cloud Code 脚本文件，然后选择 **Deploy Selected（部署所选项）**。

您现在可以在 [Unity Dashboard](./write-scripts/unity-dashboard.md) 中编辑您的 Cloud Code 脚本。

如需有关使用 Deployment（部署）窗口的更多信息，请参阅 [Deployment 包](https://docs.unity3d.com/Packages/com.unity.services.deployment@latest)。
