LoginSignup
12
9

More than 5 years have passed since last update.

[Unity]Firebase Realtimeで読み書きができるまで.

Posted at

まずはFirebaseSDKをUnityProjectにつっこむ.そこからのやり方

始める前

スクリーンショット 2016-11-30 12.29.50.png

スクリーンショット 2016-11-30 12.29.54.png

PlayerSetting
スクリーンショット 2016-11-30 14.05.32.png

Firebase プロジェクトを作成

Bandle ID設定
スクリーンショット 2016-12-07 0.52.35.jpg

今回はIOSを選択
スクリーンショット 2016-12-07 0.53.12.jpg

先ほど設定したBandle IDをペースト
スクリーンショット 2016-12-07 0.53.26.jpg

ダウンロードしたGoogleService-Info.plistをUnityのプロジェクトにつっこむ.
スクリーンショット 2016-12-07 0.55.54.jpg

ビルド

ビルドする

XCodeでRun

失敗
スクリーンショット 2016-12-09 16.36.57.png

Unityに戻ったらこのようなエラー
スクリーンショット 2016-12-09 16.17.26.png

cocoapodsをダウンロード
途中で止まったので,以下のサイトを参考に直す
http://westhillworker.com/cocoapods-progrem/

Build & Runで成功

Setting up public Access

下のサイトを参考に設定をpublicに変更

実践

スクリプトを書く

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase;
using Firebase.Unity.Editor;
using Firebase.Database;

public class FirebaseManager : MonoBehaviour {

    public class QuestData
    {
        public int questId;
        public bool isCompleted;

        public QuestData(){
        }

        public QuestData(int questId, bool isCompleted){
            this.questId = questId;
            this.isCompleted = isCompleted;
        }
    }


    private DatabaseReference _databaseReference;

    // Use this for initialization
    void Start () {
        // Set this before calling into the realtime database.
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://アプリ名-数字.firebaseio.com/");

        // Get the root reference location of the database.
        _databaseReference = FirebaseDatabase.DefaultInstance.RootReference;

        // やりたい方のコメントアウトを外す
        // 書き込み
        //WriteNewQuestData (1001, 1, false);

        // 読み込み
        ReadQuestData(1001);

    }

    //書き込み
    void WriteNewQuestData(int userId, int questId, bool isCompleted){
        var questData = new QuestData (questId, isCompleted);
        string json = JsonUtility.ToJson (questData);

        // データベースにJson形式で書き込み
        _databaseReference
            .Child ("users")
            .Child (userId.ToString ())
            .SetRawJsonValueAsync (json);


        // 値を直接書き込む場合はこっち
//      _databaseReference
//          .Child ("users")
//          .Child (userId.ToString ())
//          .Child (questId.ToString ())
//          .SetValueAsync (isCompleted);
    }

    // 読み込み
    void ReadQuestData(int userId){
        FirebaseDatabase.DefaultInstance
            .GetReference("users")
            .GetValueAsync().ContinueWith(task=>{
                if(task.IsFaulted){
                    Debug.LogError("失敗");
                }
                else if(task.IsCompleted){
                    DataSnapshot snapshot = task.Result;
                    string json = snapshot.Child(userId.ToString())
                        .GetRawJsonValue();
                    Debug.Log("Read: "+ json);
                }
            });
    }

}


読み書きできた.

書き込み
スクリーンショット 2016-12-10 22.05.31.png

読み込み
スクリーンショット 2016-12-10 23.21.12.png

12
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
9