facebook twitter hatena line google mixi email

「Unity/Firebase/CloudFunctions」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(初期化)
(初期化)
行11: 行11:
 
  using Firebase.Functions;
 
  using Firebase.Functions;
 
  FirebaseFunctions functions = FirebaseFunctions.GetInstance("asia-northeast1");
 
  FirebaseFunctions functions = FirebaseFunctions.GetInstance("asia-northeast1");
 +
 +
==サンプル==
 +
RankingEntry ranking = new RankingEntry();
 +
AddEntry(ranking);
 +
 +
    class RankingEntry {
 +
        public string Name = "hoge";
 +
        public int Point = 100;
 +
    }
 +
    async Task<object> AddEntryAsync(RankingEntry entry)
 +
    {
 +
        object data = new Dictionary<object, object>
 +
        {
 +
            { "name", entry.Name },
 +
            { "point", entry.Point },
 +
        };
 +
        return await functions.GetHttpsCallable("addEntry").CallAsync(data)
 +
            .ContinueWith(task =>
 +
            {
 +
                return task.Result.Data;
 +
            });
 +
    }
 +
    void AddEntry(RankingEntry entry)
 +
    {
 +
        AddEntryAsync(entry).ContinueWith(task =>
 +
        {
 +
            if (task.IsFaulted)
 +
            {
 +
                // onError
 +
            }
 +
            else
 +
            {
 +
                // onComplete
 +
            }
 +
        }, TaskScheduler.FromCurrentSynchronizationContext());
 +
    }
  
 
==firebaseのサーバー側変更==
 
==firebaseのサーバー側変更==

2019年7月11日 (木) 02:42時点における版

準備

Gcp/Firebase/CloudFunctions [ショートカット]

import

FirebaseFunctions.unitypackage をunityのAssets/ImportPackage/CustomPackageからImportする

初期化

using Firebase.Functions;
FirebaseFunctions functions = FirebaseFunctions.DefaultInstance;

東京リージョンの場合

using Firebase.Functions;
FirebaseFunctions functions = FirebaseFunctions.GetInstance("asia-northeast1");

サンプル

RankingEntry ranking = new RankingEntry();
AddEntry(ranking);
   class RankingEntry {
       public string Name = "hoge";
       public int Point = 100;
   }
   async Task<object> AddEntryAsync(RankingEntry entry)
   {
       object data = new Dictionary<object, object>
       {
           { "name", entry.Name },
           { "point", entry.Point },
       };
       return await functions.GetHttpsCallable("addEntry").CallAsync(data)
           .ContinueWith(task =>
           {
               return task.Result.Data;
           });
   }
   void AddEntry(RankingEntry entry)
   {
       AddEntryAsync(entry).ContinueWith(task =>
       {
           if (task.IsFaulted)
           {
               // onError
           }
           else
           {
               // onComplete
           }
       }, TaskScheduler.FromCurrentSynchronizationContext());
   }

firebaseのサーバー側変更

functions.https.onCall を使ってシリアル&認証トークンで接続するようにする

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.addEntry = functions
  .region('asia-northeast1')
  .https.onCall((data, context) =>
{
  const entry = {
    name : data.name,
    point : data.point
  };
  return admin.firestore().collection('entries')
    .add(entry)
    .then((snapshot) =>
  {
    return 'OK';
  });
});

//  トップcount件のEntryを取得
exports.getTopEntries = functions
  .region('asia-northeast1')
  .https.onCall((data, context) =>
{
  const count = data.count;

  return admin.firestore().collection('entries')
    .orderBy('point', 'desc')
    .limit(count)
    .get()
    .then((qSnapshot) =>
  {
    return {
      entries : qSnapshot.docs.map(x => x.data())
    };
  });
});

参考

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