MRTK での入力状態へのアクセス — MRTK2

入力ソースにアタッチされるコントローラーを反復処理することで、MRTKにおけるすべての入力の状態を直接照会できます。 MRTKは、目、手、頭、およびモーションコントローラーの位置と回転にアクセスするための便利な方法も提供します。

コントローラーの反復処理とInputRayUtilsクラスの使用の両方により入力を照会する一例については、InputDataExample シーンを参照してください。

例:MRTKにおける頭、手、目の位置と回転へのアクセス

MRTKのInputRayUtilsクラスは、ハンドレイ、ヘッドレイ、視線、モーションコントローラーレイにアクセスするための便利な方法を提供します。

次の名前空間をスクリプトに追加します。

using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;

サンプル コード:

// Get the head ray
UnityEngine.Ray headRay = InputRayUtils.GetHeadGazeRay();

// Get the right hand ray
if (InputRayUtils.TryGetHandRay(Handedness.Right, out UnityEngine.Ray rightHandRay))
{
    // Right hand ray is available
}
else
{
    // Right hand ray is not available
}

例:シーンでアクティブになっているすべての6DOFコントローラーの位置と回転へのアクセス

次の名前空間をスクリプトに追加します。

using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Input;

サンプル コード:

foreach (IMixedRealityController controller in CoreServices.InputSystem.DetectedControllers)
{
    // Interactions for a controller is the list of inputs that this controller exposes
    foreach (MixedRealityInteractionMapping interactionMapping in controller.Interactions)
    {
        // 6DOF controllers support the "SpatialPointer" type (pointing direction)
        // or "GripPointer" type (direction of the 6DOF controller)
        if (interactionMapping.InputType == DeviceInputType.SpatialPointer)
        {
            Debug.Log("Spatial pointer PositionData: " + interactionMapping.PositionData);
            Debug.Log("Spatial pointer RotationData: " + interactionMapping.RotationData);
        }

        if (interactionMapping.InputType == DeviceInputType.SpatialGrip)
        {
            Debug.Log("Spatial grip PositionData: " + interactionMapping.PositionData);
            Debug.Log("Spatial grip RotationData: " + interactionMapping.RotationData);
        }
    }
}

関連項目