如何使用 GameCenter Firebase 身份验证在 Unity 中访问身份验证令牌

时间:2021-02-28 18:28:16

标签: firebase unity3d game-center

我想将带有 Firebase 的 GameCenter 身份验证添加到我的 Unity 游戏中。但是在文档中没有 GameCenter 部分(只有 Play Games 一个)。 我已经实现了“统一部分”:

public static void GameCenterAuth()
    {
        GameCenterPlatform.ShowDefaultAchievementCompletionBanner(true);
        Social.localUser.Authenticate (GameCenterProcessAuth);
    }

    static void GameCenterProcessAuth(bool success)
    {
        if (success) 
        {
            Social.ReportProgress("Achievement01", 100, (result) => {
            Debug.Log(result ? "Reported achievement" : "Failed to report achievement");
        });

        }
        else
        {
            Debug.Log ("Failed to authenticate");
        }
    }

但它错过了与 Firebase 的链接。为此,我需要获取 GameCenter 的访问令牌,但由于文档不存在,我不知道如何实现。

3 个答案:

答案 0 :(得分:1)

希望 Unity 文档中没有太多漏洞,但是当您找到漏洞时,您通常可以在 Unity Quickstarts 中找到示例代码(因为这些也用于验证对 Unity SDK 的更改)。< /p>

来自 UIHandler.cs 中的 Auth quickstart

    public Task SignInWithGameCenterAsync() {
      var credentialTask = Firebase.Auth.GameCenterAuthProvider.GetCredentialAsync();
      var continueTask = credentialTask.ContinueWithOnMainThread(task => {
        if(!task.IsCompleted)
          return null;

        if(task.Exception != null)
          Debug.Log("GC Credential Task - Exception: " + task.Exception.Message);

        var credential = task.Result;

        var loginTask = auth.SignInWithCredentialAsync(credential);
        return loginTask.ContinueWithOnMainThread(HandleSignInWithUser);
      });

      return continueTask;
    }

GameCenter 的奇怪之处在于您不直接管理凭证,而是您request it from GameCenter(相比之下,您必须Here's PlayGames)。

所以在你的代码中,我会进入 if (success) 块并添加:

GameCenterAuthProvider.GetCredentialAsync().ContinueWith(task => {
    // I used ContinueWith
    // be careful, I'm on a background thread here.
    // If this is a problem, use ContinueWithOnMainThread
    if (task.Exception != null) {
        FirebaseAuth.GetInstance.SignInWithCredentialAsync(task.Result).ContinueWithOnMainThread(task => {
            // I used ContinueWithOnMainThread
            // You're on the Unity main thread here, so you can add or change scene objects
            // If you're comfortable with threading, you can change this to ContinueWith
        });
    }
});

答案 1 :(得分:0)

https://firebase.google.com/docs/auth/ios/game-center 这可能会回答您的问题。当你从 unity 导出项目时,你可能需要在 XCode 中实现这个功能

答案 2 :(得分:0)

为了完成 Patrick 的回答,我找到了 machahamster GitHub 存储库,他们在 Unity 上使用 Game Center Auth 和 Firebase。 如果它可以帮助某人: https://github.com/google/mechahamster/blob/b5ab9762cc95a2a36156d0cbd1dc08fb767c4080/Assets/Hamster/Scripts/Menus/GameCenterSignIn.cs#L45