Unity集成Google Play Games:成就与排行榜无法更新问题
Unity集成Google Play Games后成就/排行榜无法更新,报错"Must authenticate first"
问题详情
我在Unity开发的游戏里集成Google Play Games实现成就和排行榜功能,按教程完成配置并调整代码后,运行时顶部能显示Google Play Games个人资料,但成就和排行榜数据无法更新。排查后在Android Logcat中发现提交排行榜时出现以下错误:
2023.06.07 12:22:21.015 6415 6450 Error Unity Must authenticate first 2023.06.07 12:22:21.015 6415 6450 Error Unity UnityEngine.SocialPlatforms.Local:VerifyUser() 2023.06.07 12:22:21.015 6415 6450 Error Unity UnityEngine.SocialPlatforms.Local:ReportScore(Int64, String, Action`1) 2023.06.07 12:22:21.015 6415 6450 Error Unity MoneyScript:AddMoney(Int32) 2023.06.07 12:22:21.015 6415 6450 Error Unity SceneManagerPlus:WinGo() 2023.06.07 12:22:21.015 6415 6450 Error Unity UnityEngine.Events.UnityEvent:Invoke() 2023.06.07 12:22:21.015 6415 6450 Error Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1) 2023.06.07 12:22:21.015 6415 6450 Error Unity UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean) 2023.06.07 12:22:21.015 6415 6450 Error Unity UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents() 2023.06.07 12:22:21.015 6415 6450 Error Unity UnityEngine.EventSystems.StandaloneInputModule:Process()
相关代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using GooglePlayGames; using GooglePlayGames.BasicApi; using UnityEngine.SocialPlatforms; public class googleplayservices : MonoBehaviour { public void Start() { PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication); } internal void ProcessAuthentication(SignInStatus status) { if (status == SignInStatus.Success) { // Continue with Play Games Services // print(PlayGamesPlatform.Instance.GetUserId()); PlayGamesPlatform.Instance.RequestServerSideAccess( /* forceRefreshToken= */ false, code => { Debug.Log(code); SignInWithGoogleAsync(code); }); } else { // Disable your integration with Play Games Services or show a login button // to ask users to sign-in. Clicking it should call PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication); Debug.Log("No"); } } public void SignInWithGoogleAsync(string idToken) { // Your implementation goes here... } public void Osiagniecie(string id, int wartosc) { Social.ReportProgress(id, wartosc, (bool success) => { if (success) { Debug.Log("Score Update"); } }); } public void LeaderBoard(string id, int wartosc) { Social.ReportScore(wartosc, id, (bool success) => { if (success) { Debug.Log("Score Update"); } else { Debug.Log("Nie udało się przesłać"); } }); } }
解决方案
1. 绑定Social平台到PlayGamesPlatform
Unity的Social类默认未关联Google Play Games平台,初始化时需要显式激活并绑定:
public void Start() { // 激活PlayGamesPlatform并设置为Social的实现 PlayGamesPlatform.Activate(); PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication); }
2. 校验认证状态后再上报数据
报错核心原因是调用成就/排行榜接口时,用户未完成认证。添加状态标记,确保只有认证成功后才允许上报:
private bool isAuthenticated = false; internal void ProcessAuthentication(SignInStatus status) { if (status == SignInStatus.Success) { isAuthenticated = true; // 原有其他代码... } else { isAuthenticated = false; // 原有其他代码... } } public void Osiagniecie(string id, int wartosc) { if (!isAuthenticated) { Debug.LogError("用户未认证,无法提交成就"); return; } Social.ReportProgress(id, wartosc, (bool success) => { if (success) { Debug.Log("成就提交成功"); } else { Debug.Log("成就提交失败"); } }); } public void LeaderBoard(string id, int wartosc) { if (!isAuthenticated) { Debug.LogError("用户未认证,无法提交排行榜分数"); return; } Social.ReportScore(wartosc, id, (bool success) => { if (success) { Debug.Log("分数提交成功"); } else { Debug.Log("分数提交失败"); } }); }
3. 处理异步认证的时序问题
RequestServerSideAccess是异步操作,若你的SignInWithGoogleAsync涉及额外服务器认证,需确保这部分完成后再触发上报逻辑;如果不需要服务器端访问,可暂时注释该代码块,先测试基础功能是否正常。
4. 验证Google Play Console配置
- 确认游戏已关联正确的OAuth客户端ID
- 确认成就、排行榜ID与代码中使用的完全一致
- 测试账号已添加到Google Play Console的测试账号列表中
内容的提问来源于stack exchange,提问作者Adas Adasiek
相关产品推荐
相关产品推荐

