Unity/Firebase/CloudFunctions
提供: 初心者エンジニアの簡易メモ
準備
Gcp/Firebase/CloudFunctions [ショートカット]
準備(dbを使う場合)
Gcp/Firebase/Firestore [ショートカット]
import
FirebaseFunctions.unitypackage をunityのAssets/ImportPackage/CustomPackageからImportする
初期化
using Firebase.Functions; FirebaseFunctions functions = FirebaseFunctions.DefaultInstance;
東京リージョンの場合こちらを記述
using Firebase.Functions; FirebaseFunctions functions = FirebaseFunctions.GetInstance("asia-northeast1");
サンプル
ScoreInit score = new ScoreInit(); AddScore(score); GetTopScores();
using System.Threading.Tasks; class ScoreInit { public string UserName = "taro"; public int Point = 100; public string UserId = "12341234"; } async Task<object> AddScoreAsync(ScoreInit score) { object data = new Dictionary<object, object> { { "user_name", score.UserName }, { "point", score.Point }, { "user_id", score.UserId }, }; return await functions.GetHttpsCallable("addScore").CallAsync(data) .ContinueWith(task => { return task.Result.Data; }); } void AddScore(ScoreInit score) { AddScoreAsync(score).ContinueWith(task => { if (task.IsFaulted) { // onError } else { // onComplete Debug.Log("task.Result=" + task.Result); // OK } }, TaskScheduler.FromCurrentSynchronizationContext()); } async Task<object> ReplaceScoreAsync(ScoreInit score) { object data = new Dictionary<object, object> { { "user_name", score.UserName }, { "point", score.Point }, { "user_id", score.UserId }, }; return await functions.GetHttpsCallable("replaceScore").CallAsync(data) .ContinueWith(task => { return task.Result.Data; }); } void ReplaceScore(ScoreInit score) { ReplaceScoreAsync(score).ContinueWith(task => { if (task.IsFaulted) { // onError } else { // onComplete Debug.Log("task.Result=" + task.Result); // OK } }, TaskScheduler.FromCurrentSynchronizationContext()); } [Serializable] class ResData { public string status = "ok"; public string notice = ""; public List<ResUser> users; } [Serializable] class ResUser { public int point = 0; public string user_name = ""; public string user_id = ""; } async Task<object> GetTopScoresAsync() { object data = new Dictionary<object, object> { { "count", 3 }, }; return await functions.GetHttpsCallable("getTopScores").CallAsync(data) .ContinueWith(task => { return task.Result.Data; }); } void GetTopScores() { GetTopScoresAsync().ContinueWith(task => { if (task.IsFaulted) { // onError } else { // onComplete string json = task.Result.ToString(); Debug.Log("json=" + json); // {"status":"ok","notice":"","users":[{"point":140,"name":"siro"},{"point":130,"name":"saburo"},{"point":120,"name":"jiro"}]} ResData resData = JsonUtility.FromJson<ResData>(json); Debug.Log("status=" + resData.status); Debug.Log("notice=" + resData.notice); foreach (ResUser user in resData.users) { Debug.Log("user.name=" + user.user_name); Debug.Log("user.id=" + user.user_id); Debug.Log("user.point=" + user.point); } } }, TaskScheduler.FromCurrentSynchronizationContext()); }
firebaseのサーバー側変更
functions.https.onCall を使ってシリアル&認証トークンで接続するようにする
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); // スコア追加 exports.addScore = functions .region('asia-northeast1') .https.onCall((data, context) => { const score = { user_name : data.user_name, point : data.point, user_id : data.user_id }; return admin.firestore().collection('scores') .add(score) .then((snapshot) => { return 'OK'; }); }); // スコア更新 exports.replaceScore = functions .region('asia-northeast1') .https.onCall((data, context) => { const score = { point : data.point }; if (data.user_name != null) { score.user_name = data.user_name; } if (data.user_id != null) { score.user_id = data.user_id; } return admin.firestore().collection('scores') .doc(data.user_id).set(score) .then((snapshot) => { return 'OK'; }); }); // トップcount件のScoreを取得 exports.getTopScores = functions .region('asia-northeast1') .https.onCall((data, context) => { const count = data.count; return admin.firestore().collection('scores') .orderBy('point', 'desc') .limit(count) .get() .then((snapshot) => { var obj = { status: 'ok', notice: 'Processing succeeded.', users: snapshot.docs.map(x => x.data()), } var json=JSON.stringify(obj); return json; }); });
参考
https://firebase.google.com/docs/functions/callable?hl=ja
https://devlog.hassaku.blue/2019/03/unity-firebase-firebase.html
https://firebase.google.com/docs/reference/unity/class/firebase/functions/firebase-functions