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

> Use the Unity Gaming Services command-line interface to create, deploy, and manage Cloud Code modules.

您可以使用 [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 modules` 命令也可按照 `ugs cc m` 的形式使用。

> **Note:**
>
> **注意：** CLI 目前不支持模块的自定义标签，会将它们忽略。如果要将自定义键/值对附加到模块，请使用 [Cloud Code API](./rest-api.md)。

### 创建模块##create-modules

要使用示例模块在本地创建解决方案，可以运行 `new-file` 命令：

```bash
ugs cloud-code modules new-file <module-name> <module-directory>
```

此命令使用示例模块创建新解决方案。

您可以按原样使用解决方案，也可以对其进行修改以满足您的需求。

> **Note:**
>
> **注意：** 有存储限制。请查看 [Cloud Code 限制](../../reference/limits#cloud-code-c-modules)。

### 部署模块##deploy-modules

`Deploy` 命令可以将 Cloud Code 模块部署到远程环境。此命令支持 `.ccm` 格式的预编译模块和 C# 解决方案，这些模块和解决方案在部署前会自动编译和压缩。

默认情况下，部署过程基于您的解决方案的 `Release` 构建配置。为了减小大小，此配置应避免包含测试项目等额外内容。要了解有关构建配置的更多信息，请访问 [Rider](https://www.jetbrains.com/help/rider/Build_Configurations.html) 或 [Visual Studio](https://learn.microsoft.com/en-us/visualstudio/ide/understanding-build-configurations?view=vs-2022) 的文档。

要一次部署多个模块，请提供多个文件路径或一个包含多个文件的目录：

```bash
ugs deploy <path-to-ccm-file> <path-to-sln-file>
```

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

#### 部署 C# 解决方案##deploy-c-solutions

您可以部署 C# 解决方案并使其在部署前自动编译和压缩。

为了支持编译，解决方案必须包含[主项目](../module-structure#main-project)的发布配置文件。

请参阅具体的集成开发环境文档以了解如何创建发布配置文件：

* JetBrains Rider - [发布配置文件](https://www.jetbrains.com/help/rider/Publishing_Web_Apps.html#publish-profiles)
* Visual Studio - [用于 ASP.NET Core 应用部署的 Visual Studio 发布配置文件 (.pubxml)](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-7.09)
* Visual Studio - [使用 Visual Studio for Mac 发布到文件夹](https://learn.microsoft.com/en-us/visualstudio/mac/publish-folder?view=vsmac-2022\&toc=%2Faspnet%2Fcore%2Ftoc.json\&bc=%2Faspnet%2Fcore%2Fbreadcrumb%2Ftoc.json#customize-publish-options)

在部署解决方案前会自动编译和压缩解决方案。结果将保存到本地临时文件夹（例如 `<temp-folder>/<solution-name>`）。

要部署 C# 解决方案，请提供 `.sln` 文件的路径作为命令的参数：

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

> **Warning:**
>
> **重要**：目前，部署解决方案时，仅支持每个解决方案一个主项目。主项目是包含发布配置文件的项目。不能部署包含多个模块的解决方案。此外，请确保此解决方案的所有项目都位于文件夹层级视图中的解决方案文件下。要了解如何确定解决方案的结构，请参阅[模块结构](../module-structure)。

#### 部署预编译的模块##deploy-pre-compiled-modules

要部署预编译的模块，请提供 `.ccm` 文件的路径作为命令的参数：

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

> **Note:**
>
> **注意：** 对于大多数用例，可以使用[部署解决方案](#deploy-c-solutions)工作流程。作为更高级的工作流程，可以打包和部署预编译的模块。要了解如何将模块压缩为 `.ccm` 文件，请参阅[打包代码](../package-code)。

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

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

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

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

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

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

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

### 获取模块##retrieve-modules

要获取已部署的单个模块的相关信息，请运行以下命令：

```bash
ugs cloud-code modules get <module-name>
```

模块名称是包含在 .ccm 存档中的 .NET 类库的名称。还可以通过运行以下命令列出当前部署到 Cloud Code 的所有模块：

```bash
ugs cloud-code modules list
```

### 删除模块##delete-module

要删除模块，请运行以下命令：

```bash
ugs cloud-code modules delete <module-name>
```
