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

> Manage trigger configurations from the command line using the Unity Gaming Services command-line interface.

您可以使用 [Unity Gaming Services（Unity 游戏服务）CLI](https://services.docs.unity.com/guides/ugs-cli/latest/general/overview) 来处理触发器。通过 CLI 可以从命令行管理触发器配置。

> **Note:**
>
> **注意**：此 CLI 不支持带有过滤器的触发器。要创建带有过滤器的触发器，请使用 [Triggers Admin API](./rest-api.md) 或 [Unity Dashboard](./unity-dashboard.md)。

## 先决条件##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. 为服务帐户配置 [Triggers](https://services.docs.unity.com/docs/service-account-auth/index.html#triggers-configuration-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

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

> **Note:**
>
> **注意**：`ugs triggers` 命令也可按照 `ugs tr` 的形式使用。

### 部署触发器##deploy-triggers

运行 `new-file` 命令在本地创建触发器配置：

```bash
ugs triggers new-file <file-name>
```

配置文件内容可能如下所示：

```json
{
  "$schema": "https://ugs-config-schemas.unity3d.com/v1/triggers.schema.json",
  "Configs": [
    {
      "Name": "example-trigger",
      "EventType": "com.unity.services.scheduler.{{EVENT_NAME}}.v{{EVENT_VERSION}}",
      "ActionUrn": "urn:ugs:cloud-code:MyScript",
      "ActionType": "cloud-code"
    }
  ]
}
```

还可以修改配置文件来包含多个触发器：

```json
{
  "$schema": "https://ugs-config-schemas.unity3d.com/v1/triggers.schema.json",
  "Configs": [
    {
      "Name": "example-trigger",
      "EventType": "com.unity.services.scheduler.{{EVENT_NAME}}.v{{EVENT_VERSION}}",
      "ActionUrn": "urn:ugs:cloud-code:MyScript",
      "ActionType": "cloud-code"
    },
    {
      "Name": "example-trigger-2",
      "EventType": "com.unity.services.scheduler.{{EVENT_NAME}}.v{{EVENT_VERSION}}",
      "ActionUrn": "urn:ugs:cloud-code:MyScript",
      "ActionType": "cloud-code"
    }
  ]
}
```

您可以使用 `Deploy` 命令将本地触发器配置文件推送到远程环境。

您需要部署配置文件后才能激活触发器：

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

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

如果您将触发器与现有 Cloud Code 脚本或模块相关联，则发出指定 `eventType` 的事件会使触发器触发，并会执行关联的 Cloud Code 脚本或模块。

### 获取触发器##retrieve-triggers

您可以使用 `Fetch` 命令一次性从远程获取多个触发器。

提供的路径是保存触发器的目录：

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

### 删除触发器##delete-triggers

要删除触发器，您可以运行带 `--reconcile` 标志的 `deploy` 命令。

> **Warning:**
>
> **警告：** 协调 (reconcile) 是一项破坏性操作。它会删除所提供路径中不存在的所有触发器。您可以先检查带 `--dry-run` 标志的命令的输出，然后再运行不带该标志的命令。

```bash
ugs deploy --services triggers <path-to-triggers-file> --reconcile
```
