Oculus (Meta Quest)

最小 SDK バージョン: 2.3.1

このセクションでは、Oculus アカウントを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。

  • Oculus サインインの設定
  • 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
  • 匿名ログインからカスタム ID プロバイダーを介したプラットフォームログインへとプレイヤーを更新する。

ゲーム内のプレイヤーに Oculus サインインのオプションを提供するには、Oculus Developer Dashboard でアプリケーションを作成し、App ID とアプリケーションシークレット、およびナンス (nonce) とユーザー ID をメモしてください。

パッケージの追加方法については、以下のセクションを参照してください。

Oculus サインインの設定

Oculus を Unity Authentication 用の ID プロバイダーとして設定するには、以下の手順に従います。

  1. Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
  2. ID Providers (ID プロバイダー) を Oculus に設定し、Add (追加) をクリックします。
  3. App ID テキストフィールドにアプリケーション ID を入力します。
  4. App Secret テキストフィールドにアプリケーションシークレットを入力します。

以下のコードは、サインイン済みの Oculus プレイヤーを取得し、nonce を生成する方法の例です。

ノート: 参照先のパッケージは、Unity によって開発、所有、運営されているものではありません。非 Unity パッケージの扱いについては、Unity の ベストプラクティス を参照してください。

  1. ヘッドセットを装着し、Settings (設定) > System (システム) > Developer (開発者) に移動します。
  2. USB Connection Dialog (USB 接続ダイアログ) オプションを有効にします。
  3. USB-C ケーブルを使ってヘッドセットをコンピューターに接続します。
  4. データアクセスの許可を求めるプロンプトが表示されるので、Allow (許可) をクリックします。
  5. デバイス接続が成功していることを確認するために、Unity プロジェクトを開き、File (ファイル) > Build Settings (ビルド設定) に移動します。Platform (プラットフォーム) リストから Android を選択し、Switch Platform (ターゲットの切り替え) をクリックします。
  6. Run Device (実行デバイス) リストから Oculus ヘッドセットを選択します。ヘッドセットがリストにない場合は、Refresh をクリックしてオプションを更新します。
  7. こちら にある Oculus Integration パッケージを設定し、インストールします。または、Oculus Developer Center からパッケージを見つけることもできます。
  8. プロジェクトにインポートしたら、エディターのメニューバーから、Oculus > Platform (プラットフォーム) > Edit Settings (設定を編集) に移動します。Oculus アプリケーション ID を入力する必要があります。
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
        }
    }
}

ノート: Oculus ヘッドセットは開発者モードに設定する必要があります。これは、開発に使用する開発者アカウントにサインインすることで設定できます (Oculus 開発者アカウントは こちら で作成できます)。

戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成

SignInWithOculusAsync メソッドを使用して、以下のいずれかを行うことができます。

  • Oculus の認証情報を使用して新しい Unity Authentication プレイヤーを作成する。
  • Oculus の認証情報を使用して既存のプレイヤーをサインインする。

プロジェクト内の Unity Authentication プレイヤーが認証情報と関連付けられていない場合、SignInWithOculusAsync は新しいプレイヤーを作成します。プロジェクト内の Unity Authentication プレイヤーが認証情報と関連付けられている場合、SignInWithOculusAsync はそのプレイヤーアカウントにサインインします。

この関数ではキャッシュされたプレイヤーは考慮されません。SignInWithOculusAsync はキャッシュされたプレイヤーを置き換えます。

async 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 API を呼び出して、プレイヤーを Oculus セッションチケットにリンクします。

キャッシュされたプレイヤーが SDK に存在する場合は、キャッシュされたプレイヤーを Oculus アカウントにリンクできます。

  1. SignInAnonymouslyAsync を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
  2. LinkWithOculusAsync を使用して、キャッシュされたプレイヤーのアカウントを Oculus アカウントにリンクします。

キャッシュされたプレイヤーについて詳しくは、キャッシュされたプレイヤーのサインイン を参照してください。

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 アカウントをリンク解除する

UnlinkOculusAsync API を使用して、プレイヤーが自身の Oculus アカウントをリンク解除できるようにします。リンク解除後、そのアカウントが他のどの ID にもリンクされていない場合、そのアカウントは匿名アカウントに遷移します。

async 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);
   }
}