「Unity/GooglePlayGames/v1からv2へ移行」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→version2では起動時に自動ログインされる) |
|||
| 行82: | 行82: | ||
デベロッパー向け: | デベロッパー向け: | ||
コード実装の簡素化: クライアントサイド コードでログイン / ログアウトのフローを処理する必要がなくなりました。ゲームの起動時にログインが自動的にトリガーされ、アカウント管理は OS 設定内で効率化されます。 | コード実装の簡素化: クライアントサイド コードでログイン / ログアウトのフローを処理する必要がなくなりました。ゲームの起動時にログインが自動的にトリガーされ、アカウント管理は OS 設定内で効率化されます。 | ||
| + | </pre> | ||
| + | |||
| + | ==FirebaseのAuthStateChangedのタイミング変更== | ||
| + | AuthStateChangedのイベントタイミングが、v1とv2で、ズレてるので気をつける。 | ||
| + | |||
| + | <pre> | ||
| + | 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 | ||
| + | && auth.CurrentUser.IsValid(); | ||
| + | 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; | ||
| + | } | ||
</pre> | </pre> | ||
2025年7月15日 (火) 21:12時点における版
目次
v1からv2へ移行
Play Games サービス v2 に移行する(Unity) https://developer.android.com/games/pgs/unity/migrate-to-v2?hl=ja
SignOutエラー対応方法
エラー詳細
error CS1061: 'PlayGamesPlatform' does not contain a definition for 'SignOut' and no accessible extension method 'SignOut' accepting a first argument of type 'PlayGamesPlatform' could be found (are you missing a using directive or an assembly reference?)
対応方法
- ((PlayGamesPlatform)Social.Active).SignOut(); + Firebase.Auth.FirebaseAuth.DefaultInstance.SignOut();
GetServerAuthCodeエラー対応方法
エラー詳細
error CS1061: 'PlayGamesPlatform' does not contain a definition for 'GetServerAuthCode' and no accessible extension method 'GetServerAuthCode' accepting a first argument of type 'PlayGamesPlatform' could be found (are you missing a using directive or an assembly reference?)
対応前
string authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
対応後
PlayGamesPlatform.Instance.RequestServerSideAccess(
false,
authCode =>
{
try
{
Debug.Log("GooglePlaySignIn: RequestServerSideAccess Success: " + authCode);
}
catch (Exception ex)
{
Debug.LogError("Error getting credential: " + ex.Message);
}
}
);
PlayGamesClientConfigurationエラー対応方法
エラー詳細
error CS0246: The type or namespace name 'PlayGamesClientConfiguration' could not be found (are you missing a using directive or an assembly reference?) error CS0117: 'PlayGamesPlatform' does not contain a definition for 'InitializeInstance'
https://developer.android.com/games/pgs/unity/migrate-to-v2?hl=ja の対応の通り
対応後
Action<SignInStatus> processAuthentication = (SignInStatus status) =>
{
if (status == SignInStatus.Success)
{
// 成功
}
else
{
// 失敗
}
};
// GooglePlay認証開始
PlayGamesPlatform.Instance.Authenticate(processAuthentication);
version2では起動時に自動ログインされる
https://developer.android.com/games/pgs/unity/migrate-to-v2?hl=ja
ゲームが起動すると、自動ログインの試行が表示されます。
https://developer.android.com/games/pgs/unity/migrate-to-v2?hl=ja
Play Games サービス v2 SDK は、ユーザーのログインに関して v1 よりもいくつかの機能が強化されています。
ユーザー向け:
ユーザー エクスペリエンスの向上: デフォルトのアカウントを選択すると、追加のプロンプトが表示されることなく自動的にログインされます。
デベロッパー向け:
コード実装の簡素化: クライアントサイド コードでログイン / ログアウトのフローを処理する必要がなくなりました。ゲームの起動時にログインが自動的にトリガーされ、アカウント管理は OS 設定内で効率化されます。
FirebaseのAuthStateChangedのタイミング変更
AuthStateChangedのイベントタイミングが、v1とv2で、ズレてるので気をつける。
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
&& auth.CurrentUser.IsValid();
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;
}
