Cloud Firestore Unity SDK Converters

Viewed 1030

There aren't yet any samples for the newly released Cloud Firestore Unity bindings, as far as I could see, so I added this to an existing app as the world's most naive Firestore test, this being a Unity-ized version of the first example here:

https://firebase.google.com/docs/firestore/quickstart

I thought this would work, but get an error "Unable to create converter for type UserData".

A quick glance through the reference doesn't seem to show a way to register converters (?? between object and json?) Can anyone explain what I misunderstood?

[System.Serializable]
public class UserData
{
    public int x;
    public int y;
    public string name;
}

// Later on after FirebaseInit, etc...

        UserData userData = new UserData
        {
            x = 5,
            y = 7,
            name = "Joe Bloggs"
        };

        var write = await 

        FirebaseFirestore.DefaultInstance.Collection("Users").AddAsync(userData);

2 Answers

Generally, the best thing you can do right now is manually put this data into something that matches the IDictionary<string, object> interface (ex: just a plain old C# dictionary) and pass that in. This is actually also true for Realtime Database (even if you pass in RawJson, it gets converted to an IDictionary internally).

To keep with Stack Overflow's guidelines, your implementation may look something like this:

var data = new Dictionary<string, object>();
data.Add("x", 5);
data.Add("y", 7);
data.Add("name", "Joe Bloggs");
await FirebaseFirestore.DefaultInstance.Collection("Users").AddAsync(userData);
// do cool stuff here after the upload

An important note right now: Firestore for games is an alpha release. It's incomplete and the API is subject to change. This also means that you can help drive its evolution into beta. If you were to rewrite this Stack Overflow question as a feature request here, you'll get it on the team's radar.

Also, keep an eye on this repository. A sample app will likely appear soon.

Finally, it's awesome that you're excited and using Firestore in its alpha state. In addition to that github issue page I linked above, you can file bugs and feature requests here. There's also a fairly active google group here and a slack channel here, both monitored by Firebase engineers.

   [FirestoreData]
   public class UserData
   {
     [FirestoreProperty]
     public string UserId { get; set; }
    
     [FirestoreProperty]
     public string Nickname { get; set; }
   }
  1. Add [FirestoreData] attribute to your class.
  2. Add [FirestoreProperty] attribute to all of your fields.
  3. Transform your fields to { get; set; } properties.
Related