Firebase Authentication を使用すると、ユーザーが 1 つ以上のサインイン方法 (メール アドレスとパスワードによるサインイン、Google サインインや Facebook ログインなどのフェデレーション ID プロバイダーなど) を使用してゲームにサインインできるようになります。このチュートリアルでは、ゲームにメール アドレスとパスワードによるサインインを追加する方法を示して、Firebase Authentication の使用を開始します。
あなたが始める前に
Firebase Authenticationを使用する前に、次のことを行う必要があります。
Unity プロジェクトを登録し、Firebase を使用するように構成します。
Unity プロジェクトですでに Firebase を使用している場合は、Firebase 用に既に登録および構成されています。
Unity プロジェクトがない場合は、サンプル アプリをダウンロードできます。
Unity プロジェクトにFirebase Unity SDK (具体的には
FirebaseAuth.unitypackage
) を追加します。
Unity プロジェクトに Firebase を追加するには、 Firebase コンソールと開いている Unity プロジェクトの両方でタスクが必要になることに注意してください (たとえば、コンソールから Firebase 構成ファイルをダウンロードして、それらを Unity プロジェクトに移動します)。
新規ユーザー登録
新しいユーザーがメール アドレスとパスワードを使用してゲームに登録できるフォームを作成します。ユーザーがフォームに入力したら、ユーザーから提供された電子メール アドレスとパスワードを検証し、それらをCreateUserWithEmailAndPasswordAsync
メソッドに渡します。
auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
if (task.IsCanceled) {
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted) {
Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
return;
}
// Firebase user has been created.
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("Firebase user created successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
既存のユーザーにサインインする
既存のユーザーが電子メール アドレスとパスワードを使用してサインインできるフォームを作成します。ユーザーがフォームに入力したら、 SignInWithEmailAndPasswordAsync
メソッドを呼び出します。
auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
if (task.IsCanceled) {
Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted) {
Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
認証状態変更イベント ハンドラーを設定し、ユーザー データを取得する
サインインおよびサインアウト イベントに応答するには、イベント ハンドラーをグローバル認証オブジェクトにアタッチします。このハンドラーは、ユーザーのサインイン状態が変化するたびに呼び出されます。ハンドラーは、認証オブジェクトが完全に初期化され、ネットワーク呼び出しが完了した後にのみ実行されるため、サインインしているユーザーに関する情報を取得するのに最適な場所です。
FirebaseAuth
オブジェクトのStateChanged
フィールドを使用して、イベント ハンドラーを登録します。ユーザーが正常にサインインすると、イベント ハンドラーでユーザーに関する情報を取得できます。
最後に、このオブジェクトでDestroy
が呼び出されると、自動的にOnDestroy
が呼び出されます。 OnDestroy
で Auth オブジェクトの参照をクリーンアップします。
void InitializeFirebase() {
auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);
}
void AuthStateChanged(object sender, System.EventArgs eventArgs) {
if (auth.CurrentUser != user) {
bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
if (!signedIn && user != null) {
DebugLog("Signed out " + user.UserId);
}
user = auth.CurrentUser;
if (signedIn) {
DebugLog("Signed in " + user.UserId);
displayName = user.DisplayName ?? "";
emailAddress = user.Email ?? "";
photoUrl = user.PhotoUrl ?? "";
}
}
}
void OnDestroy() {
auth.StateChanged -= AuthStateChanged;
auth = null;
}
次のステップ
他の ID プロバイダーと匿名ゲスト アカウントのサポートを追加する方法を学習します。