【问题标题】:Firebase Unity Initialization not workingFirebase Unity 初始化不起作用
【发布时间】:2023-03-25 21:05:01
【问题描述】:

我正在关注 firebase 教程,但无法完成第一步。 它说要像这样初始化firebase:

Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
        var dependencyStatus = task.Result;
        if (dependencyStatus == Firebase.DependencyStatus.Available)
        {
            // Set a flag here indiciating that Firebase is ready to use by your
            // application.
        }
        else
        {
            UnityEngine.Debug.LogError(System.String.Format(
              "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
            // Firebase Unity SDK is not safe to use here.
        }
    });

但是一旦碰到第一行代码,它就会跳过整个事情并且不执行。

我已经添加了 google-services.json。我安装了最新的 firebaseunity.package。

有什么想法吗??

【问题讨论】:

  • 这些代码行检查(并可选地更新)Google Play 服务的版本,因此忽略它们应该没问题。之后您可以使用任何与 Firebase 相关的方法吗?

标签: c# firebase unity3d


【解决方案1】:

CheckAndFixDependenciesAsync() 是一个异步操作。这意味着它在与您的其他执行代码并行的不同线程上运行。这样做是为了让您的游戏不会在 Firebase SDK 在后台解决问题(例如加载 google-services.json)时冻结。

一旦 Firebase 完成,它将调用 {} 中的代码,位于“task =>”之后,这是一个由 Firebase 调用的回调。你也可以这样写:

private void Awake() {
   Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(CalledAfterSomeTime);
}


private void CalledAfterSomeTime(Task<DependencyStatus> task) {
  var dependencyStatus = task.Result;
  if (dependencyStatus == Firebase.DependencyStatus.Available) {
    // Set a flag here indiciating that Firebase is ready to use by your
    // application.
  }
  else
  {
    UnityEngine.Debug.LogError(System.String.Format(
    "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
    // Firebase Unity SDK is not safe to use here.
  }
}

【讨论】:

  • 虽然其余的似乎是正确的,但应该指出的是,异步并不一定意味着它在不同的线程上运行,而且在大多数情况下它不会。
【解决方案2】:

您可以通过强制在 Unity 的主线程中完成您的 ContinueWith 语句来解决您的问题。为此,只需使用ContinueWithOnMainThread(在namespace:Firebase.Extensions 中可用)而不是ContinueWith

例子:

void Start()
{
    FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
    {
        //...
    });
}

【讨论】:

    猜你喜欢
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多