5
0

More than 3 years have passed since last update.

unityで作成したアプリとfirebaseを接続し、Android実機でテストする方法

Last updated at Posted at 2021-06-22

# unityで作成したアプリとfirebaseを接続する
Firebase公式のドキュメントUnity プロジェクトに Firebase を追加するがあるにあるのですが、いまいちわかりにくいので、健忘録がてらまとめました。

Androidのbuild環境を設定し、プレイヤーセッティングへ

Image from Gyazo

カンパニーネームとプロダクトネームをユニークなものに設定する

Image from Gyazo

Player settingsのOther settingの中を確認。

Package Nameがユニークなものになっているかを確認し、下の図のように設定されているかを確認する。
Image from Gyazo

FireBaseコンソールに進み新しくプロジェクトを作成する
Image from Gyazo

プロジェクトの名前をつける

Image from Gyazo

アナリティクスを有効にする

Image from Gyazo

自身のアナリティクスと紐付ける

Image from Gyazo

しばらくするとプロジェクトが作られる

Image from Gyazo

unityのマークを選択する

Image from Gyazo

今回はAndroidのAppとして実装する

パッケージネームは先ほどUnityで設定したものを選ぶ
Image from Gyazo

google-services.jsonをDLし、Assets>Dataの中に保存する

Image from Gyazo
Image from Gyazo

Firebase SDKをDLしたらコンソールにもどる

Image from Gyazo

プロジェクトの設定に移動する

Image from Gyazo

SHA1を設定するために、ターミナルに下記コマンドを入力する

ターミナル
keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore

すると、パスワードを要求されるので、入力する。
Image from Gyazo

SHA1の部分をコピーし、
Image from Gyazo

プロジェクトの設定のフィンガープリントの部分に貼り付けて保存する。
Image from Gyazo

メニューバーのAssets>Import Package>Custom Packageを選択し、先ほどDLしたSDKの中からAnaliticsを開く

Image from Gyazo

全てImport
Image from Gyazo

自動更新の設定を聞かれるので、Enableを選択する
Image from Gyazo

初期に呼び出されるスクリプトを設定

Assets>Scriptフォルダを作成し、その中に下記のScriptを格納

FirebaseInit.cs
using System.Collections;
using System.Collections.Generic;
using Firebase;
using Firebase.Analytics;
using UnityEngine;


// Google認証
using Google;

public class FirebaseInit : MonoBehaviour
{

    private FirebaseApp app;

    // Start is called before the first frame update
    public virtual void Start()
    {

        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
        {
            var dependencyStatus = task.Result;
            if (dependencyStatus == DependencyStatus.Available)
            {
                // Create and hold a reference to your FirebaseApp,
                // where app is a Firebase.FirebaseApp property of your application class.
                app = FirebaseApp.DefaultInstance;

                FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);

                // Set a flag here to indicate whether Firebase is ready to use by your app.
            }
            else
            {
                //Debug.LogError(System.String.Format(
                //  "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                // Firebase Unity SDK is not safe to use here.
                Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
            }
        });
    }

}

Androidのエミュレートは実機を使用

接続の方法は、下記を参考に。
【Android】Unityで開発したゲームの実機テストを行う方法

Run and Build and Run!!!

Image from Gyazo

無事に動きました!

また、無事にFireBaseも起動しました!
Image from Gyazo

5
0
0

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
5
0