# 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** (デプロイ) を選択します。Unity のバージョンが 2022 より前の場合は、**Window** (ウィンドウ) > **Deployment** (デプロイ) を選択します。Deployment (デプロイ) ウィンドウに、ローカルの Cloud Code スクリプトファイルが表示されます。
2. 選択した Cloud Code スクリプトファイルを選択し、**Deploy Selected** (選択したアイテムのデプロイ) を選択します。

これで、Cloud Code スクリプトを [Unity Dashboard](./write-scripts/unity-dashboard.md) で編集できます。

Deployment (デプロイ) ウィンドウの操作について詳しくは、[Deployment パッケージ](https://docs.unity3d.com/Packages/com.unity.services.deployment@latest) を参照してください。
