存取 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);
        }
    }
}

另請參閱