# 创建 Remote Catalog

> 定义商品目录并将其上传到 Remote Catalog 服务。

配置商品目录并将[其上传到 Remote Catalog 服务](#upload-your-product-catalog-to-the-remote-catalog-service)，以便用户可以访问应用程序的最新商品，而无需安装新版本。

如果将商品目录上传到 Remote Catalog，Unity IAP SDK 可以从 `RemoteCatalogProvider` 类中检索商品目录并将商品显示给用户。这样做有以下好处：

* 您可以确保实时分发产品和定价，而无需用户更新其应用程序。
* 不必在多个支付提供商之间配置产品信息。

## 产品和目录列表##products-and-catalog-listings

Unity IAP 使用两个相关概念表示目录：

* **商品**是您授予用户的商品。每个商品都有一个 SKU，也称为 `productId`。
* **目录列表**是由`catalogListingId`标识的商品的可购买版本。一个商品可以有多个目录列表，因此可以用多种方法销售同一商品。

要购买商品，用户会购买其目录列表之一。开始购买或针对特定列表时，请使用 `catalogListingId` 而不是 `productId`。如果未为一个商品定义多个列表，Unity IAP 会创建一个目录列表，其中`catalogListingId`与 SKU 匹配。现有的单一列表目录将继续工作，没有任何更改。

> **Note:**
>
> 在部署商品目录之前，需要安装[部署包](https://docs.unity3d.com/Packages/com.unity.services.deployment@latest)。

> **Warning:**
>
> 对于直接到消费者 (D2C) 付款提供商，请勿使用 IAP Catalog 窗口创建商品。您必须[将商品创建为 IAP 目录项](#create-a-product-catalog-in-the-unity-editor)。同样，不要使用 Codeless IAP 路径，因为 Catalog Editor 中的 **Auto-initialize** 选项不适用于 D2C 支付提供商。

## 定义商品目录##define-your-product-catalog

您可以使用以下方法在远程产品目录中定义有关产品的信息：

* [在 Unity Editor](#create-a-product-catalog-in-the-unity-editor) 中（推荐）
* [从 App Store 或 Google Play 导入现有目录](#import-an-existing-catalog)
* [使用 API](#use-the-api-to-create-a-product-catalog)
* [使用自定义集成](#use-a-custom-deployment-window-integration-to-create-your-product-catalog)

还可以直接在 Unity Cloud Dashboard（Unity 云后台）的 **In-App Purchases（应用内购**）> **Catalog（目录** 下创作目录。

如果使用 Unity Editor 添加商品，Unity 应用内购 (IAP) 会自动处理购买履行过程所需的一些后端逻辑。尤其是在选择[通过后端 API](./implement-backend.md) 实现购买时。

> **Note:**
>
> 要在 Unity Editor UI 中创建商品目录，必须使用 Unity Editor 6.0 或更高版本。如果使用早期版本，可以改为[使用 API](#use-the-api-to-create-a-product-catalog) 创建商品目录。

### 在 Unity Editor 中创建商品目录##create-a-product-catalog-in-the-unity-editor

如果在 Unity Editor 中定义商品，则 Editor 会将商品配置存储在文件中，以便您可以使用按代码配置工作流程来管理它们。

要在 Unity Editor 中创建商品，请执行以下步骤：

1. 要创建目录项，请在目录中右键单击，然后选择 **Create** > **Services** > **IAP Catalog item**。
2. 为目录项命名。这将创建一个目录项资源，并在 Inspector 窗口中打开该资源。
3. 输入目录项的详细信息。有关更多信息，请参阅 [Catalog Item asset Inspector 参考](#catalog-item-asset-inspector-reference)。
4. 选择 **Apply（应用）**。

重复以下步骤为所有商品创建目录项。


**Frame:**
![在 Unity Editor 的 Inspector 窗口中打开的目录项资源的截屏。该图显示了 Catalog Listing Id、Sku、Product Type、Product Details、Pricing Details、Image URL 和 Store ID Overrides 的字段。](/api/media?file=/iap/media/images/catalog-inspector.png)

添加商品后，选择 **View in Deployment 窗口**可在 Deployment 窗口中打开商品目录并将[其上传到 Remote Catalog](#upload-your-product-catalog-to-the-remote-catalog-service)。

还可以直接在 文件中编辑商品详细信息，而不是在 Unity Editor 中编辑。文件中的属性映射到 [Catalog Item asset Inspector 参考](#catalog-item-asset-inspector-reference)中列出的属性。

#### Catalog Item Asset Inspector 参考##catalog-item-asset-inspector-reference

请参阅以下属性来定义 IAP 目录项：

| **属性**                 | **描述**                                                                                                |
| ---------------------- | ----------------------------------------------------------------------------------------------------- |
| --**script**           | 默认填充。                                                                                                 |
| **Catalog Listing ID** | 默认填充（使用文件路径）。在开始购买时用于定向投放此列表的标识符。                                                                     |
| **SKU**                | 默认填充（使用文件名）。                                                                                          |
| **商品类型**               | 选择 IAP 商品的类型。                                                                                         |
| **商品详细信息**             | 输入有关商品的信息，包括： Title 描述 语言 副标题 Badge（文本和图像 URL）                                                        |
| **定价详细信息**             | 输入商品价格信息，包括： 货币代码 金额                                                                                  |
| **图像 URL**             | 输入图像 URL 可指定要为商品显示的图像。                                                                                |
| **商店 ID 覆盖**           | 可选。将列表映射到 App Store 和 Google Play，它们具有不同的命名要求。仅当 SKU 不符合商店要求时才使用这些字段： Apple Override Google Override  |

### 导入现有目录##import-an-existing-catalog

如果已在 App Store 或 Google Play 中配置了目录，则可以将其导入 Unity Editor，而不是重新创建商品。

1. 在 Unity Editor 中，选择 **Services** > **In-App Purchasing** > **Import Catalog**。
2. 选择要导入的商店的 **App Store** 或 **Google Play Store** 选项卡。
3. 根据**步骤 1：获取目录数据**，输入您的商店凭据，然后选择 **Fetch Catalog**：
   * 密钥
   * **秘密范围**
4. 根据**步骤 2：Import 或 Export**，为生成的目录项设置 **Import New** 目标路径，然后选择 **Apply**。

> **Note:**
>
> 任何修改的 SKU 仅更新现有`.ucat`文件，而任何新 SKU 都会在导入目标位置创建一个新`.ucat`文件。

导入目录后，将[其上传到 Remote Catalog 服务](#upload-your-product-catalog-to-the-remote-catalog-service)。

### 使用 API 创建商品目录##use-the-api-to-create-a-product-catalog

如果希望通过 Editor 脚本而不是使用 Unity Editor UI 来管理商品目录，请使用此方法。

首先，必须创建一个 `.asmdef` 文件来包含所有必需的依赖项。
请参阅以下示例以创建`.asmdef`文件：

```json
{
    "name": "MyIAPProject.Authoring",
    "rootNamespace":"MyIAPProject.Authoring",
    "references": [
        "Unity.Services.Core",
        "Unity.Services.Core.Editor",
        "Unity.Services.Core.Internal",
        "Unity.Services.DeploymentApi",
        "Unity.Services.Core.Environments.Editor",
        "UnityEditor.Purchasing.Authoring",
        "UnityEditor.Purchasing.Editor.Authoring.Core",
        "Unity.Purchasing.Editor.Shared",
        "Unity.Purchasing"
    ],
    "includePlatforms": [
        “Editor”
    ]
}
```

然后，可以使用 `ILiveContentConfigClient` API 直接修改 Remote Catalog：

1. 通过 `PurchasingAuthoringServiceProvider` 获取相关服务：
   * `ILiveContentConfigClient`
   * `IEnvironmentsApi`
     `PurchasingAuthoringServiceProvider.GetService<ILiveContentConfigClient>()`
2. 使用所需信息初始化 `ILiveContentConfigClient` 接口。
3. 进行读取/更新目录的调用（这些调用是使用当前登录的用户权限进行的）。

> **Warning:**
>
> 此操作具有破坏性，如果您不发送项目，则该项目不会成为目录的一部分。这也跳过了部署过程通常验证的验证。

请参阅以下代码示例：

```csharp
[MenuItem("IAP/Deployment/Get and print the catalog")]
public static async void Select()
{
    var lcc = PurchasingAuthoringServiceProvider.GetService<ILiveContentConfigClient>();
    var env = PurchasingAuthoringServiceProvider.GetService<IEnvironmentsApi>();
    await lcc.Initialize(
        env.ActiveEnvironmentId.ToString(),
        CloudProjectSettings.projectId,
        CancellationToken.None);
    var items = await lcc.List(CancellationToken.None);
    Debug.Log(string.Join("\n", items.Select(i => $"{i.uSku} - {i.ProductDetails[0].Description}")));
}
```

### 使用自定义 Deployment 窗口集成来创建商品目录##use-a-custom-deployment-window-integration-to-create-your-product-catalog

您可以使用 Deployment 窗口来托管自定义集成，因此您可以创建简单的自定义部署（例如 CSV）或直接在 Remote Catalog 中进行 excel。

有关如何创建简单 Deployment 窗口的信息，请参阅[部署包文档](https://docs.unity3d.com/Packages/com.unity.services.deployment.api@latest?subfolder=/manual/deployment_api.html)。

## 将商品目录上传到 Remote Catalog 服务##upload-your-product-catalog-to-the-remote-catalog-service

使用 Deployment 窗口可将商品上传到 Remote Catalog：

1. 在 Unity Editor 中，选择 **Services** > **Deployment** 以打开 Deployment 窗口。
2. 选择要添加到 Remote Catalog 的商品，然后选择 **Deploy Selected**。


**Frame:**
![Unity Editor 中 Deployment 窗口的截屏，其中显示 .ucat 目录项的列表。](/api/media?file=/iap/media/images/deployment-window.png)

> **Important:**
>
> 在购买时，IAP 会使用您的 Remote Catalog 定义来构建 D2C 支付提供商的 Checkout 数据。IAP 不使用您在 Stripe 或 Coda 中直接添加的任何商品。

## 从远程目录中删除商品##delete-products-from-your-remote-catalog

可通过多种方法在 Unity Editor 中删除远程目录中的项：

* 在 Inspector 窗口中打开目录项，然后选择 **Delete Remote**。
* 打开 Deployment 窗口，右键单击目录项，然后选择 **Delete Remote**。

> **Note:**
>
> 要从 Remote Catalog 中删除重命名的目录项，首先使用原始 SKU 重新创建临时项。然后，您可以通过 Deployment 窗口删除该项，从而成功同步删除。

## 后续步骤##next-steps

本页面是使用 IAP 设置 D2C 支付提供商的工作流程的一部分。要继续此工作流程，请选择以下选项之一：

[Integrate D2C payment providers](./workflow.md#create-your-remote-catalog): 返回到 Integration D2C payment providers with IAP workflow（将 D2C 支付提供商与 IAP 工作流程集成）页面。
[Initialize services to handle the startup sequence](./initialize-services.md): 继续工作流程中的下一步以设置 D2C 支付提供商。
