Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity在Firebase中使用Async和Wait_C#_Firebase_Unity3d_Firebase Realtime Database_Async Await - Fatal编程技术网

C# Unity在Firebase中使用Async和Wait

C# Unity在Firebase中使用Async和Wait,c#,firebase,unity3d,firebase-realtime-database,async-await,C#,Firebase,Unity3d,Firebase Realtime Database,Async Await,我正在用Unity实现一个使用firebase服务的小游戏。我对Firebase RealtimeDataBase有点问题。 我有一个方法“UpdateScoreAsync”,这个方法在数据库中发布新玩家的记录。现在我用如下代码调用此方法: await UpdateScoreAsync(); LoadScene("ReloadActualScene"); // It starts the game again. public async Task UpdateScoreAsync(string

我正在用Unity实现一个使用firebase服务的小游戏。我对Firebase RealtimeDataBase有点问题。 我有一个方法“UpdateScoreAsync”,这个方法在数据库中发布新玩家的记录。现在我用如下代码调用此方法:

await UpdateScoreAsync();
LoadScene("ReloadActualScene"); // It starts the game again.
public async Task UpdateScoreAsync(string userId, int newScore)
{
    Debug.Log("Actualizando puntuación en la base de datos...");
    await FirebaseDatabase.DefaultInstance.GetReference("string").Child("string").Child("Score").SetValueAsync(newScore).ContinueWith(task => {
        if (task.IsFaulted) {
            ...
        } else if (task.IsCanceled) {
            ...
        } else if (task.IsCompleted) {
            Debug.Log("Score updated");
            ...
        }
    });
}
它工作得很好,但正如您在代码中看到的,当分数完全更新时,新场景将被加载。我的目标是在分数更新时加载新场景,但如果我只是在场景重新加载时删除“等待”游戏中断。我看到我的设备日志(Android),我认为问题在于:

本机线程在没有调用DetachCurrentThread的情况下退出(可能会使用pthread\u key\u create destructor?)(等等)

UpdateScoreAsync方法的对象是一个单例,在重新加载场景时不会破坏。虽然游戏崩溃了,但分数会在数据库中更新

我当前的方法“UpdateScoreAsync”如下所示:

await UpdateScoreAsync();
LoadScene("ReloadActualScene"); // It starts the game again.
public async Task UpdateScoreAsync(string userId, int newScore)
{
    Debug.Log("Actualizando puntuación en la base de datos...");
    await FirebaseDatabase.DefaultInstance.GetReference("string").Child("string").Child("Score").SetValueAsync(newScore).ContinueWith(task => {
        if (task.IsFaulted) {
            ...
        } else if (task.IsCanceled) {
            ...
        } else if (task.IsCompleted) {
            Debug.Log("Score updated");
            ...
        }
    });
}
我尝试改变这种方法,就像这样:

public void UpdateScoreAsync(string userId, int newScore)
{
    Debug.Log("Actualizando puntuación en la base de datos..."); 
    FirebaseDatabase.DefaultInstance.GetReference("string").Child("string").Child("Score").SetValueAsync(newScore).ContinueWith(task => {
        if (task.IsFaulted) {
           ...
        } else if (task.IsCanceled) {
           ...
        } else if (task.IsCompleted) {
           Debug.Log("Score updated");
           ...
        }
    });
}
以及对该方法的调用:

UpdateScoreAsync();
LoadScene("ReloadActualScene");
通过这段新代码,我得到了我之前解释过的错误。有人能帮我吗??我不知道哪里出错了


提前谢谢

根据LoadScene()的Unity文档:

注意:由于加载设置为在下一个渲染帧中完成,因此调用SceneManager.LoadScene将强制完成所有以前的AsynoOperation操作,即使AsyncOperation.allowSceneActivation设置为false。这可以通过使用LoadSceneAsync来避免


尝试改用LoadSceneAsync。

但是,如果我不太了解坏消息,那么我要做的就是在其他异步操作完成之前不加载场景。但我希望在其他操作在后台执行时加载新场景,在异步操作(在前一个场景中抛出)仍在执行时,我应该能够在新场景中播放事件。您尝试过这种方法吗?它起作用了吗?如果没有,发生了什么?我下周会试试!刚才我的电脑坏了。当我测试它时,我会再次发布!提前谢谢!恐怕错误还在继续…:(错误是:在没有合格用户的情况下调用系统进程中的方法:android.app.ContextImpl.sendBroadcast。知道问题是什么吗?看起来与您以前收到的错误消息不同,对吗?