> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 为游戏实现试玩

> Microsoft Store 中面向 GDK 游戏提供的试玩选项 — 在合作伙伴中心中配置基于使用量的免费试玩，并在游戏内实现试玩检查。

你可以为你的游戏配置多种不同类型的免费试玩：

* *基于时间的免费试玩* - 这些试玩在经过一定 *日历* 时间后过期（例如，用户下载试玩后 7 天，无论他们实际使用了多长时间）。
* *基于使用量的免费试玩* - 允许配置让用户在一定 *游戏运行时间* 内运行游戏，然后要求用户购买游戏以继续使用。
* *策展式试玩 (Curated trials)* - 这些试玩没有过期或游戏运行时间限制，但游戏可以限制可玩内容。类似于演示版，但需要下载完整游戏。

<Note>
  基于时间和基于使用量的试玩每个用户每个游戏只能提供一次，并且不得与其他试玩优惠混合使用。已过期或已耗尽的试玩权益会保留在用户账户中，并阻止再获取同一游戏的其他试玩优惠。一旦客户获得试玩，试玩类型无法更改或重置。
</Note>

<Note>
  某些试玩是免费的，但仅限拥有特定订阅的用户使用。
</Note>

本文也适用于这些试玩。

<Note>
  作为 **Free Play Days** 优惠一部分运行的游戏并不是作为实际试玩运行的，而只能通过临时的完整许可证启动。
</Note>

试玩配置和相关结构不适用。

## 在合作伙伴中心配置基于使用量的试玩

<Note>
  仅当你拥有适当权限并且你的游戏使用[限制式授权](/publishing/xstore-commerce/xstore-open-restrictive-licensing)时，才能访问此功能。
</Note>

有关详情，请联系你的 Microsoft 客户经理。

你可以在合作伙伴中心配置基于使用量的免费试玩，而无需修改游戏代码。但是，你需要在以下场景中添加代码：

* 仅允许在商店中获取试玩的用户访问试玩。
* 保存游戏会话之间的游戏状态，并在购买后将用户从试玩到完整版游戏的进度延续。

1. 在 **Submission overview** 页面，转到 **Pricing and availability** 页面。
2. 在 **Free trial** 下，打开下拉菜单，然后选择 **Usage-limited**。
3. 在出现的第二个下拉菜单中，选择用户可以使用你的应用的最长时间。
4. 选择 **Save**。

将游戏发布到 Microsoft Store 后，你将在游戏的产品页面上看到试玩信息。

## 仅允许获得试玩的用户访问

在某些情况下，你可能希望仅允许试玩拥有者运行游戏。此限制对于基于使用量的试玩很重要，因为你不希望其他人为另一个账号耗尽试玩时间。你可以通过检查 [XStoreGameLicense](/reference/system/xstore/xstore_members)`.isTrialOwnedByThisUser` 属性来实现此功能。如果为 `false`，则阻止当前玩家继续，并提示用户获取自己的试玩许可证。

启动试玩游戏时，会显示当前拥有活跃试玩许可证的用户消息：

<img src="https://mintcdn.com/microsoft-4404708b/iADzli99B7EWodAv/images/xstore/usagelimitedtrial.png?fit=max&auto=format&n=iADzli99B7EWodAv&q=85&s=d7ce2cbb02c139f99d5889ed54544e4d" alt="基于使用量的试玩 TCUI：“<Gamertag>，you have less than x hours left”" data-og-width="1920" width="1920" data-og-height="1080" height="1080" data-path="images/xstore/usagelimitedtrial.png" data-optimize="true" data-opv="3" srcset="https://mintcdn.com/microsoft-4404708b/iADzli99B7EWodAv/images/xstore/usagelimitedtrial.png?w=280&fit=max&auto=format&n=iADzli99B7EWodAv&q=85&s=d11e00e9c546a270c8ab17a9981600d5 280w, https://mintcdn.com/microsoft-4404708b/iADzli99B7EWodAv/images/xstore/usagelimitedtrial.png?w=560&fit=max&auto=format&n=iADzli99B7EWodAv&q=85&s=75d1c3815a7b5eec324c2564ef839d98 560w, https://mintcdn.com/microsoft-4404708b/iADzli99B7EWodAv/images/xstore/usagelimitedtrial.png?w=840&fit=max&auto=format&n=iADzli99B7EWodAv&q=85&s=e958634047e60a3904ae7421171837d3 840w, https://mintcdn.com/microsoft-4404708b/iADzli99B7EWodAv/images/xstore/usagelimitedtrial.png?w=1100&fit=max&auto=format&n=iADzli99B7EWodAv&q=85&s=78eee31f32eee6cca22f80c9e518215f 1100w, https://mintcdn.com/microsoft-4404708b/iADzli99B7EWodAv/images/xstore/usagelimitedtrial.png?w=1650&fit=max&auto=format&n=iADzli99B7EWodAv&q=85&s=670602c28fd5ebae7c8a75806cf0e899 1650w, https://mintcdn.com/microsoft-4404708b/iADzli99B7EWodAv/images/xstore/usagelimitedtrial.png?w=2500&fit=max&auto=format&n=iADzli99B7EWodAv&q=85&s=94c27565ad1aaf55b4b661a4f260e418 2500w" />

