- Home /
Can I make rest api cloud functions+firestore to Unity?
Hello,
I want to make rest api for my Firestore database for my Unity game. I don't want to connect directly to database, because the game complex logic, I want to application logic is controlled on the server to avoid tampering on the client side. So I want to every write and every read request through cloud functions.
Write is very simple.
Nodejs cloud functions:
exports.helloWorld = functions.https.onCall((data, context) => {
let data = {
name: 'Los Angeless',
state: 'CA',
country: 'USA'
};
// Add a new document in collection "cities" with ID 'LA'
let setDoc = db.collection('xy').doc(data.text).set(data);
return { text : "ok" }
});
Unity:
public void writeexample()
{
var data = new Dictionary<string, object>();
data["text"] = “example”;
data[“xy”] = “example”;
FirebaseFunctions.DefaultInstance.GetHttpsCallable(“helloWorld”)
.CallAsync(data).ContinueWith((task) =>
{
if (task.IsFaulted)
{
// Handle the error...
}
else if (task.IsCompleted)
{
// DataSnapshot snapshot = task.Result;
IDictionary snapshot = (IDictionary)task.Result.Data;
print(“Result:” + snapshot["text"]);
}
});
}
Okey, it's work. How to make query? Not realtime query, only simple once query! I see docs cloud functions Call functions from your app but here only write, I see Firestore docs Get data once, but it's don't working in cloud functions. (Or I don't know how to..)
So I want to every query and write to database only through Serverless rest api (cloud functions). It is OK? Or I am crazy? Or how to make it? How to make query for Firestore database through cloud functions to Unity? Thanks!