为您的游戏添加活动
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在弃用 Google 登录 API 后,我们将于 2026 年移除 games v1 SDK。2025 年 2 月之后,您将无法在 Google Play 上发布新集成了 games v1 SDK 的游戏。我们建议您改用 games v2 SDK。
虽然采用旧版游戏 v1 集成的现有游戏仍可在未来几年内正常运行,但我们建议您从 2025 年 6 月开始迁移到 v2。
本指南介绍了如何使用 Play 游戏服务 v1 SDK。Play 游戏服务 v2 的 C++ SDK 尚未推出。
本指南介绍了如何在 C++ 应用中使用 Events 服务。
准备工作
请阅读事件游戏概念(如果您尚未执行此操作)。
如需设置 C++ 开发环境以使用 Events 服务,请按照 C++ 使用入门指南中的说明操作。您可以从 SDK 下载页面下载 Play 游戏服务 C++ SDK。
您的游戏必须先在 Google Play 管理中心内定义事件,然后才能访问这些事件。
提交活动
您可以在游戏中添加代码,以便在发生与您游戏相关的事件时发送通知给 Events 服务。您可以在游戏中捕获的事件示例包括:杀死敌人、探索或返回各种游戏区域,或获取游戏内物品。通常,您会在玩家每次执行与事件关联的操作(例如“杀死一只怪物”)时,对事件管理器调用 Increment
方法,以将事件的计数递增 1。
以下示例展示了如何将更新后的事件数提交到 Google Analytics 事件服务。
// Increment the event count when player performs the 'Attack blue
// monster' action.
game_services_->Events().Increment(BLUE_MONSTER_EVENT_ID);
检索事件
如需检索 Google 服务器中存储的特定事件的当前计数值,请调用 Fetch*
方法之一。例如,如果您想在游戏的自定义界面中显示玩家的游戏内统计信息或进度,就可以这样做。
以下示例展示了如何在游戏中检索和记录事件数据。
// Log Events details.
LogI("---- Showing Event Counts -----");
gpg::EventManager::FetchAllCallback callback =
[](gpg::EventManager::FetchAllResponse const &response) {
for (auto i : response.data) {
gpg::Event const &event = i.second;
LogI("Event name: %s count: %d", event.Name().c_str(),
event.Count());
}
};
game_services_->Events().FetchAll(callback);
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Add events to your game\n\nFollowing the deprecation of the\n[Google Sign-In](https://android-developers.googleblog.com/2024/09/streamlining-android-authentication-credential-manager-replaces-legacy-apis.html)\nAPI, we are removing the games v1 SDK in 2026. After February 2025, you will be unable to publish\ntitles that are newly integrated with games v1 SDK, on Google Play. We recommend that you use the\ngames v2 SDK instead. \n\nWhile existing titles with the previous games v1 integrations continue to function for a\ncouple of years, you are encouraged to\n[migrate to v2](/games/pgs/android/migrate-to-v2)\nstarting June 2025. \n\nThis guide is for using the Play Games Services v1 SDK. The C++ SDK for\nPlay Games Services v2 is not yet available.\n\nThis guide shows you how to use the Events service in a C++ application.\n\nBefore you begin\n----------------\n\nIf you haven't already done so, you might find it helpful to review the\n[events game concepts](/games/pgs/events).\n\nTo set up your C++ development environment to use the Events service, follow the instructions in the\n[Getting Started for C++](/games/pgs/v1/cpp) guide. You can download\nthe Play Games services C++ SDK from the [SDK downloads page](/games/pgs/downloads#sdk).\n\nBefore your game can access events, you must define them first in\nthe [Google Play Console](https://play.google.com/apps/publish/).\n\nSubmit an event\n---------------\n\nYou can add code in your game to notify the Events service whenever an event of\ninterest to your game occurs. Examples of events you could capture in your\ngame are: killing enemies, exploring or returning to various\ngame regions, or acquiring in-game items. Typically, you would call\nthe `Increment` method on the event manager to increment an event's count by 1 every time the player\nperforms an action associated with the event\n(for example, \"Killed one monster\").\n\nThe following example shows how you might submit the updated event count to\nthe Events service. \n\n // Increment the event count when player performs the 'Attack blue\n // monster' action.\n game_services_-\u003eEvents().Increment(BLUE_MONSTER_EVENT_ID);\n\nRetrieve events\n---------------\n\nTo retrieve the current count value stored in Google's servers for a specific\nevent, call one of the `Fetch*` methods. You might do\nthis, for example, if you want to show a player's in-game statistics or\nprogress from a custom UI in your game.\n\nThe following example shows how you might retrieve and log the event data in\nyour game. \n\n // Log Events details.\n LogI(\"---- Showing Event Counts -----\");\n gpg::EventManager::FetchAllCallback callback =\n [](gpg::EventManager::FetchAllResponse const &response) {\n for (auto i : response.data) {\n gpg::Event const &event = i.second;\n LogI(\"Event name: %s count: %d\", event.Name().c_str(),\n event.Count());\n }\n };\n game_services_-\u003eEvents().FetchAll(callback);"]]