简体   繁体   English

如何从Unity中的firebase读取值?

[英]How to read value from firebase in Unity?

I can't really understand how to read any values from firebase.我真的无法理解如何从 firebase 读取任何值。 In google documentation it is said that I need to use GetValueAsync().ContinueWith(), but it works rather weird.在谷歌文档中,据说我需要使用 GetValueAsync().ContinueWith(),但它的工作原理很奇怪。 So, if I use:所以,如果我使用:

FirebaseDatabase.DefaultInstance.GetReference("Leaders").GetValueAsync().ContinueWith(task => {
    if (task.IsFaulted) {
      Debug.Log("error");
    }
    else if (task.IsCompleted) {
      DataSnapshot snapshot = task.Result;
      Debug.Log(snapshot.Value.ToString());
    }
  });

then it prints true value and all is ok.然后它打印真值,一切正常。 But if i'm trying to find a GameObject with this name, nothing happens.但是,如果我试图找到具有此名称的 GameObject,则什么也不会发生。

else if (task.IsCompleted) {
  DataSnapshot snapshot = task.Result;
  Debug.Log(GameObject.Find(snapshot.Value.ToString()).name);
} 

it doesn't print anything.它不打印任何东西。 It stoppes on this line, but without any errors or messages.它停在这条线上,但没有任何错误或消息。 And this problem happens every time I try to do smth with snapshot.每次我尝试使用快照执行 smth 时都会发生这个问题。 A very simple thing, but i can't imagine what can be wrong??一件非常简单的事情,但我无法想象会有什么问题?? Please help.请帮忙。

I wrote an article and recorded a video on handling multitasking in Unity, and you should definitely check those out if you curious about all your options.写了一篇文章录制了关于在 Unity 中处理多任务处理的视频,如果您对所有选项感到好奇,一定要看看这些。

For your issue specifically, I'd suggest simply changing ContinueWith to ContinueWithOnMainThread .对于您的问题,我建议只需将ContinueWith更改为ContinueWithOnMainThread This is a drop in replacement.这是替代品的下降。

FirebaseDatabase.DefaultInstance.GetReference("Leaders").GetValueAsync().ContinueWithOnMainThread(task => {
    if (task.IsFaulted) {
      Debug.Log("error");
    }
    else if (task.IsCompleted) {
      DataSnapshot snapshot = task.Result;
      Debug.Log(snapshot.Value.ToString());
    }
  });

Which brings me to the explanation of what's happening:这让我解释了正在发生的事情:

Something that's tricky about Firebase and Unity is that Firebase tends to perform work on a background thread to avoid blocking your game for long running network operations. Firebase 和 Unity 的棘手之处在于 Firebase 倾向于在后台线程上执行工作,以避免因长时间运行的网络操作而阻塞您的游戏。 Unlike APIs like Unity's UnityWebRequest , it's left to developers to work out how and when to move logic back into the game thread.Unity 的 UnityWebRequest等 API 不同,开发人员需要确定如何以及何时将逻辑移回游戏线程。 This means that if you wanted to do more work in the background with the result of this operation you could, but it also means that you risk very unexpected behaviour if you start to do non-thread-safe operations in the continuation.这意味着如果你想在后台用这个操作的结果做更多​​的工作,你可以,但也意味着如果你开始在延续中执行非线程安全的操作,你会冒非常意外的行为风险。

Unity protects some functions, such as GameObject.Find , from access in a background thread -- Often raising an exception when it detects something untoward. Unity 保护某些函数(例如GameObject.Find )免于在后台线程中访问——当它检测到一些不合适的东西时,通常会引发异常。 ContinueWith will start executing wherever the long running task finished, whereas ContinueWithOnMainThread will push your continuation logic into a queue that gets executed on the main thread. ContinueWith将在长时间运行的任务完成时开始执行,而ContinueWithOnMainThread会将您的延续逻辑推入在主线程上执行的队列中。 This is generally efficient and will make sure that any Unity logic you execute in your continuation occurs at an expected time.这通常是有效的,并且将确保您在延续中执行的任何 Unity 逻辑都在预期的时间发生。

You may opt to convert a task into a coroutine by writing:您可以选择通过编写以下内容将任务转换为协程

var task = FirebaseDatabase.DefaultInstance.GetReference("Leaders").GetValueAsync();
yield return new WaitUntil(()=>task.IsComplete);
if (task.Exception) {
    // something bad happened, handle it
    yield break;
}
var snapshot = task.Result;

Which would give you even more control over where your logic executes in your game and provides a bit more memory safety.这将使您能够更好地控制您的逻辑在游戏中的执行位置,并提供更多的内存安全性。 You could also use async/await to make the code look a little more like typical C#.您还可以使用 async/await 使代码看起来更像典型的 C#。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM