5
3

Meta XR Core SDKを利用して、Unity GUIをコントローラーからのレーザーで操作する

Last updated at Posted at 2024-01-07

はじめに

Oculus Integrationでは、サンプル実装としてUIHelper というPrefabが提供されていて、コントローラーからのレーザーでUIを操作することができました。
Meta XR xxx SDKとなったものでは同様のPrefabが提供されていないようだったので試してみました。

参考にさせて頂きました

環境

Unity 2022.3.16f1
Meta XR Core SDK 60.0.0
Meta Quest2
Meta Quest3

環境構築

  1. Unity Projectの作成
    3D(URP)でプロジェクトを作成します

  2. Android Platformに Switch Platform

  3. Package Manager から Meta XR Core SDKInstall

  4. Project Settings > Oculus の警告、推奨設定を適用
    スクリーンショット 2024-01-08 005822.png

    1. Fix Allをクリック
    2. Apply All をクリック
  5. Project Settings > Player の設定を修正

    1. Company Name を変更
    2. Other Settings > Identification > Override Default Package Name のチェックを外す

シーン作成

  1. 新しいシーンを作成して、Main Camera を削除

  2. Oculus > Tools > Building Blocks を開く
    スクリーンショット 2024-01-08 010648.png

    1. Camera Rig を追加
    2. Controller Tracking を追加
      スクリーンショット 2024-01-08 010904.png
  3. Unity GUI を利用して TextButton を配置
    ※ VR向けに CanvasRender ModeWorld Space に設定
    スクリーンショット 2024-01-08 011825.png

  4. Canvas オブジェクトに対して、Meta QuestのUIインタラクションを処理できるように設定を追加

    1. GraphicsRaycaster コンポーネントを無効化
    2. OVRRaycaster コンポーネントを追加
      スクリーンショット 2024-01-08 012558.png
  5. EventSystem オブジェクトに対して、Meta QuestのUIインタラクションを処理できるように設定を追加

    1. StandaloneInputModule コンポーネントを無効化
    2. OVRInputModule コンポーネントを追加
      スクリーンショット 2024-01-08 013111.png
  6. レーザーの先のポインターを作成

    1. 空のGameObjectとしてOVRGazePointer(名前は自由)を作成
    2. 空のGameObjectとしてGazeIcon(名前はこの名前でないとNG)を作成し、上で作成したOVRGazePointerの子オブジェクトにする
    3. GazeIconの子オブジェクトとして、レーザーの先のポインターとなるオブジェクトを追加
    4. OVRGazePointerオブジェクトにOVRGazePointer コンポーネントを追加
      1. Depth Scale Mulitplier の項目でポインターの大きさを調整
      2. Ray Transform の項目でレーザーの始点となる位置を設定
        スクリーンショット 2024-01-08 014855.png
    5. EventSystem オブジェクトの OVRInputModule コンポーネントを設定
      1. Ray Transform の項目でレーザーの始点となる位置を設定
      2. Cursor の項目に OVRGazePointer オブジェクトを設定
        スクリーンショット 2024-01-08 022851.png
  7. レーザーを作成

    1. 空のGameObjectとしてOVRPointerVisualizer(名前は自由)を作成
    2. LineRenderer コンポーネントを追加
      1. Width の項目でレーザーの太さを調整
      2. Materials の項目でレーザーの見た目を調整
    3. OVRPointerVisualizer コンポーネントを追加
      1. Ray Transform の項目でレーザーの始点となる位置を設定
      2. Line Pointer の項目で上で追加した LineRenderer コンポーネントを設定
        スクリーンショット 2024-01-08 020100.png
        ※今回、OVRPointerVisualizer コンポーネントは、デフォルトで提供されているものをカスタマイズして、ポインターが表示されている場合は、LineRendererの長さをポインターに合わせています。
    OVRPointerVisualizer.cs
    using UnityEngine;
    
    namespace UIControllerLaserPointer
    {
     public class OVRPointerVisualizer : MonoBehaviour
     {
         [Tooltip("Object which points with Z axis. E.g. CentreEyeAnchor from OVRCameraRig")]
         public Transform rayTransform;
    
         [Header("Visual Elements")] [Tooltip("Line Renderer used to draw selection ray.")]
         public LineRenderer linePointer = null;
    
         [Tooltip("Visually, how far out should the ray be drawn.")]
         public float rayDrawDistance = 2.5f;
         
         [SerializeField] private OVRGazePointer _ovrGazePointer = null;
    
         void LateUpdate()
         {
             linePointer.enabled = (OVRInput.GetActiveController() == OVRInput.Controller.Touch);
             Ray ray = new Ray(rayTransform.position, rayTransform.forward);
             linePointer.SetPosition(0, ray.origin);
             
             if (!_ovrGazePointer.hidden)
             {
                 linePointer.SetPosition(1, _ovrGazePointer.transform.position);
                 return;
             }
             linePointer.SetPosition(1, ray.origin + ray.direction * rayDrawDistance);
         }
     }
    }
    

実行結果

※ ボタンをクリックした際の処理の説明は割愛します

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3