

Since you don’t want your game to lock up for potentially many seconds for network latency, Firebase uses Tasks to perform much of this I/O work in the background.

This can either be out to disk, or even out to the network.

What happened?įirebase does a lot of work that’s dependent on I/O. I run it and… I just see the log “Signed In!” then nothing. Then I increment this value, and write it back out before logging the new number of successes. When sign-in completes, I figure out how many times this script has run successfully before by reading PlayerPrefs.

Next I use ContinueWith to create a continuation and I start signing in anonymously with SignInAnonymouslyAsync. This shouldn’t be an issue for this post, but you’ll want to do more than assert in your own games. Note that I’m not really handling any failure cases in this example. The first thing I do is ensure that Firebase’s dependencies are available on the player’s device with CheckAndFixDependenciesAsync. PlayerPrefs.SetInt("Successes", ++successes) Var successes = PlayerPrefs.GetInt("Successes", 0) Start is called before the first frame updateįirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(fixTask =>Īuth.SignInAnonymouslyAsync().ContinueWith(authTask => Public class FirebaseContinueWith : MonoBehaviour Let’s get started with a very innocent looking demo script: Ideally even giving you more confidence to thread other parts of your game to provide your players with the smooth and responsive gameplay they expect from a modern video game. My goal with this post is to provide you the tools you need to not only safely use Firebase’s asynchronous function calls, but to do so in a way that best suits your own programming style and preferences. Unity itself isn’t super resilient to threaded logic, with most of the classes and functions in the UnityEngine namespace just flat out throwing exceptions if invoked off of the main Unity thread. The Firebase Unity SDK makes judicious use of asynchronous logic for many of its calls. How to deal with asynchronous logic in Unity Patrick Martin Developer Advocate Firebase and Tasks
