如何使用 GameCenter Firebase 身份验证在 Unity 中访问 Auth Token

How to Access Auth Token in Unity with GameCenter Firebase Authentication

我想将 GameCenter Authentication with Firebase 添加到我的 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 的 link。为此,我需要获取 GameCenter 的访问令牌,但好像文档不存在,我不知道如何实现。

https://firebase.google.com/docs/auth/ios/game-center 这可能会回答您的问题。当您从 unity

导出项目时,您可能必须在 XCode 中实现此功能

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

来自UIHandler.cs in the 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 by comparison, which you have to cache the result of Authenticate manually)。

所以在你的代码中,我会进入 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
        });
    }
});

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