Apple
Provide an Apple sign-in option to enable players to authenticate using their Apple accounts on iOS and macOS.
読み終わるまでの所要時間 3 分最終更新 1ヶ月前
最小 SDK バージョン: 2.0.0
このセクションでは、Apple アカウントを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。- Apple サインインを設定する。
- 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
- 匿名ログインから Apple アカウントを介したプラットフォームログインへとプレイヤーを更新する。
ゲーム内のプレイヤーに Apple サインインのオプションを提供するには、アプリケーションを設定して Apple でのサインインを有効にしてください。
Apple サインインを設定する
- アプリケーションを設定して Apple でのサインインを有効にします。ノート:Unity Authentication では Bundle ID (バンドル ID) ベースのサインインのみをサポートしています。Service ID (サービス ID) はサポートされていません。
- Unity Authentication について、ID プロバイダーを Apple に設定します。
- Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
- ID Providers (ID プロバイダー) を Sign-in with Apple (Apple でサインイン) に設定し、Add (追加) を選択します。
- Apple 開発者コンソールからの バンドル ID を App ID (アプリ ID) テキストフィールドに入力し、Save (保存) を選択します。バンドル ID は次のようになります: com.something.somethingelse。
- Unity プロジェクト内で Apple サインインを統合するには、Unity Asset Store からの SDK ライブラリパッケージを利用することをお勧めします。例としては、Sign in with the Apple Unity Plugin があります。
- インストールするには、パッケージのドキュメントに記載された 手順 に従ってください。
- ノート: 参照先のパッケージは、Unity によって開発、所有、運営されているものではありません。非 Unity パッケージの扱いについては、Unity の ベストプラクティス を参照してください。
- 以下のコードスニペットは、上記の参考パッケージを Unity プロジェクト内で利用して、Apple サインインを有効にする方法を示したものです。
using System.Text;using UnityEngine;// External dependenciesusing AppleAuth;using AppleAuth.Enums;using AppleAuth.Interfaces;using AppleAuth.Native;public class AppleExampleScript : MonoBehaviour{ IAppleAuthManager m_AppleAuthManager; public string Token { get; private set; } public string Error { get; private set; } public void Initialize() { var deserializer = new PayloadDeserializer(); m_AppleAuthManager = new AppleAuthManager(deserializer); } public void Update() { if (m_AppleAuthManager != null) { m_AppleAuthManager.Update(); } } public void LoginToApple() { // Initialize the Apple Auth Manager if (m_AppleAuthManager == null) { Initialize(); } // Set the login arguments var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName); // Perform the login m_AppleAuthManager.LoginWithAppleId( loginArgs, credential => { var appleIDCredential = credential as IAppleIDCredential; if (appleIDCredential != null) { var idToken = Encoding.UTF8.GetString( appleIDCredential.IdentityToken, 0, appleIDCredential.IdentityToken.Length); Debug.Log("Sign-in with Apple successfully done. IDToken: " + idToken); Token = idToken; } else { Debug.Log("Sign-in with Apple error. Message: appleIDCredential is null"); Error = "Retrieving Apple Id Token failed."; } }, error => { Debug.Log("Sign-in with Apple error. Message: " + error); Error = "Retrieving Apple Id Token failed."; } ); }}
戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成
SignInWithAppleAsync- Apple の認証情報を使用して新しい Unity Authentication プレイヤーを作成する。
- Apple の認証情報を使用して既存のプレイヤーをサインインする。
SignInWithAppleAsyncSignInWithAppleAsyncSignInWithAppleAsyncasync Task SignInWithAppleAsync(string idToken){ try { await AuthenticationService.Instance.SignInWithAppleAsync(idToken); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
プレイヤーを匿名から Apple アカウントへと更新する
匿名認証を設定した後、匿名のプレイヤーがアップグレードして Apple アカウントを作成することを希望する場合は、ゲームのプレイヤーにプロンプトを表示し、Apple サインインをトリガーして Apple から ID トークンを取得するように求める必要があります。その後、LinkWithAppleAsync- を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
SignInAnonymouslyAsync - を使用して、キャッシュされたプレイヤーのアカウントを Apple アカウントにリンクします。
LinkWithAppleAsync
プレイヤーが、サインインまたは新しいプレイヤープロファイルの作成によって Apple サインインをトリガーする場合、Apple ID トークンを受け取ったら、以下の API を呼び出してプレイヤーの認証を行います。async Task LinkWithAppleAsync(string idToken){ try { await AuthenticationService.Instance.LinkWithAppleAsync(idToken); Debug.Log("Link is successful."); } catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked) { // Prompt the player with an error message. Debug.LogError("This user is already linked with another account. Log in instead."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
async Task SignInWithAppleAsync(string idToken){ try { await AuthenticationService.Instance.SignInWithAppleAsync(idToken); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
Apple サインイン認証を実装する
プレイヤーが、サインインまたは新しいプレイヤープロファイルの作成によって Apple サインインをトリガーする場合、Apple ID トークンを受け取ったら、以下の API を呼び出してプレイヤーの認証を行います:SignInWithAppleAsync(string idToken).Apple アカウントのリンク解除
UnlinkAppleAsyncasync Task UnlinkAppleAsync(string idToken){ try { await AuthenticationService.Instance.UnlinkAppleAsync(idToken); Debug.Log("Unlink is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}