---
metadata:
  - name: generator
    content: Diplodoc Platform v5.54.4
alternate:
  - https://yandex.com/dev/games/doc/en/sdk/cocos/ysdk.md
  - https://yandex.com/dev/games/doc/hi/sdk/cocos/ysdk.md
  - https://yandex.com/dev/games/doc/ko/sdk/cocos/ysdk.md
  - https://yandex.com/dev/games/doc/ru/sdk/cocos/ysdk.md
  - https://yandex.com/dev/games/doc/tr/sdk/cocos/ysdk.md
  - https://yandex.com/dev/games/doc/vi/sdk/cocos/ysdk.md
  - https://yandex.com/dev/games/doc/zh/sdk/cocos/ysdk.md
  - href: zh/sdk/cocos/ysdk.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

# Yandex Games SDK API

<!-- 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 -->

安装插件后，您会发现对象 `ysdk`，它是 [同名的 Yandex 游戏 SDK 对象](https://yandex.com/dev/games/doc/zh/sdk/sdk-game-events.md) 的类型化等价物。它允许您访问 SDK 的所有方法。

以下是如何使用 Yandex 游戏 SDK 的 [环境变量](https://yandex.com/dev/games/doc/zh/sdk/sdk-environment.md) 的示例。

```tsx showLineNumbers
import { Component } from "cc";
import { ysdk } from "db://yandex-games-sdk/ysdk";
const { ccclass, property } = _decorator;

@ccclass("YourGameComponent")
export class YourGameComponent extends Component {
  start() {
    console.debug(`App ID: ${ysdk.environment.app.id}`);
    console.debug(`User Language: ${ysdk.environment.i18n.lang}`);
    console.debug(`URL Payload: ${ysdk.environment.payload}`);
  }

  update(deltaTime: number) {}
}
```

## 用于显示广告的组件 {#ads-display-component}

例如，我们创建一个按钮组件，用于启动带有奖励的视频广告。

此外，我们定义了方法 `onReward()` 并将其作为回调传递给 `ysdk.adv.showRewardedVideo()` 方法，以实现获取奖励事件的逻辑。

```tsx showLineNumbers
import { _decorator, Button, Component } from "cc";
import { ysdk } from "db://yandex-games-sdk/ysdk";
const { ccclass, property, requireComponent } = _decorator;

@ccclass("RewardADButton")
@requireComponent(Button)
export class RewardADButton extends Component {
  start() {
    this.node.on("click", this.onClick.bind(this));
  }

  onClick() {
    const callbacks = {
      onRewarded: this.onReward.bind(this),
    };

    ysdk.adv.showRewardedVideo({ callbacks });
  }

  onReward() {
    // 奖励用户。
  }
}
```

在 Yandex 平台上点击带有此组件的按钮时，您将看到广告。为了简化测试，请参阅 [测试](https://yandex.com/dev/games/doc/zh/sdk/cocos/testing.md) 部分。


---


<!-- source: zh/_includes/button-git.md -->
<a href="https://github.com/yandex-games-plugins/cocos">
  <span class="button">存储库</span>
</a>
<!-- endsource: zh/_includes/button-git.md -->