以下代码示例展示了如何检查 `isTrialOwnedByThisUser`。

```cpp theme={null}
void CALLBACK GameLicenseTrialCheck(XAsyncBlock* asyncBlock)
{
    XStoreGameLicense result{};

    HRESULT hr = XStoreQueryGameLicenseResult(
        asyncBlock,
        &result);

    // Is this a trial?
    if (result.isActive && result.isTrial)
    {
        // Is this trial owned by this user?  
        if (!result.isTrialOwnedByThisUser)
        {
            // The user can't use another user's trial time. Show an error message.
        }
    }
}

void QueryGameLicense(XStoreContextHandle storeContextHandle, XTaskQueueHandle taskQueueHandle)
{
    auto async = new XAsyncBlock{};
     asyncBlock->queue = m_asyncQueue;
    asyncBlock->callback = GameLicenseTrialCheck;

    HRESULT hr = XStoreQueryGameLicenseAsync(
        storeContextHandle,
        async);
}
```

## 为原始试玩用户保存游戏状态

启用 XBOX 服务的游戏，如果具有单人剧情或其他在会话之间跟踪进度的游戏模式，会使用连接存储来保存游戏状态。但是，对于限时试玩，PC 用户可以创建多个 Microsoft 账户，并在超过时间限制后继续游玩并保存进度（使用相同的 XBOX 账号）。有关此场景的更多信息，请参阅[在 PC 上处理商店账号不匹配的场景](/publishing/xstore-commerce/xstore-mismatched-accounts)。

可以通过在保存游戏进度时保存 [XStoreGameLicense](/reference/system/xstore/xstore_members)`.trialUniqueId` 值来防止此漏洞。之后，当用户再次开始玩试玩时，你可以检查 `trialUniqueId` 值是否与第一次运行的值匹配。

此代码假设游戏将 `trialUniqueId` 作为游戏存档的一部分保存，并有一个返回该值的 `GetSavedTrialUniqueId` 函数。

```cpp theme={null}
void CALLBACK GameLicenseTrialCheck(XAsyncBlock* asyncBlock)
{
    XStoreGameLicense result{};

    HRESULT hr = XStoreQueryGameLicenseResult(
        asyncBlock,
        &result);

    // Is this a trial?
    if (result.isActive && result.isTrial)
    {
        // Is this trial owned by this user?
        if (!result.isTrialOwnedByThisUser)
        {
            // The user can't use another user's trial time. Show an error message.
        }
        else
        {
            // Read the trialUniqueId that was saved with the game on the first run.
            char trialUniqueId = GetSavedTrialUniqueId();
            if (trialUniqueId != result.trialUniqueId)
            {
                // Because the IDs don't match, start the game from the beginning.
            }
        }
    }
}
```

## 在开发中测试试玩

若要使用试玩许可证测试你的游戏，游戏必须先在合作伙伴中心配置试玩许可证。

### XBOX

对于本地构建，需要真实许可证。对于 loose 构建和打包构建，需要在 MicrosoftGameConfig 中应用内容 ID 和 EKBID 覆盖。有关更多信息，请参阅[启用授权测试](/publishing/xstore-commerce/xstore-licensing-setup)。

请注意，此处必须使用来自试玩构建的实际 EKBID。要获取 EKBID，你需要使用具有试玩许可证的账号从 Microsoft Store 下载游戏构建。

<img src="https://mintcdn.com/microsoft-4404708b/hprF_XHEe0cnRr8N/images/xstore/pdptrial.webp?fit=max&auto=format&n=hprF_XHEe0cnRr8N&q=85&s=c3119c04473575c1054c89a8dc840976" alt="高亮显示 “Install Free Trial” 按钮的试玩 PDP" width="1920" height="1080" data-path="images/xstore/pdptrial.webp" />

配置完成后，使用从商店获得试玩许可证的测试账号启动，`XStoreGameLicense` 会显示准确的试玩属性。启动构建应显示试玩通知 UI。

### PC

与启用完整许可证的构建方式相同 — 在 MicrosoftGame.config 中使用正确的应用标识，并确保注册表位置中的内容 ID 正确，构建就会遵守登录 Microsoft Store 应用的账号的许可证（试玩或其他）。

## 参考 API 文档

* [XStore（API 内容）](/reference/system/xstore/xstore_members)
  * 结构体
    * [XStoreGameLicense](/reference/system/xstore/xstore_members)

## 另请参阅

[商务概述](/publishing/xstore-commerce/xstore-commerce-overview)

[启用 XStore 开发与测试](/publishing/xstore-commerce/xstore-product-testing-setup)

[启用授权测试](/publishing/xstore-commerce/xstore-licensing-setup)

[XStore API 参考](/reference/system/xstore/xstore_members)


## Related topics

- [启用授权测试](/zh-CN/publishing/xstore-commerce/xstore-licensing-setup.md)
- [GDK 商务系统概述](/zh-CN/publishing/xstore-commerce/xstore-overview.md)
- [试玩概述](/zh-CN/publishing/game-publishing/concepts/availability/trials.md)
- [XStoreGameLicense](/zh-CN/reference/system/xstore/structs/xstoregamelicense.md)
- [为游戏配置定价与可用性](/zh-CN/publishing/game-publishing/tutorial-xbox-managed/how-to-configure-pricing-and-availability.md)
