Firebase Unity初始化不起作用

时间:2018-08-01 20:49:51

标签: c# firebase unity3d

我正在遵循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。

任何想法?

2 个答案:

答案 0 :(得分:0)

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.
  }
}

答案 1 :(得分:0)

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

示例:

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