Oculus (Meta Quest)
Provide an Oculus sign-in option to enable players to authenticate using their Meta Quest accounts on virtual reality platforms.
読み終わるまでの所要時間 1 分最終更新 1ヶ月前
最小 SDK バージョン: 2.3.1
このセクションでは、Oculus アカウントを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。- Oculus サインインの設定
- 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
- 匿名ログインからカスタム ID プロバイダーを介したプラットフォームログインへとプレイヤーを更新する。
Oculus サインインの設定
Oculus を Unity Authentication 用の ID プロバイダーとして設定するには、以下の手順に従います。- Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
- ID Providers (ID プロバイダー) を Oculus に設定し、Add (追加) をクリックします。
- App ID テキストフィールドにアプリケーション ID を入力します。
- App Secret テキストフィールドにアプリケーションシークレットを入力します。
- ヘッドセットを装着し、Settings (設定) > System (システム) > Developer (開発者) に移動します。
- USB Connection Dialog (USB 接続ダイアログ) オプションを有効にします。
- USB-C ケーブルを使ってヘッドセットをコンピューターに接続します。
- データアクセスの許可を求めるプロンプトが表示されるので、Allow (許可) をクリックします。
- デバイス接続が成功していることを確認するために、Unity プロジェクトを開き、File (ファイル) > Build Settings (ビルド設定) に移動します。Platform (プラットフォーム) リストから Android を選択し、Switch Platform (ターゲットの切り替え) をクリックします。
- Run Device (実行デバイス) リストから Oculus ヘッドセットを選択します。ヘッドセットがリストにない場合は、Refresh をクリックしてオプションを更新します。
- こちら にある Oculus Integration パッケージを設定し、インストールします。または、Oculus Developer Center からパッケージを見つけることもできます。
- プロジェクトにインポートしたら、エディターのメニューバーから、Oculus > Platform (プラットフォーム) > Edit Settings (設定を編集) に移動します。Oculus アプリケーション ID を入力する必要があります。
ノート: Oculus ヘッドセットは開発者モードに設定する必要があります。これは、開発に使用する開発者アカウントにサインインすることで設定できます (Oculus 開発者アカウントは こちら で作成できます)。using UnityEngine;using Oculus.Platform;using Oculus.Platform.Models;public class OculusAuth : MonoBehaviour{ private string userId; private void Start() { Core.AsyncInitialize().OnComplete(OnInitializationCallback); } private void OnInitializationCallback(Message<PlatformInitialize> msg) { if (msg.IsError) { Debug.LogErrorFormat("Oculus: Error during initialization. Error Message: {0}", msg.GetError().Message); } else { Entitlements.IsUserEntitledToApplication().OnComplete(OnIsEntitledCallback); } } private void OnIsEntitledCallback(Message msg) { if (msg.IsError) { Debug.LogErrorFormat("Oculus: Error verifying the user is entitled to the application. Error Message: {0}", msg.GetError().Message); } else { GetLoggedInUser(); } } private void GetLoggedInUser() { Users.GetLoggedInUser().OnComplete(OnLoggedInUserCallback); } private void OnLoggedInUserCallback(Message<User> msg) { if (msg.IsError) { Debug.LogErrorFormat("Oculus: Error getting logged in user. Error Message: {0}", msg.GetError().Message); } else { userId = msg.Data.ID.ToString(); // do not use msg.Data.OculusID; GetUserProof(); } } private void GetUserProof() { Users.GetUserProof().OnComplete(OnUserProofCallback); } private void OnUserProofCallback(Message<UserProof> msg) { if (msg.IsError) { Debug.LogErrorFormat("Oculus: Error getting user proof. Error Message: {0}", msg.GetError().Message); } else { string oculusNonce = msg.Data.Value; // Authentication can be performed here } }}
戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成
SignInWithOculusAsync- Oculus の認証情報を使用して新しい Unity Authentication プレイヤーを作成する。
- Oculus の認証情報を使用して既存のプレイヤーをサインインする。
SignInWithOculusAsyncSignInWithOculusAsyncSignInWithOculusAsyncasync Task SignInWithOculusAsync(string nonce, string userId){ try { await AuthenticationService.Instance.SignInWithOculusAsync(nonce, userId); 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); }}
プレイヤーを匿名から Oculus アカウントへと更新する
匿名認証を設定した後、プレイヤーが匿名からアップグレードし、Oculus アカウントを作成してサインインすることを希望する場合は、ゲームのプレイヤーにプロンプトを表示し、Oculus サインインをトリガーして Oculus からセッションチケットを取得するよう求める必要があります。その後、LinkWithOculusAsync- を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
SignInAnonymouslyAsync - を使用して、キャッシュされたプレイヤーのアカウントを Oculus アカウントにリンクします。
LinkWithOculusAsync
async Task LinkWithOculusAsync(string nonce, string userId){ try { await AuthenticationService.Instance.LinkWithOculusAsync(nonce, userId); 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 (Exception ex) { Debug.LogError(“Link failed.”); Debug.LogException(ex); }}
Oculus アカウントをリンク解除する
UnlinkOculusAsyncasync Task UnlinkOculusAsync(string idToken){ try { await AuthenticationService.Instance.UnlinkOculusAsync(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); }}