「Unity/GooglePlayGames/Firebase連携」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→PlayGameとEmailが連携した場合) |
(→PlayGame連携) |
||
| (同じ利用者による、間の3版が非表示) | |||
| 行71: | 行71: | ||
==PlayGame連携== | ==PlayGame連携== | ||
| + | 他のアカウントにPlayGameアカウントを関連付ける。 | ||
| + | <pre> | ||
| + | Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; | ||
| + | Firebase.Auth.Credential credential = Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode); | ||
| + | GooglePlaySuccessLink(auth, credential); | ||
| + | void GooglePlaySuccessLink(Firebase.Auth.FirebaseAuth auth, Firebase.Auth.Credential credential) | ||
| + | { | ||
| + | Debug.Log("GooglePlayGameScene GooglePlaySuccessLink"); | ||
| + | Firebase.Auth.FirebaseUser user = auth.CurrentUser; | ||
| + | if (user != null && user.IsValid()) | ||
| + | { | ||
| + | user.LinkWithCredentialAsync(credential).ContinueWith(task => | ||
| + | { | ||
| + | if (task.IsCanceled) { | ||
| + | Debug.LogError("LinkWithCredentialAsync was canceled."); | ||
| + | return; | ||
| + | } | ||
| + | if (task.IsFaulted) { | ||
| + | Debug.LogError("LinkWithCredentialAsync encountered an error: " + task.Exception); | ||
| + | return; | ||
| + | } | ||
| + | Firebase.Auth.FirebaseUser newUser = task.Result.User; | ||
| + | Debug.LogFormat("Credentials successfully linked to Firebase user: {0} ({1})", | ||
| + | newUser.DisplayName, newUser.UserId); | ||
| + | }); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Debug.LogError("FirebaseUser is null or not valid."); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
===LinkWithCredentialAsyncエラー=== | ===LinkWithCredentialAsyncエラー=== | ||
連携エラー | 連携エラー | ||
| 行76: | 行108: | ||
LinkWithCredentialAsync encountered an error: System.AggregateException: One or more errors occurred. (This credential is already associated with a different user account.) ---> Firebase.Auth.FirebaseAccountLinkException: This credential is already associated with a different user account. | LinkWithCredentialAsync encountered an error: System.AggregateException: One or more errors occurred. (This credential is already associated with a different user account.) ---> Firebase.Auth.FirebaseAccountLinkException: This credential is already associated with a different user account. | ||
</pre> | </pre> | ||
| + | 既に、Firebase側に、アカウントが存在するので、Firebase側のPlayGameユーザを消せば良い。 | ||
2025年8月4日 (月) 18:29時点における最新版
目次
PlayGameのFirebaseユーザ認証
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
Firebase.Auth.Credential credential = Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode);
auth.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWith(task => {
if (task.IsCanceled)
{
Debug.LogError("SignInAndRetrieveDataWithCredentialAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInAndRetrieveDataWithCredentialAsync encountered an error: " + task.Exception);
return;
}
Firebase.Auth.AuthResult result = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
result.User.DisplayName, result.User.UserId);
});
出力
User signed in successfully: hoge1 (pGb9NkG1Q3ZqURJQGLxvXaYXxxxx)
- firebaseのauthにユーザレコードが追加される。idはPlayGame名が追加され、ユーザーuidは、上のカッコ内の28文字ハッシュが追加される。
その他気づき
- PlayGame名に日本語名を入れてもそれが、idに入る。
- firebase登録後にPlayGameの名前を変えたときは、そのゲームにログインしてもFirebase側のidは変わらない。連携解除もされない。
PlayGameのFirebaseユーザ取得
Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null && user.IsValid())
{
string playerName = user.DisplayName;
// The user's Id, unique to the Firebase project.
// Do NOT use this value to authenticate with your backend server, if you
// have one; use User.TokenAsync() instead.
string uid = user.UserId;
Debug.Log("playerName=" + playerName);
Debug.Log("uid=" + uid);
}
出力
playerName= uid=AgGbeT2LIeXWUIRk0uYbeYd4uExx
PlayGameのログアウト
((PlayGamesPlatform)Social.Active).SignOut(); Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; auth.SignOut();
ログ出力
Debug FirebaseAuth Notifying id token listeners about a sign-out event. Debug FirebaseAuth Notifying auth state listeners about a sign-out event. Info TRuntime.CctTransportBackend Making request to: ttps://firebaselogging-pa.googleapis.com/v1/firelog/legacy/batchlog Info TRuntime.CctTransportBackend Status Code: 200
PlayGameとEmailが連携した場合
firebaseのauthのユーザレコードに1行レコードがあり、PlayGameアイコンとEmailアイコンが並ぶ。
PlayGame連携
他のアカウントにPlayGameアカウントを関連付ける。
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
Firebase.Auth.Credential credential = Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode);
GooglePlaySuccessLink(auth, credential);
void GooglePlaySuccessLink(Firebase.Auth.FirebaseAuth auth, Firebase.Auth.Credential credential)
{
Debug.Log("GooglePlayGameScene GooglePlaySuccessLink");
Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null && user.IsValid())
{
user.LinkWithCredentialAsync(credential).ContinueWith(task =>
{
if (task.IsCanceled) {
Debug.LogError("LinkWithCredentialAsync was canceled.");
return;
}
if (task.IsFaulted) {
Debug.LogError("LinkWithCredentialAsync encountered an error: " + task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result.User;
Debug.LogFormat("Credentials successfully linked to Firebase user: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
}
else
{
Debug.LogError("FirebaseUser is null or not valid.");
}
}
LinkWithCredentialAsyncエラー
連携エラー
LinkWithCredentialAsync encountered an error: System.AggregateException: One or more errors occurred. (This credential is already associated with a different user account.) ---> Firebase.Auth.FirebaseAccountLinkException: This credential is already associated with a different user account.
既に、Firebase側に、アカウントが存在するので、Firebase側のPlayGameユーザを消せば良い。
