# Unity Gaming Services（Unity 游戏服务）CLI

> Create, deploy, and manage Cloud Code scripts using the Unity Gaming Services command-line interface.

您可以使用 [Unity Gaming Services（Unity 游戏服务）CLI](https://services.docs.unity.com/guides/ugs-cli/latest/general/overview) 来处理 Cloud Code 脚本。借助 CLI 可以从命令行创建、部署和管理 Cloud Code 脚本。

## 先决条件##prerequisites

要深入了解 CLI，请按照 [Unity Gaming Services（Unity 游戏服务）CLI 入门指南](https://services.docs.unity.com/guides/ugs-cli/latest/general/get-started/install-the-cli/)中的步骤操作。

为了按照该指南操作，必须首先完成以下操作：

1. [安装 UGS CLI](https://services.docs.unity.com/guides/ugs-cli/latest/general/get-started/install-the-cli/)。

2. 按如下方式配置 Project ID 和环境：
   `ugs config set project-id <your-project-id>`
   `ugs config set environment-name <your-environment-name>`

3. 为服务帐户配置 [Cloud Code](https://services.docs.unity.com/docs/service-account-auth/index.html#cloud-code-editor) 和[环境管理](https://services.docs.unity.com/docs/service-account-auth/index.html#unity-environments-admin)所需的角色。请参阅[接受身份验证](https://services.docs.unity.com/guides/ugs-cli/latest/general/get-started/get-authenticated/)。

## 使用 CLI##using-the-cli

请参阅 [Cloud Code 命令行文档](https://services.docs.unity.com/guides/ugs-cli/latest/cloud-code/Cloud%20Code%20Command%20Line/overview)，了解所有命令和选项的完整参考。

> **Note:**
>
> **注意：**`ugs cloud-code scripts` 命令也可按照 `ugs cc s` 的形式使用。

### 部署脚本##deploy-scripts

运行 `new-file` 命令在本地创建包含示例代码的脚本：

```bash
ugs cloud-code scripts new-file <file-name>
```

您可以使用 `Deploy` 命令将本地脚本推送到远程环境并立即发布脚本。本地脚本必须经过部署之后才能用于游戏客户端。

```bash
ugs deploy <path-to-script-file> <path-to-script-file>
```

```bash
ugs deploy <path-to-directory>
```

### 获取脚本##retrieve-scripts

要获取已部署的脚本的相关信息，请运行以下命令：

```bash
ugs cloud-code scripts get <script-name>
```

还可以通过运行以下命令列出当前部署到 Cloud Code 的所有脚本：

```bash
ugs cloud-code scripts list
```

您可以使用 `Fetch` 命令一次性从远程获取多个 Cloud Code 脚本。提供的路径是脚本将被提取到的目录：

```bash
ugs fetch <path>
```

> **Note:**
>
> **注意：** 如果从服务声明了参数，并且服务脚本代码中没有参数，则 Fetch 命令将更新本地脚本代码并向其追加参数。请参阅[参数解析](https://services.docs.unity.com/guides/ugs-cli/latest/cloud-code/Cloud%20Code%20Command%20Line/parameter-parsing/)。

### 环境同步##environment-synchronization

您可以将所有脚本从一个环境移动并部署到另一个环境。

运行以下命令以生成当前环境中所有脚本的存档：

```bash
ugs cloud-code scripts export <out-dir> <file-name>
```

然后，可以通过运行以下命令导入脚本并部署到另一个环境：

```bash
ugs cloud-code scripts import <in-dir> <file-name> --environment-name <environment-name>
```

脚本会自动发布。

> **Note:**
>
> **注意：** 默认情况下，`Import` 不会覆盖您的环境。如果要在导入之前删除现有脚本，请使用 `--reconcile` 标志。

### 删除脚本##delete-script

要删除现有脚本，请运行以下命令：

```bash
ugs cloud-code scripts delete <script-name>
```

## 其他资源##additional-resources

* [部署示例](https://github.com/Unity-Technologies/unity-gaming-services-cli/blob/main/Samples/Deploy/instructions.md#deploy-cloud-code-script)
* 请参阅[参数解析](https://services.docs.unity.com/guides/ugs-cli/latest/cloud-code/Cloud%20Code%20Command%20Line/parameter-parsing/)以了解命令如何处理脚本参数
