Unity中如何关闭Google Play Games自动登录功能
解决Google Play登录自动触发问题,改为按钮点击触发
核心修改思路:
- 移除游戏启动时自动调用登录的代码
- 将登录逻辑绑定到UI按钮,让玩家主动触发
具体操作步骤:
删除自动登录触发代码:检查你的脚本中
Start()或Awake()方法,找到调用SingIn();的语句并删除。这一步能彻底取消游戏启动时的自动登录弹窗。给UI按钮绑定登录事件:
- 在Unity编辑器中创建一个UI按钮(比如命名为「Google Play登录」)
- 将你的登录脚本挂载到任意一个活跃的GameObject上(比如新建空物体
LoginManager) - 选中按钮,在Inspector面板的
OnClick()列表中点击+号,把挂载脚本的物体拖入,然后选择脚本中的SingIn()方法
原登录逻辑无需修改(保留即可):
以下是你提供的登录处理代码,只要确保仅通过按钮点击触发SingIn(),就能实现玩家自主选择登录的需求:
public void SingIn() { PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication); } internal void ProcessAuthentication(SignInStatus status) { if (status == SignInStatus.Success) { PlayGamesPlatform.Instance.RequestServerSideAccess(false, ProcessServerAuthCode); } } private void ProcessServerAuthCode(string serverAuthCode) { Debug.Log("Server Auth Code: " + serverAuthCode); var request = new LoginWithGooglePlayGamesServicesRequest { ServerAuthCode = serverAuthCode, CreateAccount = true, TitleId = PlayFabSettings.TitleId }; PlayFabClientAPI.LoginWithGooglePlayGamesServices(request, OnLoginWithGooglePlayGamesServicesSuccess, OnLoginWithGooglePlayGamesServicesFailure); } private void OnLoginWithGooglePlayGamesServicesSuccess(LoginResult result) { Debug.Log("PF Login Success LoginWithGooglePlayGamesServices"); string displayName = PlayGamesPlatform.Instance.GetUserDisplayName(); var request = new UpdateUserTitleDisplayNameRequest { DisplayName = displayName }; PlayerPrefs.SetString("UserName", displayName); PlayFabClientAPI.UpdateUserTitleDisplayName(request, onSuccess => Debug.Log("Display name updated successfully"), onError => Debug.LogError("Error updating display name: " + onError.GenerateErrorReport()) ); SceneManager.LoadScene("Home"); } private void OnLoginWithGooglePlayGamesServicesFailure(PlayFabError error) { Debug.Log("PF Login Failure LoginWithGooglePlayGamesServices: " + error.GenerateErrorReport()); }
额外提示:
如果需要支持游客模式(玩家不登录也能进入游戏),可以添加「游客登录」按钮并单独处理逻辑,或者直接默认跳转至Home场景,让玩家自行选择是否登录。
内容的提问来源于stack exchange,提问作者egor_isky
相关产品推荐
相关产品推荐

