首先,我正在开发一个Unity游戏,该游戏在游戏启动时对用户进行身份验证。我的构建环境是android。我正在将Firebase authentication用于Google Play游戏服务来验证用户身份。
[当游戏在我的Android设备或仿真器中启动时,它可以验证Play游戏服务以及与Firebase的连接(我正在获取分析数据)。但是,当我将PlayGames AuthCode传递到Firebase.Auth Credentials中时,它将停止执行代码(我已为其调试日志)。它不会在LogCat中引发任何错误,除非
Firebase | server_auth_code
我尝试在网络上搜索其他问题,但一无所获。我在Google API控制台上检查了播放器设置,firebase设置,OAuth 2.0凭据中的密钥,甚至从我的Google Play控制台中检查了密钥(我目前不使用它)。我什至在游戏服务中检查了我的测试用户的电子邮件地址,并尝试了多个Google Play游戏帐户。但是问题仍然存在。
我在另一个统一项目中使用类似的脚本,在该项目中,身份验证就像一个超级按钮。我尝试在此处使用相同的脚本,并最终遇到以下问题:here。但是,我通过删除所有软件包并重新将它们重新导入为一个单位来解决此问题,并在脚本中更改了我的调用函数。现在,我被这个问题困扰。这是cs文件:
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
using System.Threading.Tasks;
public class SetFirebase : MonoBehaviour
{
string authCode;
void Start()
{
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().
RequestServerAuthCode(false /* Don't force refresh */).Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.Activate();
Social.localUser.Authenticate((bool success) =>
{
if (success)
{
authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
Debug.Log("PlayGames successfully authenticated!");
Debug.Log("AuthCode: " + authCode);
}
else
{
Debug.Log("PlayGames SignIn Failed");
}
});
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
var dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available)
{
Debug.Log("Firebase Ready!!!");
RunFirebase();
}
else
{
Debug.LogError(System.String.Format("Could not resolve all Firebase dependencies: {0}", dependencyStatus));
}
});
}
private void RunFirebase(){
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
Debug.Log("init firebase auth ");
Firebase.Auth.Credential credential = Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode);
Debug.Log(" passed auth code ");
auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInOnClick was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInOnClick encountered an error: " + task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("SignInOnClick: User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId);
});
}
}
我的LogCat会执行所有操作,直到“ init firebase auth”,但不执行“ passed auth code”,因此我知道在传递凭据时会遇到一些问题。它也不会在auth.SignInWithCredentialAsync(credential)中运行任何内容。
任何帮助或建议,我们将不胜感激。谢谢。
我可能建议两件事:
1]将ContinueWith
替换为ContinueWithOnMainThread
。这是一个Firebase Extension,它将确保您的逻辑在Unity主线程上运行(这通常可以解决许多Unity特定的问题)。我将详细介绍here。
2]您的逻辑可能在Authenticate
回调和CheckAndFixDependenciesAsync
延续之间具有竞争条件。这些不一定会按照您在逻辑中看到它们的顺序运行。
如果我正在构建此系统,则可能更喜欢使用协程和custom yield instruction:
class Authenticate : CustomYieldInstruction
{
private bool _keepWaiting = true;
public override bool keepWaiting => _keepWaiting;
public Authenticate(Social.ILocalUser user) {
user.Authenticate((bool success)=>{
/* old authentication code here */
_keepWaiting = false;
});
}
}
然后在协程中有类似的东西:
private IEnumerator InitializeCoroutine() {
/* old authentication code */
// I'm ignoring error checking for now, but it shouldn't be hard to figure in.
// I'm mostly going from memory now anyway
// start both authentication processes in parallel
var authenticate = new Authenticate(Social.localUser);
var firebaseDependenciesTask = FirebaseApp.CheckAndFixDependenciesAsync();
// wait on social
yield return authenticate;
// wait on Firebase. If it finished in the meantime this should just fall through
yield return new WaitUntil(()=>firebaseDependenciesTask.IsComplete);
RunFirebase();
}
这样,我的逻辑看起来大致是同步的,同时仍保持您所依赖的系统的异步性(拼写检查由我组成这个词),避免了使用ContinueWith
时出现的与线程相关的问题。让我知道是否有帮助!
-帕特里克