How to call http callable functions in firebase with unity?
Firebase has recently launched the facility to directly call cloud functions from mobile devices which I think is a really great feature. Details
But the thing is, I can not find any documentation on how to call these functions in unity. Does the official plugin supports it? How am I supposed to call the functions?
Answer by figurefurther · Jun 11, 2018 at 06:43 AM
You probably already figured this out, but I figured I would share what I have found with experimentation for anyone else stumbling across this thread. You can use the http callable functions with Unity. It's actually pretty easy. Use the WWW method (documentation here) to access the url for your function (which will be provided when you deploy your cloud function).
For example, I did firebase's Get Started addMessage function as a test (here) which is an https callable function that adds whatever text you append to the url to a "messages" node in the realtime database. My C# code was pretty simple, I made the base url as a string and then a public string where I could add whatever text I wanted in the inspector for testing purposes, concatenated those into another string variable and fed that into the WWW method (I just cut and paste the example code from the above link to the documentation for retrieving textures, but changed it from textures to simply text and fed that into the debug.log just to see what it gave me--it gave me the html from the database, but I didn't need it, I was just curious). Bing, bang, boom, the text showed up in my firebase just like when I did it through a browser. Pretty slick!
So, long story short--use the WWW method to access the url that firebase will give you for the function you want to run and it will do it's thing. If you are looking to get information back from the browser, that will probably be a little trickier since Unity doesn't have a huge amount of options for what you can retrieve from the url in return (mostly assets like images, movies, and textures--but I haven't played with it too much).
You can also run your cloud functions triggered by events that occur on the backend, and that has nothing to do with Unity so I don't think it should be an issue. For example, you can have functions run when you change data in the realtime database (which I have been able to successfully add and retrieve data from my unity project, but I haven't set it up so that functions are triggered when I do that--but again, it is only dependent on data changing in the database, it doesn't care how the data was changed).
The one catch is that you have to write your functions in javascript (or typescript--your choice), so if you have been using C# for unity, then you'll have to spend some time learning a new language (it's not too difficult--I recommend using either freecodecamp or Mozilla Development Network--both have excellent tutorials that will have you up and running in no time). Google firebase also has a Git repo with a couple dozen examples for both https callable functions as well as event triggered functions. All in all, I am pretty psyched that I found firebase, easy to use and loads more things that all tie into the database and cloud functions (auth, analytics, messaging, and much much more).
Hey man just wondering how you actually invoke the function URL? everything else is working fine but once i receive the Unity Web Request Im not sure how to invoke it. Cheers
Your answer
Follow this Question
Related Questions
Is it possible to create plugin with network suff (NetwrokBehaviour, SyncVar etc) ? 0 Answers
PLS recommend plugins for networking/sever which ONLY work for authentication + storing player info. 0 Answers
Send Text Over Network 0 Answers
Photon not working/detected 2 Answers
ClientRpc doesnt call on local client 0 Answers