---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://yandex.com/dev/games/doc/en/sdk/sdk-about.md
  - https://yandex.com/dev/games/doc/hi/sdk/sdk-about.md
  - https://yandex.com/dev/games/doc/ko/sdk/sdk-about.md
  - https://yandex.com/dev/games/doc/ru/sdk/sdk-about.md
  - https://yandex.com/dev/games/doc/tr/sdk/sdk-about.md
  - https://yandex.com/dev/games/doc/vi/sdk/sdk-about.md
  - https://yandex.com/dev/games/doc/zh/sdk/sdk-about.md
  - href: zh/sdk/sdk-about.md
    type: text/markdown
    title: Markdown version
  - href: ../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/games/doc/zh/llms.txt

# 连接和使用

<!-- source: zh/_includes/script-common.md -->
<!-- source: zh/_includes/script/index-js.md -->

<!-- endsource: zh/_includes/script/index-js.md -->

<!-- source: zh/_includes/script/requirements-js.md -->

<!-- endsource: zh/_includes/script/requirements-js.md -->

<!-- source: zh/_includes/script/image-modal-js.md -->

<!-- endsource: zh/_includes/script/image-modal-js.md -->
<!-- endsource: zh/_includes/script-common.md -->

## 连接 {#connect}

{% note alert %}

为了让您的游戏成功通过审核，请指定连接 Yandex&nbsp;Games SDK 的当前路径：

- 如果您通过[开发者控制台](https://games.yandex.com/console){.external}将游戏存档上传到 Yandex 服务器，请指定[相对路径](#yandex-server)。这是推荐的方式。
- 如果您使用通过自己的域名进行集成，请指定[绝对路径](#iframe)。

{% endnote %}

连接 Yandex&nbsp;Games SDK 有两种等效的方式：

- 通过 `
    ```

    使用以下属性：

    - `async` —— 用于非阻塞加载。
    - `onload` —— 用于在脚本加载后执行代码。

    在脚本加载后启动 `initSDK` 的代码示例。`initSDK` 指的是 [SDK 初始化](#use)：

    ```html showLineNumbers
    <!-- Yandex Games SDK -->
    
    ```

- 动态加载

    在您的 `
```

## 使用 {#use}

脚本加载后，使用 `YaGames` 对象的 `init()` 方法初始化 SDK。

{% note tip %}

在 `YaGames.init()` 和 [ysdk.getPayments()](https://yandex.com/dev/games/doc/zh/sdk/sdk-purchases.md#install) 中可以传递可选参数 `signed: boolean`，该参数用于[防作弊保护](https://yandex.com/dev/games/doc/zh/sdk/sdk-purchases.md#signature)。参数值的选择取决于支付处理的位置：

- 如果在客户端处理 —— 调用方法时不传递 `signed: boolean` 参数或传递 `signed: false`。购买方法将返回明文数据。
- 如果在服务器端处理 —— 传递 `signed: true`。在这种情况下，[payments.getPurchases()](https://yandex.com/dev/games/doc/zh/sdk/sdk-purchases.md#getpurchases) 和 [payments.purchase()](https://yandex.com/dev/games/doc/zh/sdk/sdk-purchases.md#payments-purchase) 方法响应中的所有数据仅以加密形式在 `signature` 参数中返回。

{% endnote %}

{% list tabs %}

- 客户端处理

    使用默认参数初始化（`signed: false`）：

    ```javascript
    const ysdk = await YaGames.init();
    ```

- 服务器端处理

    使用 `signed: true` 参数初始化：

    ```javascript
    const ysdk = await YaGames.init({ signed: true });
    ```

{% endlist %}

&nbsp; {.empty}

## 验证 {#check}

{% note warning %}

在执行 [YaGames.init()](#use) 之前必须连接 `/sdk.js` 脚本。

{% endnote %}

使用加载器检查 SDK 是否正确连接：

1. 使用[debug面板](https://yandex.com/dev/games/doc/zh/console/debug-panel.md)启动游戏：

   <!-- source: zh/_includes/requirements/start-debug-panel.md -->
   {% list tabs %}

   - 通过开发者控制台
       1. 打开 [Yandex游戏控制台](https://games.yandex.com/console){.external}。
       1. 选择所需的游戏。
       1. 在左上角点击 **Open with debug panel**。

   - 通过地址栏
       1. 打开所需的游戏。
       1. 在浏览器地址栏末尾添加 `&debug-mode=16`。

          链接示例：`https://yandex.com/games/app/XXXX?debug-mode=16`，其中 `XXXX` —— 游戏的唯一标识符。

   {% endlist %}
   <!-- endsource: zh/_includes/requirements/start-debug-panel.md -->

2. 在左下角检查[加载器](https://yandex.com/dev/games/doc/zh/console/debug-panel.md#loader)指示器的值：

    - `W` —— 等待初始化。
    - `IT` —— SDK 加载器已正确初始化。
    - `IF` —— 使用的是旧版加载器。请按照[文档](#connect)加载 SDK。

## 问题解决 {#faq}

### Uncaught ReferenceError: YaGames is not defined {#yagames-not-defined}

请注意 `sdk` 脚本的连接顺序：必须在执行 `YaGames.init()` 之前连接。

### Uncaught ReferenceError: ysdk is not defined {#ysdk-not-defined}

您尝试在 SDK 初始化之前使用 SDK 方法（广告、购买等）。可以在调试模式下通过控制台中的 `Initialized` 消息跟踪初始化时刻。要控制调用顺序，请在调用方法之前添加 SDK 初始化：

```javascript showLineNumbers
const ysdk = await YaGames.init();

ysdk.adv.showFullscreenAdv();
```

### SDK 连接示例 {#connection-example}

```html showLineNumbers
<!-- Yandex Games SDK -->


```

---

<!-- source: zh/_includes/sdk-support.md -->
{% note info %}

技术支持团队将协助您将已完成的游戏发布到 Yandex 游戏平台。关于开发和测试方面的具体问题，其他开发人员将在[Discord 频道](https://discord.com/invite/wU4p3whr4T){.external}中进行回答。

{% endnote %}

如果您遇到 Yandex Games SDK 方面的问题或有其他问题想要咨询，请联系支持部门：

<!-- source: zh/_includes/button-chat.md -->
<a href="https://yandex.com/chat/#/user/a4fa5c06-75db-9b38-6eea-b1673785f7d5">
  <span class="button">写入聊天信息</span>
</a>
<!-- endsource: zh/_includes/button-chat.md -->
<!-- endsource: zh/_includes/sdk-support.md -->
