Reputation: 363
So I've been working with FireStore for awhile for applications(Typescript/Javascript).
Working on something in Unity and and I just finished installing all the proper packages and dependencies for FireStore for unity.
I have no clue how to initialize a FireStore object to connect it with my Firebase FireStore.
Firebase with unity seem relatively new as there is little documentation.
FireStore with unity documentation seems almost non-existent.
C# firestoreTest
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase;
using Firebase.Analytics;
using Firebase.Database;
using Firebase.Unity.Editor;
using Firebase.Firestore;
public class firestoreTest : MonoBehaviour
{
void Start()
{
FirebaseFirestore db = FirebaseFirestore.DefaultInstance; // <- Not sure if this is how you properly initialize it.
//
//The follow code is from the official Firebase Firestore site for C#.
//Link
//https://firebase.google.com/docs/firestore/manage-data/add-data#c_5
DocumentReference docRef = db.Collection("TestUnity").Document("XsAaTp9dJ6vz33ngzUah");
Dictionary<string, object> city = new Dictionary<string, object>
{
{ "name", "Los Angeles" },
{ "state", "CA" },
{ "country", "USA" }
};
await docRef.SetAsync(city); // <- this throws an error for async problems.
}
}
What I'm trying to do is save some variables to FireStore from Unity.
Should be able to do handle everything else myself, just don't know the proper syntax to do so.
Issues: Not knowing how to initialize a 'FirebaseFirestore db' to my database properly and having errors with async syntax and I'm not too familiar with C#.
Note* I do know how to save information from Unity to Firebase's 'Realtime-Database' but all my other applications run off Firebase Firestore and I wish for all the applications to have an easy time communicating with each other.
Upvotes: 4
Views: 3319
Reputation: 363
After playing around and reading more I got something to upload to FireStore from unity.
Asking a question usually makes me think harder when solving problems.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase;
using Firebase.Analytics;
using Firebase.Database;
using Firebase.Unity.Editor;
using Firebase.Firestore;
public class firebaser : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start");
call();
}
async void call()
{
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
DocumentReference docRef = db.Collection("TestUnity").Document("XsAaTp9dJ6vz33ngzUah");
Dictionary<string, object> city = new Dictionary<string, object>
{
{ "name", "Los Angeles" },
{ "state", "CA" },
{ "country", "USA" }
};
Debug.Log("I am here");
await docRef.SetAsync(city, SetOptions.MergeAll);
}
}
I'll leave this here because I really couldn't find much documentation on Firestore / Unity.
Hopefully the basics as in saving generic data can help someone else.
Upvotes: 3