Unity Google Play游戏服务登录在应用程序无效时停止工作

问题描述:

在我的Unity应用程序中,我使用GPGS插件在Android设备上使用他/她的Google帐户登录用户。我已经按照Play Game Services with Unity示例中的说明配置了游戏和开发者帐户以及插件。 登录和注销完美地工作很多次,直到应用程序停用一段时间。之后,应用程序停止显示同意对话框,我必须重新启动应用程序才能使其重新工作。Unity Google Play游戏服务登录在应用程序无效时停止工作

统一的控制台调试输出:

* [玩游戏插件DLL] 17年10月16日13时25分25秒+03:00 DEBUG:启动验证过渡。 OP:SIGN_IN状态:10

[玩游戏插件DLL] 17年10月16日13点25分25秒+03:00 DEBUG:AuthState ==未经验证的电话身份验证回调失败*

这里是符号-in服务代码

public class PlayGamesSignInService { 

    public void init(bool signIn, Action<bool> signInCallback, bool requestEmail = false, bool requestToken = true, bool requestServerAuthCode = false) { 
     const string methodName = "init"; 

     // Enable debugging output (recommended) 
     PlayGamesPlatform.DebugLogEnabled = true; 

     logDebug(methodName, "Create client configuration"); 
     var builder = new PlayGamesClientConfiguration.Builder(); 
     if (requestEmail) { 
      builder.RequestEmail(); 
     } 
     if (requestToken) { 
      builder.RequestIdToken(); 
     } 
     if (requestServerAuthCode) { 
      builder.RequestServerAuthCode(false); 
     } 
     PlayGamesClientConfiguration config = builder.Build(); 

     logDebug(methodName, "Initialize and activate the platform"); 
     PlayGamesPlatform.InitializeInstance(config); 
     PlayGamesPlatform.Activate(); 

     if (signIn) { 
      logDebug(methodName, "Try silent sign-in"); 
      PlayGamesPlatform.Instance.Authenticate(signInCallback, true); 
     } 
    } 

    public SignInResult signIn(Action<bool> signInCallback) { 
     const string methodName = "signIn"; 

     if (!isSignedIn()) { 
      try { 
       logDebug(methodName, "Sign in with Play Game Services, showing the consent dialog"); 

       PlayGamesPlatform.Instance.Authenticate(signInCallback, false); 

       return SignInResult.Ok; 
      } 
      catch (Exception e) { 
       logError(methodName, "Sign-in error", e); 

       return SignInResult.Error; 
      } 
     } 
     else { 
      logDebug(methodName, "Already signed in"); 

      return SignInResult.InvalidState; 
     } 
    } 

    public SignInResult signOut() { 
     const string methodName = "signIn"; 

     if (isSignedIn()) { 
      try { 
       logDebug(methodName, "Sign out of play games"); 

       PlayGamesPlatform.Instance.SignOut(); 

       return SignInResult.Ok; 
      } 
      catch (Exception e) { 
       logError(methodName, "Sign-out error", e); 

       return SignInResult.Error; 
      } 
     } 
     else { 
      logDebug(methodName, "Not signed in"); 

      return SignInResult.InvalidState; 
     } 
    } 

    public bool isSignedIn() { 
     return PlayGamesPlatform.Instance.localUser.authenticated; 
    } 

    private readonly PlayGamesUser player = new PlayGamesUser(); 

    public PlayGamesUser getSignedInPlayer() { 
     const string methodName = "updatePlayerInfo"; 

     if (!isSignedIn()) { 
      player.id = null; 

      logDebug(methodName, "Not signed in"); 

      return null; 
     } 

     PlayGamesPlatform platform = PlayGamesPlatform.Instance; 
     player.id = platform.GetUserId(); 
     player.displayName = platform.GetUserDisplayName(); 
     player.email = platform.GetUserEmail(); 
     player.imageUrl = platform.GetUserImageUrl(); 
     player.googleIdToken = platform.GetIdToken(); 
     player.serverAuthCode = platform.GetServerAuthCode(); 

     return player; 
    } 

    private const string serviceName = "GoogleSignInService"; 

    private void logDebug(string methodName, string message) { 
     Debug.Log(string.Format("{0}.{1}: {2}", serviceName, methodName, message)); 
    } 

    private void logError(string methodName, string message) { 
     Debug.LogError(string.Format("{0}.{1}: {2}", serviceName, methodName, message)); 
    } 

    private void logError(string methodName, string message, Exception e) { 
     if (e == null) { 
      logError(methodName, message); 
     } 
     else { 
      Debug.LogError(string.Format("GoogleSignInService.{0}: {1} {2}", methodName, message, e)); 
     } 
    } 
} 

public class PlayGamesUser { 
    public String id; 
    public String displayName; 
    public String email; 
    public String imageUrl; 
    public String googleIdToken; 
    public String serverAuthCode; 

    public override string ToString() { 
     return string.Format("id: {0}, displayName: {1}, email: {2}, image: {3}, token={4}, sAuthCode={5};", 
      id, displayName, email, imageUrl, googleIdToken, serverAuthCode); 
    } 
} 

public enum SignInResult { 
    Ok, InvalidState, Error 
} 

这是测试登录服务

void Start() { 
    // Get object instances 
    signInButtonText = GameObject.Find("signInButton").GetComponentInChildren<Text>(); 
    authStatus = GameObject.Find("authStatus").GetComponent<Text>(); 

    GameObject startButton = GameObject.Find("startButton"); 
    EventSystem.current.firstSelectedGameObject = startButton; 

    signInService = new PlayGamesSignInService(); 
    signInService.init(true, SignInCallback, requestEmail: true, requestToken: true, requestServerAuthCode: false); 
} 

public void SignIn() { 
    if (!signInService.isSignedIn()) { 
     signInService.signIn(SignInCallback); 
    } 
    else { 
     signInService.signOut(); 
     signInButtonText.text = "Sign In"; 
     authStatus.text = ""; 
    } 
} 

public void SignInCallback(bool success) { 
    if (success) { 
     PlayGamesUser player = signInService.getSignedInPlayer(); 

     signInButtonText.text = "Sign out"; 
     authStatus.text = "Signed in as: " + player.displayName; 

     Debug.Log("(Lollygagger) Signed in: " + player); 
    } 
    else { 
     Debug.Log("(Lollygagger) Sign-in failed..."); 

     signInButtonText.text = "Sign in"; 
     authStatus.text = "Sign-in failed"; 
    } 
} 

任何建议代码,请?

我会想象,因为这是发生在应用程序已经停用一段时间之后,它可能与登录令牌过期有关。

您可以尝试使用MonoBehaviour.OnApplicationFocus()函数进行重新验证。当应用程序处于焦点再次,可以在尝试标志将被调用

例:

void OnApplicationFocus(bool hasFocus) 
    { 
     if(hasFocus) 
      //authenticate player if the sign in is no longer valid. 

    } 
+0

嗨米歇尔,GoogleIdToken的过期时间1小时,如果你的意思是。当应用程序处于非活动状态3-5分钟时,登录将停止工作。 – Vladimir

+0

啊好吧,你没有指定时间,所以这是第一件事,我的脑海里弹出! :) –

我做了一些更多的研究,结果发现,只有在调试模式下出现问题。我的移动设备上禁用USB调试和统一生成设置关闭所有调试选项后,登录甚至1小时后睡眠正常工作。当然这不是很方便,但至少应用程序在生产模式下工作。