find_package(com.google.android.gms.games.v2.cREQUIREDCONFIG)// link games_static for -DANDROID_STL=c++_static or default// link games_shared for -DANDROID_STL=c++_sharedtarget_link_libraries(appPUBLICcom.google.android.gms.games.v2.c::games_static)
您的游戏可以通过检索已登录玩家的玩家 ID 来访问该玩家的信息。您可以通过调用 GetPlayerID 函数来检索玩家 ID,如以下示例所示。
#include <assert.h>#include"gni/gni.h"#include"gni/gni_task.h"#include"pgs/pgs_play_games.h"#include"pgs/pgs_players_client.h"// A callback for a GniTask returned from PgsPlayersClient_getCurrentPlayerId.voidOnGetCurrentPlayerIdCompleteCallback(GniTask*task,void*user_data){if(!GniTask_isSuccessful(task)){constchar*error_message=nullptr;GniTask_getErrorMessage(task,&error_message);// Log error message here.GniString_destroy(error_message);GniTask_destroy(task);return;}constchar*result=nullptr;PgsPlayersClient_getCurrentPlayerId_getResult(task,&result);// Log player id here.GniString_destroy(result);GniTask_destroy(task);}// Gets the player ID.voidGetPlayerId(jobjectmain_activity){staticconstPgsPlayersClient*players_client=PgsPlayGames_getPlayersClient(main_activity);GniTask*get_current_player_id_task=PgsPlayersClient_getCurrentPlayerId(players_client);assert(get_current_player_id_task!=nullptr);GniTask_addOnCompleteCallback(get_current_player_id_task,OnGetCurrentPlayerIdCompleteCallback,nullptr);}// Entry point for our test appvoidTestPGSNative(JNIEnv*env,jobjectmain_activity){JavaVM*java_vm;env->GetJavaVM(&java_vm);GniCore_init(java_vm,main_activity);GetPlayerId(main_activity);}
重新启动登录提示
如果玩家拒绝在游戏启动时自动显示初始 Play 游戏服务登录提示,他们可以在游戏会话期间改变主意。只要当前没有玩家登录,您就可以调用 PgsGamesSignInClient_signIn 来重新启动登录提示。
游戏服务器授权
玩家成功登录 Play 游戏服务后,您的游戏客户端可以请求服务器授权代码,您的后端游戏服务器可以使用该代码来与 Play 游戏服务进行安全通信。这样一来,您的游戏服务器就可以检索、更新和存储已登录玩家的数据。您可以通过调用 PgsGamesSignInClient_requestServerSideAccess 函数来检索服务器授权代码。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Get started with Play Games Services for C and C++\n\nThis guide describes how to set up a native C or C++ game project to use the\nPlay Games Services v2 Native SDK and integrate the\n[Sign-in service](/games/pgs/signin). Sign-in integration is required in order\nto integrate other Play Games Services [features](/games/pgs/overview#features)\ninto your game and to integrate Play Games Services into your\n[backend game server](/games/pgs/android/server-access).\n\nSupported features\n------------------\n\nThe Play Games Services v2 Native SDK is in beta and only supports\nthe Sign-in service. It doesn't yet support other Play Games Services\n[features](/games/pgs/overview#features).\n\nAPI reference documentation\n---------------------------\n\nThe header files for the SDK contain reference documentation for the APIs. The\nheader files are located in the `include` folder in the SDK files, which are\navailable after you sync your project with SDK repository.\n\nRequirements\n------------\n\n- A game project that uses native C or C++ as the primary programming language.\n\n- Your game project and development environment must have the\n [Gradle](https://docs.gradle.org/current/userguide/userguide.html)\n build system set up.\n\nBefore you start\n----------------\n\nYou must [set up Play Games Services](/games/pgs/console/setup) in\nGoogle Play Console.\n\nSet up your game project\n------------------------\n\nComplete the following steps to set up your game project.\n\n### Update CMakeLists.txt\n\nIn your `CMakeLists.txt` file, add the following code: \n\n find_package(com.google.android.gms.games.v2.c REQUIRED CONFIG)\n\n // link games_static for -DANDROID_STL=c++_static or default\n // link games_shared for -DANDROID_STL=c++_shared\n target_link_libraries(\n app PUBLIC com.google.android.gms.games.v2.c::games_static)\n\n### Update build.gradle\n\nIn your app-level `build.gradle` file do the following:\n\n- Make sure the [prefab](/reference/tools/gradle-api/4.1/com/android/build/api/dsl/BuildFeatures#prefab) build feature is enabled.\n\n- Add the dependency for the Play Games Services v2 Native SDK:\n\n - `com.google.android.gms:play-services-games-v2-native-c:17.0.0-beta1`\n\nHere's an example: \n\n android {\n ...\n buildFeatures {\n prefab true\n }\n ...\n }\n dependencies {\n ...\n implementation \"com.google.android.gms:play-services-games-v2-native-c:17.0.0-beta1\"\n }\n\n### Update AndroidManifest.xml\n\n1. In your `AndroidManifest.xml` file, define your Play Games Services project\n ID. You can do so by adding the following lines to the file:\n\n \u003cmanifest\u003e\n \u003capplication\u003e\n \u003cmeta-data android:name=\"com.google.android.gms.games.APP_ID\"\n android:value=\"@string/game_services_project_id\"/\u003e\n \u003c/application\u003e\n \u003c/manifest\u003e\n\n2. Create a [string resource](/guide/topics/resources/string-resource#String)\n for your project ID. This allows your game to access the ID at build time.\n To create the resource, create the file\n \u003cvar translate=\"no\"\u003eproject_root\u003c/var\u003e`/app/src/main/res/values/games-ids.xml`,\n and add the following:\n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cresources\u003e\n \u003cstring name=\"game_services_project_id\"\n translatable=\"false\"\u003e\u003cvar translate=\"no\"\u003eadd\u003cspan class=\"devsite-syntax-w\"\u003e \u003c/span\u003eyour\u003cspan class=\"devsite-syntax-w\"\u003e \u003c/span\u003eProject\u003cspan class=\"devsite-syntax-w\"\u003e \u003c/span\u003eID\u003cspan class=\"devsite-syntax-w\"\u003e \u003c/span\u003ehere\u003c/var\u003e\u003c/string\u003e\n \u003c/resources\u003e\n\n3. Build and test your game. If successful, when you launch your game it\n displays a sign-in prompt or a successful sign-in banner.\n\nGet the player ID\n-----------------\n\nYour game can access player information for a signed in player by retrieving\ntheir player ID. You can retrieve a player ID by calling the `GetPlayerID`\nfunction, which is demonstrated in the following example.\n| **Note:** For more information about how Player IDs work, visit the topic [here on\nnext generation Player IDs](/games/pgs/next-gen-player-ids) \n\n #include \u003cassert.h\u003e\n #include \"gni/gni.h\"\n #include \"gni/gni_task.h\"\n #include \"pgs/pgs_play_games.h\"\n #include \"pgs/pgs_players_client.h\"\n\n // A callback for a GniTask returned from PgsPlayersClient_getCurrentPlayerId.\n void OnGetCurrentPlayerIdCompleteCallback(GniTask *task, void *user_data) {\n\n if (!GniTask_isSuccessful(task)) {\n const char *error_message = nullptr;\n GniTask_getErrorMessage(task, &error_message);\n\n // Log error message here.\n\n GniString_destroy(error_message);\n GniTask_destroy(task);\n return;\n }\n\n const char *result = nullptr;\n PgsPlayersClient_getCurrentPlayerId_getResult(task, &result);\n\n // Log player id here.\n\n GniString_destroy(result);\n GniTask_destroy(task);\n }\n\n // Gets the player ID.\n void GetPlayerId(jobject main_activity) {\n static const PgsPlayersClient *players_client =\n PgsPlayGames_getPlayersClient(main_activity);\n\n GniTask *get_current_player_id_task =\n PgsPlayersClient_getCurrentPlayerId(players_client);\n assert(get_current_player_id_task != nullptr);\n GniTask_addOnCompleteCallback(get_current_player_id_task,\n OnGetCurrentPlayerIdCompleteCallback,\n nullptr);\n }\n\n // Entry point for our test app\n void TestPGSNative(JNIEnv *env, jobject main_activity) {\n JavaVM *java_vm;\n env-\u003eGetJavaVM(&java_vm);\n\n GniCore_init(java_vm, main_activity);\n\n GetPlayerId(main_activity);\n }\n\nRe-launch the sign-in prompt\n----------------------------\n\nIf a player declines the initial Play Games Services sign-in prompt that is\nautomatically displayed when your game launches, they may change their mind\nduring the game session. You can re-launch the sign-in prompt by calling\n`PgsGamesSignInClient_signIn` as long as no players are currently signed in.\n\nGame server authorization\n-------------------------\n\nOnce a player successfully signs into to Play Games Services, your game client\ncan request a server authorization code that your backend game server can use to\nsecurely communicate with Play Games Services. This allows your game server to\nretrieve, update, and store data for the signed in player. You can retrieve the\nserver authorization code by calling the\n`PgsGamesSignInClient_requestServerSideAccess` function.\n\nFor more information, see the\n[server access guide](/games/pgs/android/server-access)."]]